android 控件拖动并添加归位动画
程序员文章站
2022-07-14 17:41:45
...
用两种简单实现方式:
第一种:
relBottom.setOnTouchListener(new View.OnTouchListener() {
private int downX;
private int downY;
private int viewLeft;
private int viewRight;
private int viewTop;
private int viewBottom;
@Override
public boolean onTouch(final View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
isMoving=false;
//按下位置
downX = (int) event.getRawX();
downY = (int) event.getRawY();
//初始位置
viewLeft=v.getLeft();
viewRight=v.getRight();
viewTop=v.getTop();
viewBottom=v.getBottom();
break;
case MotionEvent.ACTION_MOVE:
int moveX = (int) event.getRawX();
int moveY = (int) event.getRawY();
int dx = moveX - downX;
int dy = moveY - downY;
int l = v.getLeft();
int r = v.getRight();
int t = v.getTop();
int b = v.getBottom();
// 更新位置
v.layout(l + dx, t + dy, r + dx, b + dy);
// 下一次按下位置
downX = (int) event.getRawX();
downY = (int) event.getRawY();
break;
case MotionEvent.ACTION_UP:
//归位
v.layout(viewLeft,viewTop,viewRight,viewBottom);
break;
}
return true;
}
});
第二种:
relBottom.setOnTouchListener(new View.OnTouchListener() {
private int downX;
private int downY;
@Override
public boolean onTouch(final View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
isMoving=false;
//按下位置
downX = (int) event.getRawX();
downY = (int) event.getRawY();
break;
case MotionEvent.ACTION_MOVE:
int moveX = (int) event.getRawX();
int moveY = (int) event.getRawY();
int dx = moveX - downX;
int dy = moveY - downY;
float translationX=v.getTranslationX()+dx;
float translationY=v.getTranslationY()+dy;
v.setTranslationX(translationX);
v.setTranslationY(translationY);
// 下一次按下位置
downX = (int) event.getRawX();
downY = (int) event.getRawY();
break;
case MotionEvent.ACTION_UP:
//归位
v.setTranslationX(0);
v.setTranslationY(0);
break;
}
return true;
}
});
第二种 归位动画实现:
case MotionEvent.ACTION_UP:
isMoving=true;
final int avgX= (int) Math.abs(v.getTranslationX()/10)==0 ? 1 : (int)Math.abs(v.getTranslationX()/10);
final int avgY= (int) Math.abs(v.getTranslationY()/10)==0 ? 1 : (int)Math.abs(v.getTranslationY()/10);
new Thread(new Runnable() {
@Override
public void run() {
while (isMoving){
Message msg=new Message();
msg.what=MOVING_VIEW_WHAT;
msg.obj=v;
msg.arg1=avgX;
msg.arg2=avgY;
countDownHandler.sendMessage(msg);
try {
Thread.sleep(20);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
private boolean isMoving;
private Handler countDownHandler =new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if(MOVING_VIEW_WHAT==msg.what){
View view=((View)msg.obj);
int avgX=msg.arg1;
int avgY=msg.arg2;
float tX=0;
if(view.getTranslationX()!=0){
tX=view.getTranslationX()>0 ? ((view.getTranslationX()-avgX)<0 ? 0 : view.getTranslationX()-avgX) : ((view.getTranslationX()+avgX)>0 ? 0 : view.getTranslationX()+avgX) ;
}
float tY=0;
if(view.getTranslationY()!=0){
tY=view.getTranslationY()>0 ? ((view.getTranslationY()-avgY)<0 ? 0 : view.getTranslationY()-avgY) : ((view.getTranslationY()+avgY)>0 ? 0 : view.getTranslationY()+avgY) ;
}
view.setTranslationX(tX);
view.setTranslationY(tY);
isMoving= (tX!=0 || tY!=0);
LogTools.i(TAG,"MOVING_VIEW_WHAT==tX=="+tX+",tY=="+tY+",avgX="+avgX+",avgY="+avgY+",isMoving="+isMoving);
}
}
};
如需第一种归位动画,请参考第二种归位动画实现方法,实现思路一致的
上一篇: LeetCode之括号生成问题
下一篇: LeetCode算法之22-括号生成