滑动速度切换画笔大小

在做Note 应用的时候,有个需求是要橡皮擦的粗细随着pecil的移动速度来控制。


其实只要了解一个类就足够了:

VelocityTracker

贴出部分代码:

private VelocityTracker mTracker;

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        mEvent = 0;
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                LogUtils.d(TAG, ("touch down\n"));
                startUpdating();

                if (mTracker == null) {
                    mTracker = VelocityTracker.obtain();
                } else {
                    mTracker.clear();<pre name="code" class="java">    public void initPaint(int b, int w) {
        mPaint.setColor(b);
        mPaint.setStrokeWidth(w);
    }

} mTracker.addMovement(event); break; case MotionEvent.ACTION_MOVE: LogUtils.d(TAG, ("touch move\n")); mTracker.addMovement(event); mTracker.computeCurrentVelocity(1000); float abx = Math.abs(mTracker.getXVelocity()); float aby = Math.abs(mTracker.getYVelocity()); float abs = Math.max(abx, aby); // 0 - 12000 int a; // 0 - 40 if (CUR == MODE_CLEAR) { a = (int) (abs / 100); if (a < 5) a = 5; // min else if (a > 40) a = 50; // max initPaint(0x00FFFFFF, a); } break; case MotionEvent.ACTION_UP: LogUtils.d(TAG, ("touch up\n")); mUpdateFlag = false; break; case MotionEvent.ACTION_CANCEL: mTracker.recycle(); break; }

 

    public void initPaint(int b, int w) {
        mPaint.setColor(b);
        mPaint.setStrokeWidth(w);
    }


    public static final int MODE_CLEAR  = 0;
    public static final int MODE_NORMAL = 1;
    public              int CUR         = MODE_NORMAL;

    public void setPaint(int b, int w, boolean p) {
        this.flag = false;
        initPaint(b, w);
        if (p) {
            //erase
            mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
            CUR = MODE_CLEAR;
        } else {
            //paint
            mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SCREEN));
            CUR = MODE_NORMAL;
        }
    }





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值