圆形ImageView背景实际上是正方形而不是圆形?

时间:2016-09-14 07:24:39

标签: android bitmap imageview

我对循环ImageView有疑问。当我尝试设置ImageView的背景颜色时,它实际上最终变成了正方形,而不是圆形,实际上我希望ImageView是一个简单的圆形ImageView一些文字作为图像。这是我的尝试。

ImageView imageView = postViewHolder.fourthCommenter;
Bitmap b=Bitmap.createBitmap(30, 30, Bitmap.Config.ARGB_8888);
BitmapShader shader = new BitmapShader(b,  Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
Paint paint = new Paint();
paint.setShader(shader);
paint.setAntiAlias(true);
Canvas c = new Canvas(b);
c.drawCircle(b.getWidth()/2, b.getHeight()/2, b.getWidth()/2, paint);
c.drawText("+10",30,30,paint);
imageView.setBackgroundColor(ContextCompat.getColor(context, R.color.material_color_grey_200));
imageView.setImageBitmap(b);

我想要的只是让图像显示一个灰色圆形Imageview,其中包含“+10”字样。显示的是一个简单的方格Imageview,其中没有文字。我看不出我做错了什么。

任何帮助都会得到帮助。

谢谢!

编辑:这是我想要的一个例子。我想要的图像看起来与此照片上显示的+16图像完全相同:

enter image description here

2 个答案:

答案 0 :(得分:0)

不确定这是否是正确的方法,但它应该适合你的情况:

首先使用它来创建具有所需圆形的位图:

public Bitmap getCircledBmp(Context mContext,Bitmap bmp){

        Canvas canvas = new Canvas(bmp);
        int color = ContextCompat.getColor(mContext, R.color.material_color_grey_200);

        Paint paint = new Paint();
        Rect rect = new Rect(0, 0, bmp.getWidth(), bmp.getHeight());
        RectF rectFloat = new RectF(rect);

        paint.setAntiAlias(true);
        paint.setColor(color);


        canvas.drawARGB(0, 0, 0, 0);
        canvas.drawOval(rectFloat, paint);

        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));

        canvas.drawBitmap(bmp, rect, rect, paint);

        return bmp;

    }

然后添加您想要的文字:

public Bitmap drawText(String text, Bitmap bmp, int textSize) {

    //text dimensions

    TextPaint textPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
    textPaint.setTextSize(textSize);
    textPaint.setColor(Color.BLACK);
    textPaint.setTextAlign(Paint.Align.CENTER);

    StaticLayout mTextLayout = new StaticLayout(text, textPaint,
            bmp.getWidth(), Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false);

    Canvas canvas = new Canvas(bmp);

    //draw text
    canvas.save();
    canvas.translate((canvas.getWidth() / 2), (canvas.getHeight() / 2) -
            ((mTextLayout.getHeight() / 2)));
    mTextLayout.draw(canvas);
    canvas.restore();

    return bmp;
}

最后你应该得到你想要的图像:

Bitmap bmp = Bitmap.createBitmap(100,100,Bitmap.Config.ARGB_8888); //change the values whatever you like
ImageView imageView = postViewHolder.fourthCommenter;
imageview.setImageBitmap(drawText("+10",getCircledBmp(this,bmp),30));

答案 1 :(得分:0)

  

我想要的只是让图像显示灰色圆形Imageview

试试此代码

Bitmap picture = BitmapFactory.decodeResource(getResources(), R.mipmap.add_image);

ImageView imageView = (ImageView) findViewById(R.id.imgProfilePicture);
imageView.setImageBitmap(getRoundedBitmap(picture));

 public Bitmap getRoundedBitmap(Bitmap bitmap){
        Bitmap circleBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
        BitmapShader shader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
        Paint paint = new Paint();
        paint.setShader(shader);
        paint.setAntiAlias(true);
        Canvas c = new Canvas(circleBitmap);
        c.drawCircle(bitmap.getWidth() / 2, bitmap.getHeight() / 2, bitmap.getWidth() / 2, paint);
        return circleBitmap;
    }

您的xml文件

  <de.hdodenhof.circleimageview.CircleImageView
        android:id="@+id/imgProfilePicture"
        android:layout_width="110dp"
        android:layout_height="110dp"
        android:layout_marginBottom="20dp"
        app:civ_border_width="3dp"
        app:civ_border_color="@color/light_gray" />

并在 build.gradle

中添加此内容
 compile 'de.hdodenhof:circleimageview:2.1.0'

Cirular ImageView完成!

enter image description here