我正在使用此代码来获取位图的顶部圆角
public static void setTopRounded(Bitmap workingBitmap , int w, int h, ImageView v,Context context)
{
Bitmap bmp = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(bmp);
Shader shader = new BitmapShader(workingBitmap, Shader.TileMode.MIRROR,
Shader.TileMode.MIRROR);
Paint paint = new Paint(Paint.FILTER_BITMAP_FLAG);
paint.setAntiAlias(true);
paint.setShader(shader);
RectF rec = new RectF(0, 0, w, h - 20);
c.drawRect(new RectF(0, 20, w, h), paint);
c.drawRoundRect(rec, 10, 10, paint);
v.setBackgroundDrawable(new BitmapDrawable(context.getResources(), bmp));
}
我的图像视图的XML代码是
<ImageView
android:layout_height="140dp"
android:layout_width="match_parent"
android:id="@+id/profileIV"
android:visibility="gone"
android:scaleType="centerCrop" />
现在的问题是设置
v.setBackgroundDrawable(new BitmapDrawable(context.getResources(), bmp))
,
android:scaleType="centerCrop"
属性不起作用,如果我使用
v.setImageDrawable(new BitmapDrawable(context.getResources(), bmp))
然后缩放类型可以工作,但图像不会被舍入。 我做错了什么?
答案 0 :(得分:0)
尝试使用v.setImageBitmap(bmp)
答案 1 :(得分:0)
/**
* This method used for bitmap compress and rounded corner
* @param Bitmap object
* @param pixels
* @return nothing
*/
public static Bitmap getRoundedRectBitmap(final Bitmap bitmap,final int pixels)
{
int color;
float roundPx;
final Bitmap result = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
final Canvas canvas = new Canvas(result);
color = 0xff424242;
final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
final RectF rectF = new RectF(rect);
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 result;
}
//像这样调用上面的方法
profileImage.setImageBitmap(getRoundedRectBitmap(bitmap,50));
答案 2 :(得分:0)
我找到了答案。我没有改变任何事情。对顶部圆角图像使用相同的功能,并在图像视图中使用位图作为背景。但现在使用ThumbnailUtils
在imageView的代码中进行CenterCrop首先我使用下面的函数来获得缩放的CenterCroped图像
Bitmap resizedProfileBitmap = ThumbnailUtils.extractThumbnail(profileBitmap, voucherTitleRLT.getWidth(), height);
然后使用顶部圆角函数获取顶部圆角中心的图像作为imageview的背景,如下所示
setTopRounded(resizedProfileBitmap, resizedProfileBitmap.getWidth(), resizedProfileBitmap.getHeight(), profileIV, thisContext);
答案 3 :(得分:0)
您可以通过创建Shape Drawable并将其设置为ImageView的背景,以XML格式执行此操作。本文介绍了如何:http://www.techrepublic.com/article/pro-tip-round-corners-on-an-android-imageview-with-this-hack/#
你的形状可以看起来像这样:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#00ffffff" />
<padding
android:left="1dp"
android:top="1dp"
android:right="1dp"
android:bottom="1dp" />
<corners
android:topLeftRadius="6dp"
android:topRightRadius="6dp"
android:bottomLeftRadius="0.1dp"
android:bottomRightRadius="0.1dp" />
<stroke
android:width="6dp"
android:color="#ffffffff" />