【问题标题】:Bitmap and ImageView issue - problems with image width/height位图和 ImageView 问题 - 图像宽度/高度问题
【发布时间】:2016-06-28 18:15:01
【问题描述】:

我需要显示存储在数据库中的图像,但图像(位图)的宽度/高度和 ImageView 存在问题...

仅用于测试 - 当我在项目的可绘制对象中添加相同的图像时,我可以使用它:

作品:

Bitmap b = BitmapFactory.decodeResource(context.getResources(), R.drawable.menu_image);
BitmapDrawable bd = new BitmapDrawable(context.getResources(), b);
imageView.setImageDrawable(bd);

imageView.setImageResource(R.drawable.menu_image);

以下不起作用,因为图片未调整大小:

imageView.setImageBitmap(image);

imageView.setImageDrawable(new BitmapDrawable(context.getResources(), image));

使用此构造图像的位置:

public static Bitmap base64ToBitmap(String b64) {
    byte[] imageAsBytes = Base64.decode(b64.getBytes(), Base64.DEFAULT);
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inDensity =     context.getResources().getDisplayMetrics().densityDpi;
    options.inTargetDensity = context.getResources().getDisplayMetrics().densityDpi;
    Bitmap bitmap = BitmapFactory.decodeByteArray(imageAsBytes, 0, imageAsBytes.length, options);
    return bitmap;
}

图片原始尺寸为 338x94。

676x188,这是我使用项目可绘制目录中的图像时的图像大小。在这种情况下,这是我正在寻找的尺寸。我想一个快速的解决方法是使用 Bitmap.createScaledBitmap(),但我有几种不同的图像格式,我想使用 imageView.setImageBitmap 或 imageView.setImageDrawable 来表现我从项目的 drawables 目录加载 Bitmap 的行为。

【问题讨论】:

    标签: android android-bitmap bitmapfactory


    【解决方案1】:

    使用来自github的以下帮助类

    public class BitmapScaler
    {
    // scale and keep aspect ratio
    public static Bitmap scaleToFitWidth(Bitmap b, int width)
    {
        float factor = width / (float) b.getWidth();
        return Bitmap.createScaledBitmap(b, width, (int) (b.getHeight() * factor), true);
    }
    
    
    // scale and keep aspect ratio
    public static Bitmap scaleToFitHeight(Bitmap b, int height)
    {
        float factor = height / (float) b.getHeight();
        return Bitmap.createScaledBitmap(b, (int) (b.getWidth() * factor), height, true);
    }
    
    
    // scale and keep aspect ratio
    public static Bitmap scaleToFill(Bitmap b, int width, int height)
    {
        float factorH = height / (float) b.getWidth();
        float factorW = width / (float) b.getWidth();
        float factorToUse = (factorH > factorW) ? factorW : factorH;
        return Bitmap.createScaledBitmap(b, (int) (b.getWidth() * factorToUse),
                (int) (b.getHeight() * factorToUse), true);
    }
    
    
        // scale and don't keep aspect ratio
        public static Bitmap strechToFill(Bitmap b, int width, int height)
         {
        float factorH = height / (float) b.getHeight();
        float factorW = width / (float) b.getWidth();
        return Bitmap.createScaledBitmap(b, (int) (b.getWidth() * factorW),
                (int) (b.getHeight() * factorH), true);
         }
      } 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-01-12
      • 2021-10-27
      • 2012-03-15
      • 2020-06-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多