0

我在资产文件夹中有一个要显示的位图图像列表(使用 ViewPager)。我尝试根据屏幕尺寸(使用布局参数)设置图像的宽度和高度。但是图像质量会受到干扰。如何让图像质量更好?

 Drawable drw = Drawable.createFromStream(getAssets().
                            open("Parts/"+drawables[i]),null);

在这里,drawables[i]String[](比如ball.bmp,“Parts”是asset的子文件夹)。现在,我已将 imageview 中的图像设置为,

imageView.setBackgroundDrawable(imageArray[position]);

该图像在移动设备中看起来不错,但在标签中看起来很拉伸。

4

1 回答 1

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;
}
于 2013-09-21T04:24:02.560 回答