如何解决Android —地图标记显示方式不同dip / px或解压缩
|| 我的地图图钉尺寸有个奇怪的问题。为了保持动态性,将不同类别的地图图钉存储在站点的服务器上,以便即使在应用程序发布后也可以随时更改它们。 每次下载时,我都会缓存这些图钉,并且只有在服务器发回一点消息说自上次下载以来,它已更改时,我才重新下载它们。第一次抓图钉时,我先使用位图,然后再将它们保存到文件中,并且地图标记的大小正确。每次之后,我都会直接从图像文件中加载已保存的图钉版本。与使用第一次下载的位图时相比,这些显示的尺寸要小得多。 起初,我认为保存PNG的方式存在问题,但它们的大小正确(64 x 64)。这是dip / px问题还是我需要使用某种选项解压缩图像文件? 这是我第一次拍摄图像的方式:public static Bitmap loadMapPin(String category,int width,int height) {
URL imageUrl;
category = category.toLowerCase().replace(\" \",\"\");
try {
imageUrl = new URL(PIN_URL+category+\".png\");
InputStream is = (InputStream) imageUrl.getContent();
Options options = new Options();
options.inJustDecodeBounds = true; //Only find the dimensions
//Decode without downloading to find dimensions
BitmapFactory.decodeStream(is,null,options);
boolean scaleByHeight = Math.abs(options.outHeight - height) >= Math.abs(options.outWidth - width);
if(options.outHeight * options.outWidth >= width * height){
// Load,scaling to smallest power of 2 that\'ll get it <= desired dimensions
double sampleSize = scaleByHeight
? options.outHeight / height
: options.outWidth / width;
options.inSampleSize =
(int)Math.pow(2d,Math.floor(
Math.log(sampleSize)/Math.log(2d)));
}
options.inJustDecodeBounds = false; //Download image this time
is.close();
is = (InputStream) imageUrl.getContent();
Bitmap img = BitmapFactory.decodeStream(is,options);
return img;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
这是我从缓存文件中加载它们的方式:
BitmapFactory.decodeFile(filepath);
提前致谢!
解决方法
我发现默认情况下,将图像解压缩为位图无法在高密度屏幕上缩放。您必须将密度设置为无。换句话说,您指定图像用于未知密度。
解:
Bitmap b = BitmapFactory.decodeFile(filepath);
b.setDensity(Bitmap.DENSITY_NONE);
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。