View滑动的六种方法

view滑动方法1:通过layout重新设置左上右下参数,刷新界面

//实现效果,view跟随手指移动
@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;
                layout(getLeft()+offsetX,getTop()+offsetY,getRight()+offsetX,getBottom()+offsetY);
                break;
        }
        return true;
    }

view滑动方法2:offsetLeftAndRight,offsetTopAndBottom

//实现效果,view跟随手指移动
@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;
        }
        return true;
    }

view滑动方法3: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;
                //View 父类groupView必须是LinearLayout,或者换成ViewGroup.LayoutParams
                LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) getLayoutParams();
                layoutParams.leftMargin = getLeft()+offsetX;
                layoutParams.topMargin = getTop()+offsetY;
                setLayoutParams(layoutParams);
                break;
        }
        return true;
    }

view滑动方法4:动画

view视图 动画
1.
    
    
        

    
    2.
      watchView.setAnimation(AnimationUtils.loadAnimation(this,R.anim.translate_view));
      view动画不能改变view的位置参数

  或者利用属性动画来移动
 ObjectAnimator.ofFloat(watchView,"translationX",0,300)
                .setDuration(1000)
                .start();

view移动方法5: scrollTo,scrollBy

    /**
     * 移动到一个具体的点
     * @param x
     * @param y
     */
    @Override
    public void scrollTo(int x, int y) {
        super.scrollTo(x, y);
        LogUtil.d("hh","customView scrollTo");
    }

    /**
     * 移动的增量,最终还是会调用scrollTo
     * @param x
     * @param y
     */
    @Override
    public void scrollBy(int x, int y) {
        super.scrollBy(x, y);
        LogUtil.d("hh","customView 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;
                //父类group view必须是LinearLayout
                ((View)getParent()).scrollBy(-offsetX,-offsetY);
                break;
        }
        return true;
    }

view滑动方法6:scroller

    scrollTo,scrollBy移动的是瞬间完成的,没有过渡效果,可以用Scroller来完成
    private Scroller mScroller = new Scroller(mContext); 

 /**
     * Scroller本身是不能实现view的滑动的,它需要和view的computeScroll()方法配合才能实现弹性滑动的效果
     * 系统会在绘制view的时候在draw()方法中调用该方法
     * 调用父类的scrollTo()方法,并通过Scroller来不断获取当前的滚动值,每滑动一小段距离
     * 我们就调用invalidate()方法不断重绘,重绘就会调用computeScroll(),这样就不断的移动一个小的距离并连贯起来就实现了平滑的移动
     */
    @Override
    public void computeScroll() {
        super.computeScroll();
        if (mScroller.computeScrollOffset()){
            ((View)getParent()).scrollTo(mScroller.getCurrX(),mScroller.getCurrY());
            invalidate();
        }
    }

    /**
     * 在2000ms内沿X轴平移deltaX像素,在需要用到的地方调用即可
     * @param destX
     */
    public void smoothScrollTo(int destX){
        int scrollX = getScrollX();
        int deltaX = destX-scrollX;//x轴增量
        mScroller.startScroll(scrollX,0,deltaX,0,2000);
    }

你可能感兴趣的:(View滑动的六种方法)