日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

Android画板,橡皮擦为黑色痕迹的问题

發布時間:2024/3/12 Android 50 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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画板,橡皮擦为黑色痕迹的问题的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。