Android:Issue Image resolution(Android:问题图像分辨率)
问题描述
在设备中使用高分辨率图像时出现问题.
Problem occurs while using high resolution image in device.
imageview a;
InputStream ims = getAssets().open("sam.png");//sam.png=520*1400 device=320*480 or 480*800
Drawable d=Drawable.createFromStream(ims, null);
a.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
a.setImageDrawable(d);
通过使用上面的代码图像在顶部和底部留下空间到下一个内容,或者如果我通过给定固定像素来缩小图像,它会在其大小上获得模糊图像.无论如何要解决这个问题?
by using above code image leaves spaces on top and bottom to next contents or If I shrink an image by giving fixed px, its get blur image on its size. Anyway to solve this issue?
推荐答案
希望以下解决方案有所帮助.您可以将 imageView 设为固定大小,并将该 imageView 的宽度和高度传递给 calculateInSampleSize
方法.根据图像大小决定是否对图像进行下采样.</p>
Hope following solution helps.
You can make your imageView of fixed size and pass that imageView's width and height to calculateInSampleSize
method. Based on image size, it will decide whether to down sample the image or not.
public Bitmap getBitmap(Context context, final String imagePath)
{
AssetManager assetManager = context.getAssets();
InputStream inputStream = null;
Bitmap bitmap = null;
try
{
inputStream = assetManager.open(imagePath);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inScaled = true;
options.inJustDecodeBounds = true;
// First decode with inJustDecodeBounds=true to check dimensions
bitmap = BitmapFactory.decodeStream(inputStream);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, requiredWidth, requiredHeight);
options.inJustDecodeBounds = false;
bitmap = BitmapFactory.decodeStream(inputStream);
}
catch(Exception exception)
{
exception.printStackTrace();
bitmap = null;
}
return bitmap;
}
public int calculateInSampleSize(BitmapFactory.Options options, final int requiredWidth, final int requiredHeight)
{
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if(height > requiredHeight || width > requiredWidth)
{
if(width > height)
{
inSampleSize = Math.round((float)height / (float)requiredHeight);
}
else
{
inSampleSize = Math.round((float)width / (float)requiredWidth);
}
}
return inSampleSize;
}
这篇关于Android:问题图像分辨率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Android:问题图像分辨率
基础教程推荐
- Java 中保存最后 N 个元素的大小受限队列 2022-01-01
- 如何使用 Stream 在集合中拆分奇数和偶数以及两者的总和 2022-01-01
- 如何使用 Eclipse 检查调试符号状态? 2022-01-01
- 由于对所需库 rt.jar 的限制,对类的访问限制? 2022-01-01
- 如何强制对超级方法进行多态调用? 2022-01-01
- Spring Boot Freemarker从2.2.0升级失败 2022-01-01
- 如何对 HashSet 进行排序? 2022-01-01
- 如何在不安装整个 WTP 包的情况下将 Tomcat 8 添加到 Eclipse Kepler 2022-01-01
- 首次使用 Hadoop,MapReduce Job 不运行 Reduce Phase 2022-01-01
- 在螺旋中写一个字符串 2022-01-01