
我尝试了一些方法.它总是给我0%.它不起作用.请参阅该方法的代码底部..
自定义视图
public class MyVIEw extends VIEw{ private final Paint mPaint; private Bitmap mBitmap; private Canvas mCanvas; private final Path mPath; private final Paint mBitmapPaint; private Bitmap eraseableBitmap; public MyVIEw(Context context) { super(context); if (Build.VERSION.SDK_INT >= 11) { setLayerType(VIEw.LAYER_TYPE_SOFTWARE,null); } mPaint = new Paint(); mPath = new Path(); mBitmapPaint = new Paint(Paint.DITHER_FLAG); } protected voID PaintObjectinit() { mPaint.setAntiAlias(true); mPaint.setDither(true); mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR)); mPaint.setStyle(Paint.Style.stroke); mPaint.setstrokeJoin(Paint.Join.ROUND); mPaint.setstrokeCap(Paint.Cap.ROUND); mPaint.setstrokeWIDth(30); } @OverrIDe protected voID onSizeChanged(int w,int h,int olDW,int oldh) { super.onSizeChanged(w,h,olDW,oldh); try { //mBitmap = Bitmap.createBitmap(w,Bitmap.Config.ARGB_8888); eraseableBitmap = BitmapFactory.decodeResource(this.getResources(),R.drawable.tharu_rena_over).copy( Bitmap.Config.ARGB_8888,true); eraseableBitmap = getResizedBitmap(eraseableBitmap,w); mCanvas = new Canvas(eraseableBitmap ); } catch (Exception e) { // Todo: handle exception e.printstacktrace(); } } public Bitmap getResizedBitmap(Bitmap bm,int newHeight,int newWIDth) { int wIDth = bm.getWIDth(); int height = bm.getHeight(); float scaleWIDth = ((float) newWIDth) / wIDth; float scaleHeight = ((float) newHeight) / height; // CREATE A MATRIX FOR THE MANIPulATION Matrix matrix = new Matrix(); // RESIZE THE BIT MAP matrix.postscale(scaleWIDth,scaleHeight); // "RECREATE" THE NEW BITMAP Bitmap resizedBitmap = Bitmap.createBitmap(bm,wIDth,height,matrix,false); return resizedBitmap; } @OverrIDe protected voID onDraw(Canvas canvas) { //canvas.drawBitmap(mBitmap,mBitmapPaint); canvas.drawBitmap(eraseableBitmap,mBitmapPaint); canvas.drawPath(mPath,mPaint); } private float mX,mY; private static final float touch_TolERANCE = 4; 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); // commit the path to our offscreen mCanvas.drawPath(mPath,mPaint); // kill this so we don't double draw Toast.makeText(getContext(),"Deleted: " + percenttransparent(eraseableBitmap,10),Toast.LENGTH_SHORT).show(); } @OverrIDe public boolean ontouchEvent(MotionEvent event) { float x = event.getX(); float y = event.getY(); switch (event.getAction()) { case MotionEvent.ACTION_DOWN: touch_start(x,y); invalIDate(); break; case MotionEvent.ACTION_MOVE: touch_move(x,y); invalIDate(); break; case MotionEvent.ACTION_UP: touch_up(); invalIDate(); break; } return true; } static public float percenttransparent(Bitmap bm,int scale) { final int wIDth = bm.getWIDth(); final int height = bm.getHeight(); // size of sample rectangles final int xStep = wIDth / scale; final int yStep = height / scale; // center of the first rectangle final int xInit = xStep / 2; final int yInit = yStep / 2; // center of the last rectangle final int xEnd = wIDth - xStep / 2; final int yEnd = height - yStep / 2; int totaltransparent = 0; for (int x = xInit; x <= xEnd; x += xStep) { for (int y = yInit; y <= yEnd; y += yStep) { if (bm.getPixel(x,y) == color.transparent) { totaltransparent++; } } } return ((float) totaltransparent) / (scale * scale); }} 内部活动类onCreate
try{ MyVIEw myVIEw = new MyVIEw(this); myVIEw.requestFocus(); myVIEw.PaintObjectinit(); // setContentVIEw(myVIEw); linearLayout upper = (linearLayout) findVIEwByID(R.ID.linearLayout01); upper.addVIEw(myVIEw);}catch (Exception e){ // Todo: handle exception e.printstacktrace();}解决方法 问题是您无法调用PaintObjectinit()方法,因此您使用默认绘画进行绘制,因此使用color.BLACK而不是color.transparent进行绘制.在构造函数的底部添加对PaintObjectinit()的调用,它应该可以工作. 此外,以下创建一个不可变的位图!请参见createBitmap.因此,您的位图永远不会被修改.您通过绘制路径和位图将这一事实隐藏在用户界面上.
// "RECREATE" THE NEW BITMAPBitmap resizedBitmap = Bitmap.createBitmap(bm,false);return resizedBitmap;
尝试制作一个像这样的可变位图 –
// "RECREATE" THE NEW BITMAP Bitmap resizedBitmap = Bitmap.createBitmap(newWIDth,newHeight,Bitmap.Config.ARGB_8888); Canvas c = new Canvas(resizedBitmap); c.setMatrix(matrix); c.drawBitmap(bm,null); return resizedBitmap;总结
以上是内存溢出为你收集整理的如何计算android中位图的擦除区域的百分比?全部内容,希望文章能够帮你解决如何计算android中位图的擦除区域的百分比?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)