建立android畫筆程式的例子(有鏡面效果)

來源:互聯網
上載者:User

標籤:android手指畫   鏡面效果   

先:



關鍵是在檢測到手指移動的時候用mPath.quadTo的方法,android sdk解釋是:

Add a quadratic bezier from the last point, approaching control point (x1,y1), and ending at (x2,y2). If no moveTo() call has been made for this contour, the first point is automatically set to (0,0).

中文是用貝茲路徑連結了(x1,y1),(x2,y2)這兩個點,如果沒有moveTo()這個方法調用的話,第一個點預設為(0,0)


android畫圖是用Canvas的API,如畫一個實心的矩形,可以用在重寫一個View的onDraw():

<span style="white-space:pre"></span>Rect rect = new Rect(100,100,500,500);mPaint.setStrokeWidth(5); //設定畫筆的粗細mPaint.setColor(Color.RED); //設定畫筆的顏色mPaint.setStyle(Style.FILL); //填充整個圖形mPaint.setAntiAlias(true); //消除鋸齒效果canvas.drawRect(rect, mPaint);

對於檢測手指的移動,我們可以用onTouchEvent來實現:

private float mX, mY;private float mOppositeX, mOppositeY;private static final float TOUCH_TOLERANCE = 4; //當手指移動超過4時我們才去set Path@Overridepublic boolean onTouchEvent(MotionEvent event) {float x = event.getX();float y = event.getY();switch (event.getAction()) {case MotionEvent.ACTION_DOWN:touch_start(x, y);if(isMirrorDraw) {touch_opposite_start(x, y);}invalidate();break;case MotionEvent.ACTION_MOVE:touch_move(x, y);if(isMirrorDraw) {touch_opposite_move(x, y);}invalidate();break;case MotionEvent.ACTION_UP:touch_up();if(isMirrorDraw) {touch_opposite_up();}invalidate();break;}return true;}private void touch_start(float x, float y) {mPath.reset();mPath.moveTo(x, y);mX = x;mY = y;}private void touch_move(float x, float y) {float dx = Math.abs(x - mX);float dy = Math.abs(y - mY);if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) {mPath.quadTo(mX, mY, (x + mX) / 2, (y + mY) / 2);mX = x;mY = y;}}private void touch_up() {mPath.lineTo(mX, mY);mCanvas.drawPath(mPath, mPaint);mPath.reset();}private void touch_opposite_up() {mOppositePath.lineTo(mOppositeX, mY);mCanvas.drawPath(mOppositePath, mOppoPaint);mOppositePath.reset();}private void touch_opposite_move(float x, float y) {float oppositeX = OppositeDrawActivity.screenWidth - x; //<span style="font-family: Arial, Helvetica, sans-serif;">OppositeDrawActivity.screenWidth是螢幕寬度</span>float dx = Math.abs(oppositeX - mOppositeX);float dy = Math.abs(y - mY);if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) {mOppositePath.quadTo(mOppositeX, mY, (mOppositeX + oppositeX) / 2, (y + mY) / 2);mOppositeX = oppositeX;mY = y;}}private void touch_opposite_start(float x, float y) {mOppositePath.reset();float oppositeX = OppositeDrawActivity.screenWidth - x;mOppositePath.moveTo(oppositeX, y);mOppositeX = oppositeX;}

然後重寫onDraw():

@Overrideprotected void onDraw(Canvas canvas) {canvas.drawColor(0xFFAAAAAA);canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);canvas.drawPath(mPath, mPaint);if(isMirrorDraw) {canvas.drawPath(mOppositePath, mOppoPaint);}}

代碼可以在http://download.csdn.net/detail/baidu_nod/7572549下載

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.