现在的位置: 首页 > 综合 > 正文

android根据density自动放大网络图片

2013年12月13日 ⁄ 综合 ⁄ 共 1265字 ⁄ 字号 评论关闭

首先,使用图片尽量用bitmap,因为drawable创建的对象不会自动适应分辨率和密度,所以,最好不用。

File file = new File(imagePath);

FileInputStream fileInputStream=null;
if (file.exists()) {
try {
fileInputStream=new FileInputStream(imagePath);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();

}

方法一:

/*
TypedValue value = new TypedValue();
Bitmap bitmap = BitmapFactory.decodeResourceStream(res, value,
fileInputStream, null, null);

*/

方法二:

/*
TypedValue value = new TypedValue();
BitmapFactory.Options opts = new BitmapFactory.Options();
int density = value.density;
            if (density == TypedValue.DENSITY_DEFAULT) {
                opts.inDensity = DisplayMetrics.DENSITY_DEFAULT;
            } else if (density != TypedValue.DENSITY_NONE) {
                opts.inDensity = density;
            }
            opts.inTargetDensity = res.getDisplayMetrics().densityDpi;
       
Bitmap bitmap = BitmapFactory.decodeStream(fileInputStream,null,opts);

*/

方法三:

BitmapFactory.Options options = new BitmapFactory.Options();
DisplayMetrics metrics = res.getDisplayMetrics();
options.inScreenDensity = metrics.densityDpi;
options.inTargetDensity =  metrics.densityDpi;
options.inDensity = DisplayMetrics.DENSITY_DEFAULT;

Bitmap bitmap = BitmapFactory.decodeStream(fileInputStream, null, options);

方法四:

Bitmap bitmap = BitmapFactory.decodeFile(imagePath,options);

file.setLastModified(System.currentTimeMillis());
return bitmap;

}

抱歉!评论已关闭.