pec*_*eps 8 android image drawable
如何从InputStream(资源,文件系统)加载drawable并根据屏幕分辨率hdpi,mdpi或ldpi动态调整大小?
原始图像是hdpi,我只需要调整mdpi和ldpi的大小.
Android如何动态调整/ res中drawable的大小?
这很好很容易(其他答案对我不起作用),在这里找到:
ImageView iv = (ImageView) findViewById(R.id.imageView);
Bitmap bMap = BitmapFactory.decodeResource(getResources(), R.drawable.picture);
Bitmap bMapScaled = Bitmap.createScaledBitmap(bMap, newWidth, newHeight, true);
iv.setImageBitmap(bMapScaled);
Run Code Online (Sandbox Code Playgroud)
Android文档可在此处获得.
找到了:
/**
* Loads image from file system.
*
* @param context the application context
* @param filename the filename of the image
* @param originalDensity the density of the image, it will be automatically
* resized to the device density
* @return image drawable or null if the image is not found or IO error occurs
*/
public static Drawable loadImageFromFilesystem(Context context, String filename, int originalDensity) {
Drawable drawable = null;
InputStream is = null;
// set options to resize the image
Options opts = new BitmapFactory.Options();
opts.inDensity = originalDensity;
try {
is = context.openFileInput(filename);
drawable = Drawable.createFromResourceStream(context.getResources(), null, is, filename, opts);
} catch (Exception e) {
// handle
} finally {
if (is != null) {
try {
is.close();
} catch (Exception e1) {
// log
}
}
}
return drawable;
}
Run Code Online (Sandbox Code Playgroud)
像这样使用:
loadImageFromFilesystem(context, filename, DisplayMetrics.DENSITY_MEDIUM);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
22182 次 |
| 最近记录: |