Android实现滑动的方法之二

前面通过了layout或者margin值来实现了View的滑动,这里使用ScrollTo与ScrollBy

ScrollTo与ScrollBy

ScrollTo表示移动到一个具体的坐标

ScrollBy表示移动一定的偏移量

那么我们把上节方法进行改变,看其移动效果

public class DragView1 extends View {

    private int lastX;
    private int lastY;

    public DragView1(Context context, AttributeSet attrs) {
        super(context, attrs);
        setBackgroundColor(Color.BLUE);
    }


    @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;

                // ScrollTo与ScrollBy
                scrollBy(offsetX, offsetY);
                break;
        }
        return true;
    }
}

结果是并不能移动,由于该方法是移动View的content,比如ViewGroup中的子View,TextView的文本,ImageView的drawable对象

那么修改如下

public class DragView1 extends View {

    private int lastX;
    private int lastY;

    public DragView1(Context context, AttributeSet attrs) {
        super(context, attrs);
        setBackgroundColor(Color.BLUE);
    }


    @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;

                // ScrollTo与ScrollBy
                // scrollBy(offsetX, offsetY);

                // 一闪而过
                ((View)getParent()).scrollBy(offsetX, offsetY);

                break;
        }
        return true;
    }
}

现在可以移动了,但效果与预期的很不相同,一闪而过,基本看不到移动的轨迹,实际上这里移动与之前参数的改变有所不同,前面偏移是以屏幕的左上角为原点,而当调用Scroll方法时,是以ViewGroup左上角为原点便宜

假如屏幕中有一控件坐标为(10,10),现在要其向屏幕的右边移动10,那么ViewGroup不变,既然要向右,那么屏幕就要向左,这样就是移动-10的偏移量了

public class DragView1 extends View {

    private int lastX;
    private int lastY;

    public DragView1(Context context, AttributeSet attrs) {
        super(context, attrs);
        setBackgroundColor(Color.BLUE);
    }


    @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;

                // ScrollTo与ScrollBy
                // scrollBy(offsetX, offsetY);

                // 一闪而过
                // ((View)getParent()).scrollBy(offsetX, offsetY);

                ((View)getParent()).scrollBy(-offsetX, -offsetY);
                break;
        }
        return true;
    }
}

此时就能正常的移动了,实际就是正常的偏移量取负

  

  

posted @ 2015-11-04 20:19  轻云沉峰  阅读(145)  评论(0)    收藏  举报