Android滑动绘制坐标和view的移动

1.Android坐标系 ,原点位于屏幕的左上角.


2.视图坐标系,原点位于父视图的左上角.


3.



1. layout(左,上,右,下)

public class CustomView extends View {
    private int lastX;
    private int lastY;
    public CustomView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }
    public CustomView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    public CustomView(Context context) {
        super(context);
    }
    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方法来重新放置它的位置  layout(getLeft()+offsetX, getTop()+offsetY,
                        getRight()+offsetX , getBottom()+offsetY);
                break;
        }
        return true;
    }
}

2. offsetLeftAndRight()和offsetTopAndBottom()

public class CustomView extends View {
    private int lastX;
    private int lastY;
    public CustomView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }
    public CustomView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    public CustomView(Context context) {
        super(context);
    }
    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;
                //leftright进行偏移  offsetLeftAndRight(offsetX);
                //topbottom进行偏移  offsetTopAndBottom(offsetY);
                break;
        }
        return true;
    }
}
 3.  
 layoutParams. 
 leftMargin 和  
  
 layoutParams. 
 topMargin 
 

public class CustomView extends View {
    private int lastX;
    private int lastY;
    public CustomView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }
    public CustomView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    public CustomView(Context context) {
        super(context);
    }
    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;
                LinearLayout.LayoutParams layoutParams= (LinearLayout.LayoutParams) getLayoutParams();
                layoutParams.leftMargin = getLeft() + offsetX;
                layoutParams.topMargin = getTop() + offsetY;
                setLayoutParams(layoutParams);
                break;
        }
        return true;
    }
}
 
 
4.给view设置动画
<1.
 
 
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate android:fromXDelta="0" android:toXDelta="300" android:duration="1000"/>
</set>
 
 
mCustomView.setAnimation(AnimationUtils.loadAnimation(this, R.anim.translate));
<2.属性动画
ObjectAnimator.ofFloat(mCustomView,"translationX",0,300).setDuration(1000).start();

5.scollTo与scollBy
转载:http://blog.csdn.net/itachi85/article/details/50724558