圓角代碼對Volley NetworkImageView和資源都起作用。但它不適用於來自ApplicationContext的位圖。下面是代碼。圓角無法在openFileInput位圖上工作
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個小時。有人可以點亮一些光線嗎?
什麼意思是「不起作用」?你看到沒有圓角的圖像,或者根本看不到圖像? – Divers
@潛水員我可以看到圖像,但沒有看到圓角。 – 53iScott
我認爲這個問題在於你的SD卡圖像具有更大的分辨率,這就是爲什麼10px的圓角半徑不足以產生視覺效果。其他建議是因爲來自SD卡的scaleType和其他圖像的長寬比,所以剛剛由ImageView進行了掃描。 – Divers