ViewDragHelper实战,实现滑动解锁

说到滑动解锁,就回到了2012~2014年,iPhone4S、5、5S年代,如今准备踏入2020年,这些年国产机崛起,再也不是公交车上都是iPhone4S的场景。本篇来使用ViewDragHelper实现滑动解锁。

成品展示

 

ViewDragHelper实战,实现滑动解锁.gif

 

 

先来分析一下页面的元素

  1. 背景图
  2. 圆角滑道
  3. 圆形滑块
  4. 闪动提示文字

其他一些细节:

  1. 滑道和圆形滑块之间有些边距,我们使用padding来处理。

我们需要自定义的就是第2点,这个滑道包含一个滑块的图片和提示文字,滑块使用原生ImageView即可,而提示文字则是一个支持渐变着色的TextView(不是重点)。

渐变着色的TextView

先秒掉简单的,渐变着色的TextView,不是重点,代码量不多。

public class ShineTextView extends TextView {
    private LinearGradient mLinearGradient;
    private Matrix mGradientMatrix;
    private int mViewWidth = 0;
    private int mTranslate = 0;

    public ShineTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        if (mViewWidth == 0) {
            mViewWidth = getMeasuredWidth();
            if (mViewWidth > 0) {
                Paint paint = getPaint();
                mLinearGradient = new LinearGradient(0,
                        0,
                        mViewWidth,
                        0,
                        new int[]{getCurrentTextColor(), 0xffffffff, getCurrentTextColor()},
                        null,
                        Shader.TileMode.CLAMP);
                paint.setShader(mLinearGradient);
                mGradientMatrix = new Matrix();
            }
        }
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (mGradientMatrix != null) {
            mTranslate += mViewWidth / 5;
            if (mTranslate > 2 * mViewWidth) {
                mTranslate = -mViewWidth;
            }
            mGradientMatrix.setTranslate(mTranslate, 0);
            mLinearGradient.setLocalMatrix(mGradientMatrix);
            //每80毫秒执行onDraw()
            postInvalidateDelayed(80);
        }
    }
}

下面重点介绍我们使用ViewDragHelper实现拽托、滑动的滑道View:SlideLockView

让滑块滑动起来

滑道实际就是一个FrameLayout,我们使用ViewDragHelper将滑块ImageView进行拽托,主要我们要做以下几件事:

  1. 限制拽托的左侧起点、右侧终点(否则滑块就出去啦!)
  2. 松手时判断滑块的x坐标是偏向滑道的左侧还是右侧,来决定滑动到起点还是终点。
  3. 滚动结束,判断是否到达了右侧的终点。
  4. 判断拽托速度,如果超过指定速度,则自动滚动滑块到右侧终点。

看到这4点,如果让我们用事件分发来处理,代码量和判断会非常多,并且需要做速度检测,而使用ViewDragHelper,上面4点都封装好啦,我们添加一个回调,再将事件委托给它,在回调中做事情上面4点的处理,一切都简单起来了。

  • 创建SlideLockView,继承FrameLayout
public class SlideLockView extends FrameLayout {
    /**
     * 拽托帮助类
     */
    private ViewDragHelper mViewDragHelper;
    
    public SlideLockView(@NonNull Context context) {
        this(context, null);
    }

    public SlideLockView(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public SlideLockView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs, defStyleAttr);
    }

    private void init(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
    	//进行初始化...
    }
}
  • 创建ViewDragHelper,使用create静态方法创建,有3个参数,第一个拽托控件的父控件(就是当前View),第二个参数是拽托灵敏度,数值越大,越灵敏,默认为1.0,第三个参数为回调对象。
private void init(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
    final SlideLockView slideRail = this;
    mViewDragHelper = ViewDragHelper.create(this, 0.3f, new ViewDragHelper.Callback() {
        ...
    });
}
  • 委托onInterceptTouchEvent、onTouchEvent事件给ViewDragHelper
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值