使用此方法,以適應UR圖像,並獲得圓潤的邊角也
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 = 19;
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;
}
,並設置圖像的ImageView
addimg.setImageBitmap(getRoundedCornerBitmap(docode("Give path of your image here")));
在SD卡或手機內存
保存圖像,使用其解碼它的路徑。
public static Bitmap decodeFile(File f) {
try {
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(new FileInputStream(f), null, o);
// Find the correct scale value. It should be the power of 2.
final int REQUIRED_SIZE = 70;
int width_tmp = o.outWidth, height_tmp = o.outHeight;
int scale = 1;
while (true) {
if (width_tmp/2 < REQUIRED_SIZE
|| height_tmp/2 < REQUIRED_SIZE)
break;
width_tmp /= 2;
height_tmp /= 2;
scale++;
}
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
} catch (FileNotFoundException e) {
Log.e("decodeFile", "" + e);
}
return null;
}
使用'scaleType = 「fitXY」' – Piyush 2014-09-26 09:58:19
image1.setScaleType(ScaleType.FIT_XY); – iffu 2014-09-26 10:06:08
[相機顯示/全屏預覽不能保持寬高比 - 圖像偏斜,拉伸以適合屏幕]可能的重複(http://stackoverflow.com/questions/16727836/camera-display-preview-在全屏此結果未維持縱橫比圖像-i)的 – Gattsu 2014-09-26 10:09:52