您好,登錄后才能下訂單哦!
前言
任何View都有觸摸事件,經常在自定義控件時重寫setOnTouchListener
本篇通過手繪圖片來講述這個知識點,下面話不多說了,來一起看看詳細的介紹吧
本篇分為三個等級:一覽圖:
直線
曲線
筆觸
LEVEL1:基礎實現
在Activity中通過一個全屏的Bitmap創建的Canvas繪制
為ImageView添加觸摸事件監聽。
1.成員變量
ImageView mIdIvShow; float downX = 0; float downY = 0; float upX = 0; float upY = 0; private Canvas mCanvas; private Paint mPaint;
2.創建畫布
//獲取屏幕尺寸 Point point = new Point(); getWindowManager().getDefaultDisplay().getSize(point); //創建一個和屏幕一樣大的Bitmap Bitmap bitmap = Bitmap.createBitmap(point.x, point.y, Bitmap.Config.ARGB_8888); //創建Canvas對象 mCanvas = new Canvas(bitmap); mPaint = new Paint(Paint.ANTI_ALIAS_FLAG); mPaint.setStrokeWidth(10); mPaint.setColor(Color.RED); //將bitmap用ImageView展示 mIdIvShow.setImageBitmap(bitmap);
3.監聽事件
mIdIvShow.setOnTouchListener((v, event) -> { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: downX = event.getX(); downY = event.getY(); L.d("按下:(" + downX + "," + downY + ")" + L.l()); break; case MotionEvent.ACTION_CANCEL: break; case MotionEvent.ACTION_MOVE: break; case MotionEvent.ACTION_UP: upX = event.getX(); upY = event.getY(); L.d("抬起:(" + upX + "," + upY + ")" + L.l()); mCanvas.drawLine(downX, downY, upX, upY, mPaint); mIdIvShow.invalidate();//更新視圖 break; } return true; }); }
升級版:LEVER2
mIdIvShow.setOnTouchListener((v, event) -> { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: downX = event.getX(); downY = event.getY(); break; case MotionEvent.ACTION_CANCEL: break; case MotionEvent.ACTION_MOVE: upX = event.getX(); upY = event.getY(); mCanvas.drawLine(downX, downY, upX, upY, mPaint); mIdIvShow.invalidate(); //更新點位 downY = upY; downX = upX; break; case MotionEvent.ACTION_UP: //抬起點Y>1100,清除筆跡 if (upY > 1100) { Paint paint = new Paint(); paint.setColor(Color.WHITE); mCanvas.drawRect(0, 0, mPoint.x, mPoint.y, paint); } break; } return true; });
再升級版:LEVER3
筆觸根據繪制的速度動態改變畫筆粗細
float movingX = 0; float movingY = 0; private long lastTimestamp = 0L;//最后一次的時間戳
mIdIvShow.setOnTouchListener((view, event) -> { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: lastTimestamp = System.currentTimeMillis(); downX = event.getX(); downY = event.getY(); break; case MotionEvent.ACTION_CANCEL: break; case MotionEvent.ACTION_MOVE: movingX = event.getX(); movingY = event.getY(); long curTimestamp = System.currentTimeMillis(); //計算時間差 long detaT = curTimestamp - lastTimestamp; //計算距離差 float detaS = Logic.disPos2d(movingX, movingY, downX, downY); //由于速度是 px/ms double v = detaS / detaT; //速度轉化為畫筆寬度的等式 float width = 14/(float)v; L.d(width + L.l()); //限制極值情況 if ((width > 0) && width < 30) { mPaint.setStrokeWidth(width); } mCanvas.drawLine(downX, downY, movingX, movingY, mPaint); mIdIvShow.invalidate(); downX = movingX; downY = movingY; lastTimestamp = curTimestamp;//更新時間 movePos.add(new PointF(event.getX(), event.getY())); break; } return true; });
拓展
1.其中可以改變求寬度的等式實現不同的筆觸:如
float width = (float) Math.log10(v) * 40;
2.在圖片上繪畫
//圖片原型 Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.iv_500x400); //圖片副本 Bitmap mNewBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), bitmap.getConfig()); //用副本生成Canvas mCanvas = new Canvas(mNewBitmap); mPaint = new Paint(Paint.ANTI_ALIAS_FLAG); mPaint.setStrokeCap(Paint.Cap.ROUND);//直線圓頭 mCanvas.drawBitmap(bitmap, new Matrix(), mPaint); mPaint.setStrokeWidth(10); mPaint.setColor(Color.parseColor("#88164BE6")); //設置副本圖片到ImageView mIdIvShow.setImageBitmap(mNewBitmap);
總結
以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對億速云的支持。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。