如何计算在Android的位图的擦除区的百分比是多少? [英] How to calculate the percentage of erased area of a bitmap in android?

查看:124
本文介绍了如何计算在Android的位图的擦除区的百分比是多少?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的机器人。我做的可擦除用手指帆布位图的应用程序。像手指画橡皮擦。我想计算擦除区的百分比(例如,60%的已被擦除从完整图像)。请帮我做这件事..在此先感谢。

我尝试了一些方法。它总是给我0%。它不工作。见的code的底部的方法。

自定义视图

 公共类MyView的扩展视图
{
    私人最终涂料mPaint;
    私人位图mBitmap;
    私人帆布mCanvas;
    私人最终路径的mpath;
    私人最终涂料mBitmapPaint;
    私人位图eraseableBitmap;

    公共MyView的(上下文的背景下)
    {
        超(上下文);
        如果(Build.VERSION.SDK_INT> = 11)
        {
            setLayerType(View.LAYER_TYPE_SOFTWARE,NULL);
        }
        mPaint =新的油漆();
        的mpath =新路径();
        mBitmapPaint =新的油漆(Paint.DITHER_FLAG);
    }

    保护无效PaintObjectInit()
    {
        mPaint.setAntiAlias​​(真正的);
        mPaint.setDither(真正的);
        mPaint.setXfermode(新PorterDuffXfermode(PorterDuff.Mode.CLEAR));
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeJoin(Paint.Join.ROUND);
        mPaint.setStrokeCap​​(Paint.Cap.ROUND);
        mPaint.setStrokeWidth(30);
    }

    @覆盖
    保护无效onSizeChanged(INT W,INT小时,INT oldw,INT oldh)
    {
        super.onSizeChanged(W,H,oldw,oldh);

        尝试
        {
            // mBitmap = Bitmap.createBitmap(W,H,Bitmap.Config.ARGB_8888);
            eraseableBitmap =
                BitmapFactory.de codeResource(this.getResources(),R.drawable.tharu_rena_over).copy(
                    Bitmap.Config.ARGB_8888,真正的);

            eraseableBitmap = getResizedBitmap(eraseableBitmap中,h,w)的;

            mCanvas =新的Canvas(eraseableBitmap);
        }
        赶上(例外五)
        {
            // TODO:处理异常
            e.printStackTrace();
        }
    }

    公共位图getResizedBitmap(位图BM,诠释newHeight,INT newWidth)
    {
        INT宽度= bm.getWidth();
        INT高= bm.getHeight();
        浮动scaleWidth =((浮点)newWidth)/宽度;
        浮动scaleHeight =((浮点)newHeight)/身高;
        //创建用于操纵矩阵
        字模=新的Matrix();
        //调整位图
        matrix.postScale(scaleWidth,scaleHeight);

        //重建新位图
        位图resizedBitmap = Bitmap.createBitmap(宽多重峰,0,0,宽度,高度,矩阵,假);
        返回resizedBitmap;
    }

    @覆盖
    保护无效的OnDraw(帆布油画)
    {

        //canvas.drawBitmap(mBitmap,0,0,mBitmapPaint);
        canvas.drawBitmap(eraseableBitmap,0,0,mBitmapPaint);
        canvas.drawPath(的mpath,mPaint);
    }

    私人浮动MX,我的;
    私有静态最终浮动TOUCH_TOLERANCE = 4;

    私人无效touch_start(浮X,浮动Y)
    {
        // mPath.reset();
        mPath.moveTo(X,Y);
        MX = X;
        我= Y;
    }

    私人无效TOUCH_MOVE(浮X,浮动Y)
    {
        浮DX = Math.abs(X  -  MX);
        浮DY = Math.abs(Y  - 我的);
        如果(DX> = TOUCH_TOLERANCE || DY> = TOUCH_TOLERANCE)
        {
            mPath.quadTo(MX,MY,(X + MX)/ 2,(Y +我)/ 2);
            MX = X;
            我= Y;
        }
    }

    私人无效touch_up()
    {
        mPath.lineTo(MX,MY);
        //提交路径,我们的屏幕外
        mCanvas.drawPath(的mpath,mPaint);
        //杀了这个,所以我们不重复抽奖
        Toast.makeText(的getContext(),删除+ percentTransparent(eraseableBitmap,10),Toast.LENGTH_SHORT).show();
    }

    @覆盖
    公共布尔的onTouchEvent(MotionEvent事件)
    {
        浮X = event.getX();
        浮动Y = event.getY();

        开关(event.getAction())
        {
            案例MotionEvent.ACTION_DOWN:
                touch_start(X,Y​​);
                无效();
                打破;
            案例MotionEvent.ACTION_MOVE:
                TOUCH_MOVE(X,Y);
                无效();
                打破;
            案例MotionEvent.ACTION_UP:
                润色();
                无效();
                打破;
        }
        返回true;
    }

    静态的公众持股量percentTransparent(位图BM,int标)
    {

        最终诠释宽度= bm.getWidth();
        最终诠释身高= bm.getHeight();

        //样本矩形的大小
        最终诠释XSTEP =宽度/规模;
        最终诠释yStep =身高/规模;

        //第一个矩形的中心
        最终诠释xInit = XSTEP / 2;
        最终诠释yInit = yStep / 2;

        //最后一个矩形的中心
        最终诠释XEND =宽度 - 特步/ 2;
        最终诠释YEND =身高 -  yStep / 2;

        INT totalTransparent = 0;

        对于(INT X = xInit,X< = XEND; X + = XSTEP)
        {
            对于(INT Y = yInit; Y< = YEND; Y + = yStep)
            {
                如果(bm.getPixel(X,Y)== Color.TRANSPARENT)
                {
                    totalTransparent ++;
                }
            }
        }
        返程((浮点)totalTransparent)/(刻度*刻度);

    }

}
 

