frameView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
int action = event.getAction();
final int screenWidth = dm.widthPixels;
final int screenHeight = dm.heightPixels;
switch(action){
case MotionEvent.ACTION_DOWN:
lastX = (int) event.getRawX();// 获取触摸事件触摸位置的原始X坐标
lastY = (int) event.getRawY();
break;
case MotionEvent.ACTION_MOVE:
int dx = (int) event.getRawX() - lastX;
int dy = (int) event.getRawY() - lastY;
int l = v.getLeft() + dx;
// int b = v.getBottom() + dy;
int b = v.getBottom();
int r = v.getRight() + dx;
// int t = v.getTop() + dy;
int t = v.getTop();
// 下面判断移动是否超出屏幕
if (l < 0) {
l = 0;
r = l + v.getWidth();
}
if (t < 0) {
t = 0;
b = t + v.getHeight();
}
if (r > screenWidth) {
r = screenWidth;
l = r - v.getWidth();
}
if (b > screenHeight) {
b = screenHeight;
t = b - v.getHeight();
}
v.layout(l, t, r, b);
lastX = (int) event.getRawX();
lastY = (int) event.getRawY();
v.postInvalidate();
break;
case MotionEvent.ACTION_UP:
if (mMediaInfo.prepare()){
cupX = (int) (((double)frameView.getX()/screenWidth)*mMediaInfo.vWidth);
}
break;
default:
break;
}
return true;
}
});
实现View跟着手指移动
最新推荐文章于 2019-10-16 22:38:52 发布