当前位置:主页 > 移动开发 > Android代码 >

Android 实现滑动的六种方式

时间:2022-10-14 10:24:17 | 栏目:Android代码 | 点击:

1、效果视频

2、剖析Android坐标系

滑动的本质是移动,滑动的原理就是通过不断的改变View的坐标而实现。

Android系统提供了很多方法获取坐标值,可以将其分为两种类别,具体如下:

View 提供的获取坐标的方法:

MotionEvent 提供的方法:

3、实现方式

3.1 layout

使用绝对坐标系,每次执行移动逻辑后需要重新设置初始化坐标

 @Override
    public boolean onTouchEvent(MotionEvent event) {
        int rawX = (int) (event.getRawX());
        int rawY = (int) (event.getRawY());
                switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:
                lastX = rawX;
                lastY = rawY;
                break;
            case MotionEvent.ACTION_MOVE:
                int offsetX = rawX - lastX;
                int offsetY = rawY - lastY;
               layout( getLeft()+offsetX,getTop()+offsetY,getRight()+offsetX,getBottom()+offsetY );
               lastX = rawX;
               lastY = rawY;
                break;
            case MotionEvent.ACTION_UP:
                break;

        }
        return true;
    }

3.2 scrollBy

    @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;
                ((View) getParent()).scrollBy( -offsetX, -offsetY );
                break;
            case MotionEvent.ACTION_UP:
                break;

        }
        return true;
    }

3.3 offsetLeftAndRight offsetTopAndButton

 @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;
               offsetLeftAndRight( offsetX );
               offsetTopAndBottom( offsetY );
                break;
            case MotionEvent.ACTION_UP:
                break;

        }
        return true;
    }

3.4 LayoutParams

通过getLayoutParams()获取LayoutParams时,需要根据不同的父类型使用设置不同的类型,比如父布局为LinearLayout则设置类型为 LinearLayout.LayoutParams

    @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;
                LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams)getLayoutParams();
                layoutParams.leftMargin = getLeft()+offsetX;
                layoutParams.topMargin = getTop()+offsetY;
                setLayoutParams( layoutParams );
                break;
            case MotionEvent.ACTION_UP:
                break;

        }
        return true;
    }

更方便的是直接使用ViewGroup,不需要判断父布局类型

  ViewGroup.MarginLayoutParams layoutParams = (ViewGroup.MarginLayoutParams)getLayoutParams();
                layoutParams.leftMargin = getLeft()+offsetX;
                layoutParams.topMargin = getTop()+offsetY;
                setLayoutParams( layoutParams );

3.5 Scroller

初始化Scroller:

 scroller = new Scroller( context );

重写computeScroll()方法

使用computeScrollOffset()判定是否完成了整个滑动:

   @Override
    public void computeScroll() {
        if (scroller.computeScrollOffset()) {
            ((View) getParent()).scrollTo( scroller.getCurrX(), scroller.getCurrY() );
            invalidate();
        }
    super.computeScroll();
    }

  @Override
    public boolean onTouchEvent(MotionEvent mv) {
        View parent = (View) getParent();
        switch (mv.getAction()) {
            case MotionEvent.ACTION_DOWN:
                lastX = (int) mv.getX();
                lastY = (int) mv.getY();
                break;
            case MotionEvent.ACTION_MOVE:
                int upX = (int) (mv.getX() - lastX);
                int upY = (int) (mv.getY() - lastY);
                parent.scrollBy( -upX, -upY );
                break;
            case MotionEvent.ACTION_UP:
                scroller.startScroll( parent.getScrollX(), parent.getScrollY(), -parent.getScrollX(), -parent.getScrollY(), 1000 );
                invalidate();
                break;
        }
        return super.onTouchEvent( mv );
    }

3.6 平移动画

效果视频:

private void InitAnimation(){
        animation = new TranslateAnimation( 0,200,0,200 );
        animation.setDuration( 1000 );
    }

 moveButton.startAnimation( animation );

您可能感兴趣的文章:

相关文章