在活动课的onCreate

 尝试
{
    MyView的MyView的=新的MyView的(这一点);
    myView.requestFocus();
    myView.PaintObjectInit();
    //的setContentView(MyView的);

    LinearLayout中上=(的LinearLayout)findViewById(R.id.LinearLayout01);
    upper.addView(MyView的);
}
赶上(例外五)
{
    // TODO:处理异常
    e.printStackTrace();
}
 

解决方案

现在的问题是你不能打电话给你的 PaintObjectInit()的方法,所以你画了默认的油漆,所以画中 Col​​or.BLACK 而不是 Col​​or.TRANSPARENT 。调用添加到 PaintObjectInit()你构造的底部,它应该工作。

此外,下面创建一个不可变的位图!请参阅<一href="http://developer.android.com/reference/android/graphics/Bitmap.html#createScaledBitmap%28android.graphics.Bitmap,%20int,%20int,%20boolean%29"相对=nofollow> createBitmap 。所以,你的位图是永远不会被修改。你被绘制的路径以及该位图隐藏这个事实在用户界面上。

  //RECREATE新位图
位图resizedBitmap = Bitmap.createBitmap(宽多重峰,0,0,宽度,高度,矩阵,假);
返回resizedBitmap;
 

试着做一个可变的位图这样的 -

  //RECREATE新位图
        位图resizedBitmap = Bitmap.createBitmap(newWidth,newHeight,Bitmap.Config.ARGB_8888);
        帆布C =新的Canvas(resizedBitmap);
        c.setMatrix(矩阵);
        c.drawBitmap(BM,矩阵,NULL);
        返回resizedBitmap;
 

I'm new to android. I'm doing an application which can erase the bitmap on canvas using finger. Something like finger paint eraser. I want to calculate the percentage of erased area (eg. 60% has been erased from complete image). Please help me to do this.. Thanks in advance..

I tried some method. It always give me 0%. Its not working. See the bottom of the code for that method..

Customized View

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);

    }

}

Inside Activity Class 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();
}

解决方案

The problem is you are failing to call your PaintObjectInit() method, so you are painting with a default paint, so painting in Color.BLACK instead of Color.TRANSPARENT. Add a call to PaintObjectInit() at the bottom of you constructor and it should work.

Also, the following creates an immutable bitmap! See createBitmap. So, your bitmap is never being modified. You are hiding this fact on the user interface by drawing the path as well as the bitmap.

// "RECREATE" THE NEW BITMAP
Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, false);
return resizedBitmap;

Try making a mutable bitmap like this --

// "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, matrix, null);
        return resizedBitmap;

这篇关于如何计算在Android的位图的擦除区的百分比是多少?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