ImageView 中带圆角的位图 [英] Bitmap in ImageView with rounded corners
本文介绍了ImageView 中带圆角的位图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个 ImageView,我想用圆角
制作它.
I have an ImageView and I want to make it with rounded corners
.
我用这个:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<solid android:color="@null"/>
<stroke android:width="1dp"
android:color="#ff000000"/>
<corners android:radius="62px"/>
</shape>
并将此代码设置为我的图像视图的背景.它可以工作,但是我放在 ImageView
上的 src 图像超出了边界,并且不能适应新的形状.
And set this code as background of my imageview.
It works, but the src image that I put on the ImageView
is going out of the borders and doesn't adapt itself into the new shape.
我该如何解决问题?
推荐答案
试试这个:
public class CustomImageView extends ImageView {
public static float radius = 18.0f;
public CustomImageView(Context context) {
super(context);
}
public CustomImageView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomImageView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
protected void onDraw(Canvas canvas) {
//float radius = 36.0f;
Path clipPath = new Path();
RectF rect = new RectF(0, 0, this.getWidth(), this.getHeight());
clipPath.addRoundRect(rect, radius, radius, Path.Direction.CW);
canvas.clipPath(clipPath);
super.onDraw(canvas);
}
}
和
<your.pack.name.CustomImageView
android:id="@+id/selectIcon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:scaleType="centerCrop" />
CustomImageView iconImage = (CustomImageView )findViewById(R.id.selectIcon);
iconImage.setImageBitmap(bitmap);
或者,
ImageView iv= new CustomImageView(this);
iv.setImageResource(R.drawable.pic);
这篇关于ImageView 中带圆角的位图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文