【问题标题】:How to compress images programmatically如何以编程方式压缩图像
【发布时间】:2019-01-14 16:37:36
【问题描述】:

我的资源文件夹中有 30 到 40 张图片。我目前正在使用 DrawBitpMap() 在我的屏幕上绘制它。但它给了我一个错误“VM outOfMemeory 错误”。所以我试图调整图像的大小。现在我的图像分辨率是 160x160。现在我没有得到那个错误。它运行没有任何问题。所以我的问题是, 无论如何我可以以编程方式压缩我的图像,而不是手动调整资源中的图像大小?

【问题讨论】:

    标签: android image


    【解决方案1】:

    试试这个例子

    public class bitmaptest extends Activity {
        @Override
        public void onCreate(Bundle icicle) {
            super.onCreate(icicle);
            LinearLayout linLayout = new LinearLayout(this);
    
            // load the origial BitMap (500 x 500 px)
            Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(), 
                   R.drawable.android);
    
            int width = bitmapOrg.width();
            int height = bitmapOrg.height();
            int newWidth = 200;
            int newHeight = 200;
    
            // calculate the scale - in this case = 0.4f
            float scaleWidth = ((float) newWidth) / width;
            float scaleHeight = ((float) newHeight) / height;
    
            // createa matrix for the manipulation
            Matrix matrix = new Matrix();
            // resize the bit map
            matrix.postScale(scaleWidth, scaleHeight);
            // rotate the Bitmap
            matrix.postRotate(45);
    
            // recreate the new Bitmap
            Bitmap resizedBitmap = Bitmap.createBitmap(bitmapOrg, 0, 0, 
                              width, height, matrix, true); 
    
            // make a Drawable from Bitmap to allow to set the BitMap 
            // to the ImageView, ImageButton or what ever
            BitmapDrawable bmd = new BitmapDrawable(resizedBitmap);
    
            ImageView imageView = new ImageView(this);
    
            // set the Drawable on the ImageView
            imageView.setImageDrawable(bmd);
    
            // center the Image
            imageView.setScaleType(ScaleType.CENTER);
    
            // add ImageView to the Layout
            linLayout.addView(imageView, 
                new LinearLayout.LayoutParams(
                          LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT
                    )
            );
    
            // set LinearLayout as ContentView
            setContentView(linLayout);
        }
    }
    

    你也可以这样做

    Bitmap.createScaledBitmap(yourimage, 160, 160, true);
    

    【讨论】:

      【解决方案2】:

      这就是我解决这个问题的方法。

      Bitmap originalImage= Bitmap.createScaledBitmap (BitmapFactory.decodeResource(getResources(), imageId), 160, 160, true);
      

      【讨论】:

      • 正如我在回答中所说,除非您有令人信服的理由这样做,否则我建议您在 android 之外缩放图像。每次应用程序需要为屏幕充气时缩放每个位图的成本太高了。为什么每次都这样?
      【解决方案3】:

      要在 android 中压缩图像,请使用 Bitmap 类中的方法 compress

      但我相信您想要缩放,而不是压缩。在同一个类中有一个方法可以对其进行缩放。

      或者,不是每次读取图像时都调整大小,而是在批处理文件中使用 ImageMagick 在 PC 中调整它们的大小,然后将它们复制到已调整大小的 res/ 文件夹中。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-01-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-03-23
        • 1970-01-01
        相关资源
        最近更新 更多