Bitmap.compress 花费太多时间(约 2 秒)

abr*_*sze 1 android bitmap bitmapimage bitmapfactory android-bitmap

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

在活动中:

icon= BitmapFactory.decodeResource(getResources(),R.mipmap.image);
Run Code Online (Sandbox Code Playgroud)

在回调类中:

        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);
        }
Run Code Online (Sandbox Code Playgroud)

此行大约需要 2 秒:

icon.compress(Bitmap.CompressFormat.PNG, 100, stream);
Run Code Online (Sandbox Code Playgroud)

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

Moh*_*oor 5

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

icon.compress(Bitmap.CompressFormat.JPEG, 100, stream);
Run Code Online (Sandbox Code Playgroud)