9

我正在创建一个RecyclerView显示图片网格。选择其中一个时,它应该打开一个带有转换的新活动。

我正在使用 Glide 库来加载图片,并且过渡看起来很糟糕,因为它会在新活动中重新加载图片。所以我不得不将它保存在缓存中,然后将其用于过渡。

我有代码,但有时如果图片没有加载,它会抛出 Canvas RuntimeException。

这是日志:

07-03 15:19:58.633  28461-28461/jahirfiquitiva.project E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: jahirfiquitiva.project, PID: 28461
    java.lang.RuntimeException: Canvas: trying to use a recycled bitmap android.graphics.Bitmap@29f09d20
            at android.graphics.Canvas.throwIfCannotDraw(Canvas.java:1282)
            at android.view.GLES20Canvas.drawBitmap(GLES20Canvas.java:599)
            at android.graphics.drawable.BitmapDrawable.draw(BitmapDrawable.java:538)
            at android.widget.ImageView.onDraw(ImageView.java:1176)
            at android.view.View.draw(View.java:15239)
            at android.view.View.updateDisplayListIfDirty(View.java:14175)
            at android.view.View.getDisplayList(View.java:14197)
            at android.view.GhostView.onDraw(GhostView.java:52)
            at android.view.View.draw(View.java:15239)
            at android.view.View.updateDisplayListIfDirty(View.java:14175)
            at android.view.View.getDisplayList(View.java:14197)
            at android.view.View.draw(View.java:14967)
            at android.view.ViewGroup.drawChild(ViewGroup.java:3406)
            at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3199)
            at android.view.View.updateDisplayListIfDirty(View.java:14170)
            at android.view.View.getDisplayList(View.java:14197)
            at android.view.View.draw(View.java:14967)
            at android.view.ViewGroup.drawChild(ViewGroup.java:3406)
            at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3199)
            at android.view.ViewOverlay$OverlayViewGroup.dispatchDraw(ViewOverlay.java:219)
            at android.view.View.draw(View.java:15248)
            at android.widget.FrameLayout.draw(FrameLayout.java:598)
            at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:2906)
            at android.view.View.updateDisplayListIfDirty(View.java:14175)
            at android.view.View.getDisplayList(View.java:14197)
            at android.view.ThreadedRenderer.updateViewTreeDisplayList(ThreadedRenderer.java:273)
            at android.view.ThreadedRenderer.updateRootDisplayList(ThreadedRenderer.java:279)
            at android.view.ThreadedRenderer.draw(ThreadedRenderer.java:318)
            at android.view.ViewRootImpl.draw(ViewRootImpl.java:2536)
            at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:2352)
            at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1982)
            at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1061)
            at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5891)
            at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
            at android.view.Choreographer.doCallbacks(Choreographer.java:580)
            at android.view.Choreographer.doFrame(Choreographer.java:550)
            at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5289)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)

这是打开其他活动并将图片保存为缓存的代码:

private void openViewer(WallpapersAdapter.WallsHolder wallsHolder, int index, final HashMap<String, String> data) {

        final Intent intent = new Intent(wallsActivity, ViewerActivity.class);
        intent.putExtra("wallUrl", data.get(WallpapersActivity.WALL));
        intent.putExtra("wallName", data.get(WallpapersActivity.NAME));
        intent.putExtra("transitionName", ViewCompat.getTransitionName(wallsHolder.wall));

        //save image from drawable
        //get its path and send it to activity
        Bitmap bitmap = drawableToBitmap(wallsHolder.wall.getDrawable());
        //Convert to byte array and send to the other activity

        Log.e("Resolution", bitmap.getWidth() + "x" + bitmap.getHeight());
        try {
            //Write file
            String filename = "bitmap.png";
            FileOutputStream stream = this.openFileOutput(filename, Context.MODE_PRIVATE);
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);

            //Cleanup
            stream.close();
            bitmap.recycle();

            //Pop intent
            intent.putExtra("image", filename);
        } catch (Exception e) {
            e.printStackTrace();
        }

        ActivityOptionsCompat options = ActivityOptionsCompat.makeSceneTransitionAnimation(
                this, wallsHolder.wall, ViewCompat.getTransitionName(wallsHolder.wall));
        startActivity(intent, options.toBundle());

    }

    public static Bitmap drawableToBitmap (Drawable drawable) {
        Bitmap bitmap = null;
        if (drawable instanceof BitmapDrawable) {
            BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
            if(bitmapDrawable.getBitmap() != null) {
                return bitmapDrawable.getBitmap();
            }
        }

        if(drawable.getIntrinsicWidth() <= 0 || drawable.getIntrinsicHeight() <= 0) {
            bitmap = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888); // Single color bitmap will be created of 1x1 pixel
        } else {
            bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
        }

        Canvas canvas = new Canvas(bitmap);
        drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
        drawable.draw(canvas);
        return bitmap;
    }

我能做些什么来解决这个问题?提前致谢。

4

5 回答 5

13

我怀疑你的位图有时会在Canvas有机会在这里绘制之前进入回收状态drawable.draw(canvas);

一个快速的解决方案应该是不调用bitmap.recycle();,这对于android >2.3.3并不是严格要求的。如果您仍想强制回收此内存,则必须找到一种方法来检查何时确实不再需要该位图(即,Canvas有机会完成其绘制操作)。

于 2015-07-05T06:11:59.770 回答
5

移动bitmap.recycle();到代码中不再需要此位图的另一个位置。

于 2015-07-05T07:16:13.070 回答
1

使用这个自定义 ImageView 类

public class MyImageView extends ImageView {
 
    public MyImageView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }
 
    public MyImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
 
    public MyImageView(Context context) {
        super(context);
    }
 
    @Override
    protected void onDraw(Canvas canvas) {
        try {
            super.onDraw(canvas);
        } catch (Exception e) {
            Log.i(MyImageView.class.getSimpleName(), "Catch Canvas: trying to use a recycled bitmap");
        }
    }
 
}
于 2021-01-25T22:37:24.070 回答
0

我通过添加这个解决了:

Glide.with(activity).clear(view);

在加载图像之前:

Glide.with(activity)
                .load(imageUrl)
                .apply(options)
                .placeholder(R.drawable.loading_image)
                .error(R.drawable.not_found)
                .into(view);

请参阅文档:

http://bumptech.github.io/glide/doc/resourcereuse.html

http://bumptech.github.io/glide/doc/resourcereuse.html#cannot-draw-a-recycled-bitmap

于 2020-11-25T08:22:00.580 回答
0

我对画布不太了解(我不经常使用动画),但如果你找不到任何方法来解决这个问题,你可以尝试使用这个库:https ://github.com/codepath/android_guides /wiki/shared-element-activity-transition

于 2015-07-04T12:18:22.650 回答