如何将图像置于圆形图像视图中?

时间:2015-08-13 16:35:59

标签: android image imageview

我有一个ImageView。我已经做了一些操作来使它循环。一切都很好。但是,我意识到图像不是居中的。显然,图像位于ImageView的左上角。如何使此图像以圆形ImageView为中心?

这是我的代码:

// Decode the Byte[] into bitmap
Bitmap bmp = BitmapFactory.decodeByteArray(data, 0, data.length);
// Set the Bitmap into the imageView
mImage.setImageBitmap(bmp);

//circle img
int wbmp = bmp.getWidth();
int hbmp = bmp.getHeight();
int diameter;
if (wbmp > hbmp) {
   diameter = hbmp;
} else {
   diameter = wbmp;
}

Bitmap resized = Bitmap.createScaledBitmap(bmp, wbmp, hbmp, true);
Bitmap conv_bm = ImageHelper.getRoundedRectBitmap(resized, diameter);

mImage.setImageBitmap(conv_bm);
// TODO Auto-generated method stub
//circle img ends

这是ImageHelper:

public class ImageHelper {

//circle image
public static Bitmap getRoundedRectBitmap(Bitmap bitmap, int radius) {
    Bitmap result = null;
    try {

        Bitmap sbitmap;
        if(bitmap.getWidth() != radius || bitmap.getHeight() != radius) sbitmap = Bitmap.createScaledBitmap(bitmap, radius, radius,false);
        else sbitmap = bitmap;

        result = Bitmap.createBitmap(sbitmap.getWidth(), sbitmap.getHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(result);

        int color = 0xff424242;
        Paint paint = new Paint();
        Rect rect = new Rect(0, 0, sbitmap.getWidth(), sbitmap.getHeight());

        paint.setAntiAlias(true);
        canvas.drawARGB(0, 0, 0, 0);
        paint.setColor(color);
        canvas.drawCircle(sbitmap.getWidth() / 2, sbitmap.getHeight() / 2,
                sbitmap.getWidth() / 2, paint);
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
        canvas.drawBitmap(bitmap, rect, rect, paint);

    } catch (NullPointerException e) {
    } catch (OutOfMemoryError o) {
    }
    return result;
}
//circle image ends

}

0 个答案:

没有答案