我有一个要显示的资产文件夹中的位图图像列表(使用ViewPager)。我试图根据屏幕大小设置图像的宽度和高度(使用布局参数)。但图像质量受到干扰。如何提高图像质量?
Drawable drw = Drawable.createFromStream(getAssets().
open("Parts/"+drawables[i]),null);
此处,drawables[i]
是String[]
(比如ball.bmp,“Parts”是资产的子文件夹)。
现在,我已将imageview中的图像设置为
imageView.setBackgroundDrawable(imageArray[position]);
图像在移动设备中看起来不错,但在标签中看起来很拉伸。
答案 0 :(得分:0)
试试这个;解码图像并找到正确的比例值:
private Bitmap getBitmap(String url) {
// from web
try {
Bitmap bitmap = null;
URL imageUrl = new URL(url);
HttpURLConnection conn = (HttpURLConnection) imageUrl
.openConnection();
conn.setConnectTimeout(1000);
conn.setReadTimeout(1000);
conn.setInstanceFollowRedirects(true);
InputStream is = conn.getInputStream();
OutputStream os = new FileOutputStream(f);
Utils.CopyStream(is, os);
os.close();
bitmap = decodeFile(f);
return bitmap;
} catch (Exception ex) {
ex.printStackTrace();
return null;
}
}
// decodes image and scales it to reduce memory consumption
private Bitmap decodeFile(File f) {
try {
// decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(new FileInputStream(f), null, o);
// Find the correct scale value. It should be the power of 2.
final int REQUIRED_SIZE = 150;
int width_tmp = o.outWidth, height_tmp = o.outHeight;
int scale = 1;
while (true) {
if (width_tmp / 2 < REQUIRED_SIZE
|| height_tmp / 2 < REQUIRED_SIZE)
break;
width_tmp /= 2;
height_tmp /= 2;
scale *= 2;
}
// decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
} catch (FileNotFoundException e) {
}
return null;
}