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 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值