Rounded Corner代码适用于Volley NetworkImageView和资源。但它不适用于ApplicationContext的Bitmap。以下是代码。
public class ImageViewUtils {
public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, int pixels) {
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);
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
canvas.drawRoundRect(rectF, pixels, pixels, paint);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);
return output;
}
}
public class Fragment{
......
imageView.setImageBitmap(ImageViewUtils.getRoundedCornerBitmap(((AppContext) getActivity().getApplicationContext()).getUserPhoto(), 10); // The rounded corner doesn't work.
imageView.setImageBitmap(ImageViewUtils.getRoundedCornerBitmap(BitmapFactory.decodeResource(getActivity().getResources(), R.drawable.ic_avatar), 10); // The rounded corner works.
......
}
public class AppContext extends Application {
......
// Get the Bitmap of a photo.
public Bitmap getUserPhoto(){
FileInputStream in = null;
try {
in = openFileInput(KEY_USER_PHOTO_FILE_NAME);
return BitmapFactory.decodeStream(in);
} catch (IOException e) {
Log.d(TAG, e.toString());
return BitmapFactory.decodeResource(getResources(),R.drawable.ball);
} finally {
if(in != null) try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
......
}
以下是此ImageView的布局
<ImageView
android:id="@+id/logo"
android:src="@drawable/ball"
android:contentDescription="@string/title_activity_profile"
android:padding="4dp"
android:layout_height="@dimen/touchable_height_15"
android:layout_width="@dimen/touchable_width_15"
android:scaleType="fitXY" />
奇怪的是,它适用于资源,但它不适用于Bitmap对象。这困扰了我4个小时。有人可以解释一下吗?
答案 0 :(得分:0)
可能您的图像分辨率太高,因此10px圆不可见,尝试使用更高的圆角半径值 - 它应该使其可见。