android区域统计,如何计算android中位图的擦除区域的百分比?

这篇博客介绍了一个Android新手如何创建一个使用手指作为橡皮擦来擦除画布上位图的应用。作者提供了一个自定义视图类`MyView`,并实现了橡皮擦功能。当用户触摸屏幕移动手指时,会擦除位图的一部分,并计算擦除区域的百分比。然而,目前的代码存在一个问题,总是返回0%的擦除百分比。作者希望找到解决方案来正确计算擦除区域的比例。

我是

android的新手.我正在做一个可以用手指擦除画布上的位图的应用程序.像手指画橡皮擦的东西.我想计算擦除区域的百分比(例如,60%已从完整图像中删除).请帮我这样做..提前致谢..

我尝试了一些方法.它总是给我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, h, Bitmap.Config.ARGB_8888);

eraseableBitmap =

BitmapFactory.decodeResource(this.getResources(), R.drawable.tharu_rena_over).copy(

Bitmap.Config.ARGB_8888, true);

eraseableBitmap = getResizedBitmap(eraseableBitmap, h, 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, 0, 0, width, height, matrix, false);

return resizedBitmap;

}

@Override

protected void onDraw(Canvas canvas)

{

//canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);

canvas.drawBitmap(eraseableBitmap, 0, 0, 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();

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值