【问题标题】:Bitmap.compress takes too much time ( ~2 seconds )Bitmap.compress 需要太多时间(~2 秒)
【发布时间】:2017-08-22 06:52:35
【问题描述】:

位图压缩过程需要太多时间。我该如何解决这个问题?

在活动中:

icon= BitmapFactory.decodeResource(getResources(),R.mipmap.image);

在回调类中:

        synchronized (holder) {
            stream = new ByteArrayOutputStream();
            Log.d("LIFE_CYCLE", "settingImage 1=" + System.currentTimeMillis());
            icon.compress(Bitmap.CompressFormat.PNG, 100, stream);
            Log.d("LIFE_CYCLE", "settingImage 2=" + System.currentTimeMillis());
            byteArray = stream.toByteArray();
            b = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
            if(mWidth<mHeight){
                icon= Bitmap.createScaledBitmap(b, (int)(mWidth*0.75), (int)(mWidth*0.75), false);
            }
            else{
                icon= Bitmap.createScaledBitmap(b, (int)(mHeight*0.75), (int)(mHeight*0.75), false);
            }

            canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
            canvas.drawBitmap(icon, ((mWidth)-icon.getWidth())/2, (mHeight-icon.getHeight())/2, new Paint());
            draw_target(canvas);
        }

这条线大约需要 2 秒:

icon.compress(Bitmap.CompressFormat.PNG, 100, stream);

附:我的图像是部分透明的,所以我需要使用 .PNG 而不是 .JPG

【问题讨论】:

  • 你到底想做什么?
  • 在surfaceView上绘制一张图片(来自mipmap文件夹)
  • 这段代码没有意义。你没有存储压缩位图,所以压缩是没用的。它还使用内存不必要的图标和 b 占用相同数量的内存。只需从图标创建缩放。最好先加载缩放图标。
  • 塞尔文,谢谢!!!这对我有帮助!!!!请把它写成答案,我会接受的。再次感谢我的朋友!

标签: android bitmap android-bitmap bitmapimage bitmapfactory


【解决方案1】:

尝试使用 JPG 而不是 PNG100% 质量 进行压缩。它完成得如此之快,质量也更好。我也遇到了这个问题并解决了这个问题。

icon.compress(Bitmap.CompressFormat.JPEG, 100, stream);

【讨论】:

    【解决方案2】:

    尝试在异步任务中使用位图相关操作,因为最好在后台线程/异步任务上执行繁重的操作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-01-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-15
      • 2011-09-23
      • 2013-05-19
      • 2011-06-03
      相关资源
      最近更新 更多