Android - Cut a circle from a square Bitmap

前端 未结 3 805
借酒劲吻你
借酒劲吻你 2020-12-08 08:13

I am trying to cut a circle from a square bitmap using following code

        Canvas canvas=new Canvas(bitmapimg );
        int circleXCoord = bitmapimg .get         


        
相关标签:
3条回答
  • 2020-12-08 08:51

    Seems like PorterDuff.Mode.Clear did not work for gingerbread

    Solved the problem (cut circle from square using this code)

     public Bitmap BitmapCircularCroper(Bitmap bitmapimg){
        Bitmap output = Bitmap.createBitmap(bitmapimg.getWidth(),
                bitmapimg.getHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(output);
    
        final int color = 0xff424242;
        final Paint paint = new Paint();
        final Rect rect = new Rect(0, 0, bitmapimg.getWidth(),
                bitmapimg.getHeight());
    
        paint.setAntiAlias(true);
        canvas.drawARGB(0, 0, 0, 0);
        paint.setColor(color);
        canvas.drawCircle(bitmapimg.getWidth() / 2,
                bitmapimg.getHeight() / 2, bitmapimg.getWidth() / 2, paint);
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
        canvas.drawBitmap(bitmapimg, rect, rect, paint);
        return output;
    
    }
    
    0 讨论(0)
  • 2020-12-08 08:56

    For anyone that's still looking at this, this answer will cause a few issues.

    1.) The canvas that you are creating in this instance will not have hardware acceleration. Even though your Paint object has anti-aliasing, the canvas will not. This will cause artifacting when you decide to paint this back to your original canvas in your onDraw() call.

    2.) This takes a lot more resources. You have to create a second Bitmap (which can cause OOM), and a secondary Canvas as well as all of the different alterations you have to do.

    Please check out Romain Guy's answer. You create a BitmapShader and then create a RoundRect that gives you a Circle. You just need to know the dimensions of your RectF so that it can determine the circle properly.

    This means that if you know the center point (x, y) and radius, you can easily determine the RectF.

    left = x - radius;
    top = y - radius;
    right = x + radius;
    bottom = y + radius;
    

    This also means that with this solution posted below you only have to draw to the screen once, everything else is done in the off-screen buffer.

    http://www.curious-creature.com/2012/12/11/android-recipe-1-image-with-rounded-corners/

    The best solution is found here:

    BitmapShader shader;
    shader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
    
    Paint paint = new Paint();
    paint.setAntiAlias(true);
    paint.setShader(shader);
    
    RectF rect = new RectF(0.0f, 0.0f, width, height);
    
    // rect contains the bounds of the shape
    // radius is the radius in pixels of the rounded corners
    // paint contains the shader that will texture the shape
    canvas.drawRoundRect(rect, radius, radius, paint);
    
    0 讨论(0)
  • 2020-12-08 09:00
    import android.graphics.PorterDuff.Mode;
    import android.graphics.Bitmap.Config;
    
    public static Bitmap getCircularBitmap(Bitmap bitmap) 
    {
        Bitmap output;
    
        if (bitmap.getWidth() > bitmap.getHeight()) {
            output = Bitmap.createBitmap(bitmap.getHeight(), bitmap.getHeight(), Config.ARGB_8888);
        } else {
            output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getWidth(), Config.ARGB_8888);
        }
    
        Canvas canvas = new Canvas(output);
    
        final int color = 0xff424242;
        final Paint paint = new Paint();
        final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
    
        float r = 0;
    
        if (bitmap.getWidth() > bitmap.getHeight()) {
            r = bitmap.getHeight() / 2;
        } else {
            r = bitmap.getWidth() / 2;
        }
    
        paint.setAntiAlias(true);
        canvas.drawARGB(0, 0, 0, 0);
        paint.setColor(color);
        canvas.drawCircle(r, r, r, paint);
        paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
        canvas.drawBitmap(bitmap, rect, rect, paint);
    
        return output;
    }
    
    0 讨论(0)
提交回复
热议问题