android滑动的六种方式

public class MyView extends View {

    int lastX;
    int lastY;
    Scroller mScroller;

    public MyView(Context context, AttributeSet attributeSet){
        super(context, attributeSet);
        mScroller = new Scroller(context);
    }

    //6、2  属性动画一定要实现该属性的set方法
    public void setWidth(int width){
        getLayoutParams().width = width;
        requestLayout();
    }

    public int getW(){
        return getLayoutParams().width;

    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        Log.i("_tag","MyView  dispatchTouchEvent");
        return super.dispatchTouchEvent(event);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        Log.i("_tag","MyView  onTouchEvent  " + event.getAction());
        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;
                //1、滑动方法1 --->  layout()
//                layout(getLeft() + offsetX, getTop() + offsetY, getRight() + offsetX, getBottom() + offsetY);
                //2、滑动方法2 ------> offsetLeftAndRight()
//                offsetLeftAndRight(offsetX);
//                offsetTopAndBottom(offsetY);
                //3、滑动方法3  ------->  layoutParams
//                ViewGroup.MarginLayoutParams layoutParams = (ViewGroup.MarginLayoutParams) getLayoutParams();
//                layoutParams.leftMargin = getLeft() + offsetX;
//                layoutParams.topMargin = getTop() + offsetY;
//                setLayoutParams(layoutParams);

                //4、scrollBy是移动一定的偏移,scrollTo是移动到某个位置
//                ((View)getParent()).scrollBy(-offsetX, -offsetY);
                break;
            case MotionEvent.ACTION_UP:
                View viewGroup = ((View)getParent());
                //5.1、使用 Scroller 结合  scrollTo()
                mScroller.startScroll(viewGroup.getScrollX(), viewGroup.getScrollY(), viewGroup.getScrollX(), viewGroup.getScrollY());
                invalidate();

                //6.1、属性动画
                AnimatorSet animatorSet = new AnimatorSet();
                animatorSet.playTogether(
                        ObjectAnimator.ofInt(this, "width", getW(), 100),
                        ObjectAnimator.ofFloat(this, "rotationX", 0, 360),
                        ObjectAnimator.ofFloat(this, "rotationX", 0, 180),
                        ObjectAnimator.ofFloat(this, "rotation", 0, -90),
                        ObjectAnimator.ofFloat(this, "translationX", 0, 90),
                        ObjectAnimator.ofFloat(this, "translationY", 0, 90),
                        ObjectAnimator.ofFloat(this, "scaleX", 1, 1.5F),
                        ObjectAnimator.ofFloat(this, "scaleY", 1, 0.5F),
                        ObjectAnimator.ofFloat(this, "alpha", 1, 0.25F, 1)
                );
                
                break;
        }

        return super.onTouchEvent(event);
//        return true;
    }


    //5.2、使用Scroller, 循环:invalidate()---> onDraw() ---> computeScroll()
    @Override
    public void computeScroll() {
        super.computeScroll();
        //判断scroller是否执行完毕
        if(mScroller.computeScrollOffset()){
            ((View)getParent()).scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
            invalidate();
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值