關於 Bitmap.createScaledBitmap(); 重新生成新的bitmap 問題

異常信息:

    java.lang.RuntimeException: Canvas: trying to use a recycled bitmap android.graphics.Bitmap@a693a8f
        at android.graphics.Canvas.throwIfCannotDraw(Canvas.java:1271)
        at android.view.DisplayListCanvas.throwIfCannotDraw(DisplayListCanvas.java:257)
        at android.graphics.Canvas.drawBitmap(Canvas.java:1415)
        at android.graphics.drawable.BitmapDrawable.draw(BitmapDrawable.java:545)
        at android.widget.ImageView.onDraw(ImageView.java:1268)
        at android.view.View.draw(View.java:17071)
        at android.view.View.updateDisplayListIfDirty(View.java:16053)
        at android.view.View.draw(View.java:16837)
        at android.view.ViewGroup.drawChild(ViewGroup.java:3764)
        at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3550)
        at android.view.View.updateDisplayListIfDirty(View.java:16048)
        at android.view.View.draw(View.java:16837)

 

先看createScaledBitmap()源碼:

    public static Bitmap createScaledBitmap(@NonNull Bitmap src, int dstWidth, int dstHeight,
            boolean filter) {
        Matrix m = new Matrix();

        final int width = src.getWidth();
        final int height = src.getHeight();
        if (width != dstWidth || height != dstHeight) {
            final float sx = dstWidth / (float) width;
            final float sy = dstHeight / (float) height;
            m.setScale(sx, sy);
        }
        return Bitmap.createBitmap(src, 0, 0, width, height, m, filter);
    }

實際是調用了 createBitmap() ,部分源碼如下:

    public static Bitmap createBitmap(@NonNull Bitmap source, int x, int y, int width, int height,
            @Nullable Matrix m, boolean filter) {

        checkXYSign(x, y);
        checkWidthHeight(width, height);
        if (x + width > source.getWidth()) {
            throw new IllegalArgumentException("x + width must be <= bitmap.width()");
        }
        if (y + height > source.getHeight()) {
            throw new IllegalArgumentException("y + height must be <= bitmap.height()");
        }
        if (source.isRecycled()) {
            throw new IllegalArgumentException("cannot use a recycled source in createBitmap");
        }

        // check if we can just return our argument unchanged
        if (!source.isMutable() && x == 0 && y == 0 && width == source.getWidth() &&
                height == source.getHeight() && (m == null || m.isIdentity())) {
            return source;
        }

源碼重點:當以下條件成立時 ,返回原bitmap,就不再生成新的bitmap


        // check if we can just return our argument unchanged
        if (!source.isMutable() && x == 0 && y == 0 && width == source.getWidth() &&
                height == source.getHeight() && (m == null || m.isIdentity())) {
            return source;
        }

導致問題出現的原因:由於createScaleBitmap 返回的是原bitmap ,且並沒有重新生成新的bitmap ,而後面做了bitmap回收處理,導致異常錯誤

            Bitmap newBp = Bitmap.createScaledBitmap(tmpBitmap, width,height, true);
            tmpBitmap.recycle();
            tmpBitmap=null;
            imageView.setImageBitmap(newBp);

解決方案:只需要把上面的條件改爲不成立即可,強制生成新bitmap ,或者也可以做width,height 判斷處理

如:在生成原bitmap 時 使用 

BitmapFactory.Options options=new BitmapFactory.Options();
options.inMutable=true;

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章