public class Custom extends View {
private int lastx;
private int lasty;
public Custom(Context context) {
super(context);
}
public Custom(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
public Custom(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
int x= (int) event.getX();
int y= (int) event.getY();
switch (event.getAction()){
case MotionEvent.ACTION_DOWN:
lastx=x;
lasty=y;
break;
case MotionEvent.ACTION_MOVE:
int offsetx=x-lastx;
int offsety=y-lasty;
layout(getLeft()+offsetx,getTop()+offsety,getRight()+offsetx,getBottom()+offsety);
break;
}
return true;
}
}
简单的view滑动效果
最新推荐文章于 2021-07-11 20:34:58 发布