Android可按住拖拉的View,悬浮窗拖拉效果
作者:
小明好爱学习 | 来源:发表于
2019-09-29 15:45 被阅读0次package com.bg.sdk.floatwin;
import android.app.Activity;
import android.view.MotionEvent;
import android.view.ViewGroup;
import android.widget.ImageView;
/**
* @author ljj
* @create-time 2019/9/10
* @desc
*/
public class BGFloatView extends ImageView {
private int screenWidth;
private int screenHeight;
public BGFloatView(Activity activity) {
super(activity);
setBackgroundColor(activity.getResources().getColor(android.R.color.holo_blue_bright));
ViewGroup.LayoutParams viewParams = new ViewGroup.LayoutParams(
100, 100);
setLayoutParams(viewParams);
ViewGroup contentView = activity.findViewById(android.R.id.content);
screenWidth = contentView.getWidth(); //根据实际情况设置可移动的范围
screenHeight = contentView.getHeight();
}
float downX, downY;
@Override
public boolean onTouchEvent(MotionEvent event) {
super.onTouchEvent(event);
if (this.isEnabled()) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
downX = event.getRawX();
downY = event.getRawY();
break;
case MotionEvent.ACTION_MOVE:
float x = getX() + (event.getRawX() - downX);
float y = getY() + (event.getRawY() - downY);
downX = event.getRawX();
downY = event.getRawY();
if (x < 0) {
x = 0;
}
if (y < 0) {
y = 0;
}
if (x >= screenWidth - getWidth()) {
x = screenWidth - getWidth();
}
if (y >= screenHeight - getHeight()) {
y = screenHeight - getHeight();
}
setX(x);
setY(y);
break;
}
return true;
}
return false;
}
}
本文标题:Android可按住拖拉的View,悬浮窗拖拉效果
本文链接:https://www.haomeiwen.com/subject/mrjbyctx.html
网友评论