Android 模拟滑动事件

2024-11-19 21:26:36
推荐回答(4个)
回答1:

private int start_x;

private int start_y;

private int stop_x;
private int stop_y;

//模拟移动一张图片
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN://手指落下时

mode = DRAG;
stop_x = (int) event.getRawX();
stop_y = (int) event.getRawY();

//start_x = (int) event.getX();
start_x=stop_x-this.getLeft();
start_y = stop_y - this.getTop();
break;
case MotionEvent.ACTION_MOVE://然后在Move里写你的单指移动的过程比如移动一张图片
if (mode == DRAG) {
if (Math.abs(stop_x - start_x - getLeft()) < 88
&& Math.abs(stop_y - start_y - getTop()) < 85) {
isZoom = false;
this.setPosition(stop_x - start_x, stop_y - start_y, stop_x
+ this.getWidth() - start_x, stop_y - start_y
+ this.getHeight());

stop_x = (int) event.getRawX();
stop_y = (int) event.getRawY();
break;

}
}

}

}

回答2:

这个有点深度。
给个思路(未验证,估计玄):
用一个线程每隔30毫秒左右生成一个MotionEvent,传递到UI线程调用
dispatchTouchEvent(MotionEvent ev) 这个方法用来分发TouchEvent

回答3:

ViewPager可以实现

回答4:

系统自带的功能