如何从InputStream(资产,文件系统)加载drawable并基于屏幕分辨率hdpi,mdpi或ldpi动态调整其大小?Android负载以编程方式绘制并调整大小
原始图像是在hdpi中,我只需要调整大小为mdpi和ldpi。
有谁知道Android如何动态调整/ res中的drawables的大小?
如何从InputStream(资产,文件系统)加载drawable并基于屏幕分辨率hdpi,mdpi或ldpi动态调整其大小?Android负载以编程方式绘制并调整大小
原始图像是在hdpi中,我只需要调整大小为mdpi和ldpi。
有谁知道Android如何动态调整/ res中的drawables的大小?
发现:
/**
* 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 (Throwable e) {
// handle
} finally {
if (is != null) {
try {
is.close();
} catch (Throwable e1) {
// ingore
}
}
}
return drawable;
}
使用这样的:
loadImageFromFilesystem(context, filename, DisplayMetrics.DENSITY_MEDIUM);
Unfortunatley此代码不适用于HTC Desire HD和HTC Evo。在此处查看解决方案:http://stackoverflow.com/questions/7747089/exception-in-drawable-createfromresourcestream-htc-only/9195531#9195531 – peceps 2012-02-08 14:46:47
如果你想显示的图像,但不幸的是这个形象是大尺寸的,让例子,你要显示的图像以30乘30的格式,然后检查它的大小,如果它大于你的要求大小,然后除以你的数量(在这里是30 * 30),然后你再次拿到并用来再次分割图像区域。
drawable = this.getResources().getDrawable(R.drawable.pirImg);
int width = drawable.getIntrinsicWidth();
int height = drawable.getIntrinsicHeight();
if (width > 30)//means if the size of an image is greater than 30*30
{
width = drawable.getIntrinsicWidth()/30;
height = drawable.getIntrinsicWidth()/30;
}
drawable.setBounds(
0, 0,
drawable.getIntrinsicWidth()/width,
drawable.getIntrinsicHeight()/height);
//and now add the modified image in your overlay
overlayitem[i].setMarker(drawable)
后加载您的图片,并将其设置为imageview的 你可以使用layoutparamsto大小的图像match_parent
这样
android.view.ViewGroup.LayoutParams layoutParams = imageView.getLayoutParams();
layoutParams.width =MATCH_PARENT;
layoutParams.height =MATCH_PARENT;
imageView.setLayoutParams(layoutParams);
出于好奇,有什么理由不预上浆它(为'mdpi'和'ldpi')并在'res'目录中链接到它? – 2012-01-12 15:45:58
从网络下载的图像。我可以在服务器上执行预处理,但下载速度会变慢。 – peceps 2012-01-12 15:48:42