本文主要是介绍滑动速度切换画笔大小,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
在做Note 应用的时候,有个需求是要橡皮擦的粗细随着pecil的移动速度来控制。
其实只要了解一个类就足够了:
VelocityTracker
贴出部分代码:
private VelocityTracker mTracker;@Overridepublic 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) {//erasemPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));CUR = MODE_CLEAR;} else {//paintmPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SCREEN));CUR = MODE_NORMAL;}}
这篇关于滑动速度切换画笔大小的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!