draw Circle on Bitmap - Android android.graphics

Android examples for android.graphics:Bitmap Operation

Description

draw Circle on Bitmap

Demo Code


//package com.java2s;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PaintFlagsDrawFilter;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;

import android.graphics.RectF;

public class Main {

    private static Bitmap drawCircle(Bitmap bitmap) {
        int size = Math.min(bitmap.getWidth(), bitmap.getHeight());

        Bitmap localBitmap = Bitmap.createBitmap(size, size,
                Bitmap.Config.ARGB_8888);
        Canvas localCanvas = new Canvas(localBitmap);
        localCanvas.setDrawFilter(new PaintFlagsDrawFilter(0,
                Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG));
        Paint paint = new Paint();
        paint.setAntiAlias(true);/*  www .  ja  v  a 2 s  . com*/

        int left = bitmap.getWidth() / 2 - size / 2;
        int top = bitmap.getHeight() / 2 - size / 2;

        RectF rectF = new RectF(0, 0, size, size);
        localCanvas.drawOval(rectF, paint);
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
        localCanvas.drawBitmap(bitmap, -left, -top, paint);
        paint.setXfermode(null);

        return localBitmap;
    }
}

Related Tutorials