Bor*_*ora 47 android bitmap rounded-corners imageview
我有一个ImageView,我想用它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>
Run Code Online (Sandbox Code Playgroud)
并将此代码设置为我的imageview的背景.它可以工作,但我放在它上面的src图像ImageView是走出边界而不适应新的形状.
我该如何解决这个问题?
小智 82
试试这个:
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);
}
}
Run Code Online (Sandbox Code Playgroud)
和
<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);
Run Code Online (Sandbox Code Playgroud)
要么,
ImageView iv= new CustomImageView(this);
iv.setImageResource(R.drawable.pic);
Run Code Online (Sandbox Code Playgroud)
lob*_*zik 27
奇怪的是,这里没有人提到Android支持库v4 中的RoundedBitmapDrawable.对我来说,这是获得无边框圆角的最简单方法.以下是用法示例:
RoundedBitmapDrawable roundedBitmapDrawable = RoundedBitmapDrawableFactory.create(getResources(), bitmap);
final float roundPx = (float) bitmap.getWidth() * 0.06f;
roundedBitmapDrawable.setCornerRadius(roundPx);
Run Code Online (Sandbox Code Playgroud)
Ket*_*hir 19
使用canvas创建一个舍入到位图的函数.
public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, int pixels) {
Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap
.getHeight(), 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 = pixels;
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);
return output;
}
Run Code Online (Sandbox Code Playgroud)
了解更多信息:> 这里
接受的答案使用路径剪辑,但它不支持抗锯齿.请参阅Romain Guy对其帖子的评论."路径裁剪不支持抗锯齿,你会得到锯齿状的边缘."
http://www.curious-creature.com/2012/12/11/android-recipe-1-image-with-rounded-corners/
有一个很好的库(vinc3m1的RoundedImageView)在ImageView上支持圆角,但它只支持每个角上相同的半径.所以我做了一个你可以在每个角上设置不同的半径.
它不依赖于路径裁剪,也不依赖于重绘.它只用canvas.drawPath()方法绘制一次.所以我终于得到了我想要的结果如下.

请参阅:https://github.com/pungrue26/SelectableRoundedImageView
对我来说,下面的方法很神奇。:) 此方法接受一个位图对象并将其返回为圆角。roundPx是您想要的舍入像素数:
public static Bitmap getRoundedCornerBitmap(Bitmap bitmap) {
Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
bitmap.getHeight(), 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(Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);
return output;
}
Run Code Online (Sandbox Code Playgroud)
...或者您可以使用此库代替ImageView,而无需任何进一步的编码。
如果您需要制作具有不同圆角半径的位图,我建议您遵循以下代码:
private static Bitmap createRoundedRectBitmap(@NonNull Bitmap bitmap,
float topLeftCorner, float topRightCorner,
float bottomRightCorner, float bottomLeftCorner) {
Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(),
Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(output);
final int color = Color.WHITE;
final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
final RectF rectF = new RectF(rect);
Path path = new Path();
float[] radii = new float[]{
topLeftCorner, bottomLeftCorner,
topRightCorner, topRightCorner,
bottomRightCorner, bottomRightCorner,
bottomLeftCorner, bottomLeftCorner
};
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
path.addRoundRect(rectF, radii, Path.Direction.CW);
canvas.drawPath(path, paint);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);
return output;
}
Run Code Online (Sandbox Code Playgroud)
如果您还需要边框,则:1.您可以使用带有透明主体和外部白色的圆形框图像.例如:
并将其与目标图像一起使用,如下所示:
<FrameLayout
android:layout_width="100px"
android:layout_height="100px" >
<ImageView
android:id="@+id/targetImage"
android:layout_width="100px"
android:layout_height="100px"
android:src="@drawable/app_icon"
android:layout_gravity="center" />
<ImageView
android:id="@+id/boxImage"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="fitXY"
android:src="@drawable/box" />
Run Code Online (Sandbox Code Playgroud)
ImageView也是一个很好的解决方案.| 归档时间: |
|
| 查看次数: |
46183 次 |
| 最近记录: |