生活随笔
收集整理的這篇文章主要介紹了
Android画板,橡皮擦为黑色痕迹的问题
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
本人小菜鳥一枚,最近想做畫板(用的瘋狂Android講義中,雙緩沖技術那種),遇到一個橡皮擦為黑色痕跡的問題,網上搜索資料,基礎太差,實在看不懂,于是寫下自己的解決辦法,幫助跟我同樣的小白。
1.畫線改用畫圓
問題2.改用畫圓后出現,手松開后,橡皮擦效果才顯示出來?
? ? ?解決:在onTouchEvent 的MotionEvent.ACTION_MOVE中,每次都將寫到內存中區canvas.drawPath(path, paint); ----------- -----就是每次移動時,都加上與手松開時調用的方法
第一次發,廢話有點多,好吧,上代碼(代碼太爛,輕噴)
?
?
?
public class DrawView extends View{float preX;float preY;private Bitmap cacheBitmap = null;//畫布Canvas canvas = null;//畫筆public Paint paint = null;private Path path = null;//屏幕寬度 private int width;//屏幕高度 private int height;public boolean isRubber = false;public DrawView(Context context, AttributeSet set) {super(context,set);WindowManager wm = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);width = wm.getDefaultDisplay().getWidth();height = wm.getDefaultDisplay().getHeight();cacheBitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);canvas = new Canvas();canvas.drawColor(Color.TRANSPARENT);path = new Path();//給畫布設置bitmapcanvas.setBitmap(cacheBitmap);//初始化畫筆信息setPaint(Color.BLACK,Params.initStrokeWidth); }public void setPaint(int color){isRubber = false;setPaint(color,5);}//初始化畫筆信息public void setPaint(int color,int strokeWidth){System.out.println("===========setPaint==========");paint = new Paint(Paint.DITHER_FLAG);paint.setColor(color);// 設置畫筆風格paint.setStyle(Paint.Style.STROKE);paint.setStrokeWidth(strokeWidth);// 反鋸齒paint.setAntiAlias(true);paint.setDither(true);}//初始化畫筆信息public void setRubber(){isRubber = true;paint = new Paint(Paint.DITHER_FLAG);// 設置畫筆風格paint.setStyle(Paint.Style.STROKE);paint.setStrokeWidth(Params.initStrokeWidth + 5);// 反鋸齒paint.setAntiAlias(true);paint.setDither(true);paint.setXfermode(new PorterDuffXfermode(Mode.DST_OUT));}@Overridepublic boolean onTouchEvent(MotionEvent event) {float x = event.getX();float y = event.getY();switch (event.getAction()) {case MotionEvent.ACTION_DOWN:System.out.println("====MotionEvent.ACTION_DOWN===DOWN");path.moveTo(x, y);preX = x;preY = y;break;case MotionEvent.ACTION_MOVE:System.out.println("====MotionEvent.ACTION_MOVE:===MOVE");path.quadTo(preX, preY, x, y);preX = x;preY = y;if(isRubber){//這樣性能可能會很差,自己想辦法解決canvas.drawPath(path, paint); // 橡皮擦關鍵點================== 看這里 問題2解決============}break;case MotionEvent.ACTION_UP:System.out.println("====MotionEvent.ACTION_UP===UP");canvas.drawPath(path, paint); //用畫筆和路徑畫一張圖片path.reset(); //重置路徑break;}invalidate();//重新繪制 onDrawreturn true;} @Overrideprotected void onDraw(Canvas canvas) {Paint bmpPaint = new Paint();// 將cacheBitmap繪制到該View組件上canvas.drawBitmap(cacheBitmap, 0, 0, bmpPaint);if (isRubber) { //橡皮擦走這邊 ======================看這里 問題一解決======================canvas.drawCircle(preX, preY, 5, paint);} else {canvas.drawPath(path, paint);}}}
?
?
?
?
?
總結
以上是生活随笔為你收集整理的Android画板,橡皮擦为黑色痕迹的问题的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。