Android中的圆形图像

时间:2015-11-17 06:40:06

标签: java android xml

我正在尝试在Andrdoid中添加圆形图像。我跟着this去做了。

这是我的代码。

<ImageView
        android:id="@+id/profPic"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true"
        android:src="@drawable/ic_prof"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        />

这是我的MainActivity.java

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_extract);

        imageView=(ImageView)findViewById(R.id.profPic);
        Bitmap bitmap= BitmapFactory.decodeResource(getResources(), R.drawable.ic_prof);
        imageView.setImageBitmap(getCircleBitmap(bitmap));

}

//Circle Image
    public static Bitmap getCircleBitmap(Bitmap bitmap) {

        Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
                bitmap.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, bitmap.getWidth(), bitmap.getHeight());
        final RectF rectF = new RectF(rect);
        final float roundPx = 12;

        paint.setAntiAlias(true);
        canvas.drawARGB(0, 0, 0, 0);
        paint.setColor(color);
        canvas.drawRoundRect(rectF, roundPx, roundPx, paint);

        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
        canvas.drawBitmap(bitmap, rect, rect, paint);

        return output;
    }

但是,我无法创建圆形图像。它仍然显示一个矩形1。

提前致谢。

0 个答案:

没有答案