使用此方法,以适应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