我很困惑。
我有一个应用程序,它有一个由9个图像按钮组成的矩阵3 x 3
中心按钮有一个徽标,该徽标是从资源ID R.drawable.logo加载的,其他按钮被初始化为“尚无图像”图像。所有图像均为86x86,由屏幕构建器工具设置。
当用户登录时,将从web下载周围8个镜像中的一个或多个。它们也主要是86x86。
与预先设置的图像相比,下载的图像在x和y方向上大约是x的一半大小。
通过检查bitmapFactory选项对象,可以检查图像并知道其下载后的大小为86x86。
在此页面中通过两种不同方法下载的图像...How to load an ImageView by URL in Android?
发布于 2016-01-28 00:24:19
如果找不到目标设备密度dpi的图像,则从资源中拉出的图像将被放大。例如,如果您使用的设备是DisplayMetrics.DENSITY_HIGH (hdpi),但您只有/res/drawable-mdpi格式的图像,则当您通过getDrawable()之类的东西检索该图像时,该图像将自动放大。
但是,对于下载的图像,系统不知道图像的设计密度,因为它不包含在指定其密度的资源文件夹中,因此无法自动缩放。必须使用BitmapFactory.Options手动定义密度。考虑以下函数:
/**
* Downloads an image for a specified density DPI.
* @param context the current application context
* @param url the url of the image to download
* @param imgDensity the density DPI the image is designed for (DisplayMetrics.DENSITY_MEDIUM, DisplayMetrics.DENSITY_HIGH, etc.)
* @return the downloaded image as a Bitmap
*/
public static Bitmap loadBitmap(Context context, String url, int imgDensity) {
DisplayMetrics metrics = context.getResources().getDisplayMetrics();
BitmapFactory.Options options = new BitmapFactory.Options();
// This defines the density DPI the image is designed for.
options.inDensity = imgDensity;
// These define the density DPI you would like the image to be scaled to (if necessary).
options.inScreenDensity = metrics.densityDpi;
options.inTargetDensity = metrics.densityDpi;
try {
// Download image
InputStream is = new java.net.URL(url).openStream();
return BitmapFactory.decodeStream(is, null, options);
}
catch(Exception ex) {
// Handle error
}
return null;
}因此,如果指定URL处的图像是为mdpi屏幕设计的,则需要为imgDensity参数传递DisplayMetrics.DENSITY_MEDIUM。如果当前上下文具有较大的密度DPI (例如DisplayMetrics.DENSITY_HIGH),则会相应地放大图像。
发布于 2012-03-06 02:26:42
将android:scaleType="fitXY"添加到ImageView。这将始终在指定的边框内呈现图像。
https://stackoverflow.com/questions/9570740
复制相似问题