action_main
MyDragPicture
package wanghuiqi.bawie.com.whq_dalls;
import android.annotation.SuppressLint;
import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.ImageView;
@SuppressLint("AppCompatCustomView")
public class MyDragPicture extends ImageView {
private int mDownX;
private int mDownY;
public MyDragPicture(Context context) {
super(context);
}
public MyDragPicture(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
//获取手指处的横坐标和纵坐标
int x = (int) event.getX();
int y = (int) event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN://手指按下
mDownX = x;
mDownY = y;
break;
case MotionEvent.ACTION_MOVE://手指移动
//计算移动的距离
int moveX = x - mDownX;
int moveY = y - mDownY;
//调用layout方法重新放置它的位置
movingXY(moveX,moveY);
break;
case MotionEvent.ACTION_UP:
break;
}
return true;
}
private void movingXY(int moveX, int moveY) {
//移动View
layout(getLeft()+moveX,getTop()+moveY,getRight()
+moveX,getBottom()+moveY);
}
}