create Circle Bitmap - Android Graphics

Android examples for Graphics:Bitmap Create

Description

create Circle Bitmap

Demo Code


//package com.java2s;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;

public class Main {
    public static Bitmap createCircleBitmap(Context context,
            byte[] picture, int sizeDp) {
        int size = (int) (sizeDp * context.getResources()
                .getDisplayMetrics().density);
        Bitmap pictureBitmap = Bitmap.createBitmap(size, size,
                Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(pictureBitmap);
        Paint paint = new Paint();
        paint.setAntiAlias(true);/*from   www  . j a  v  a2  s  .c  om*/
        float halfSize = size / 2.0f;
        canvas.drawCircle(halfSize, halfSize, halfSize, paint);
        Rect rect = new Rect(0, 0, size, size);
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
        canvas.drawBitmap(
                BitmapFactory.decodeByteArray(picture, 0, picture.length),
                rect, rect, paint);
        return pictureBitmap;
    }
}

Related Tutorials