{"id":1550,"date":"2022-08-30T15:17:28","date_gmt":"2022-08-30T15:17:28","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/27\/how-to-safely-remove-bitmaps-from-a-viewpager-in-android-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:17:28","modified_gmt":"2022-08-30T15:17:28","slug":"how-to-safely-remove-bitmaps-from-a-viewpager-in-android-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/how-to-safely-remove-bitmaps-from-a-viewpager-in-android-collection-of-common-programming-errors\/","title":{"rendered":"how to safely remove bitmaps from a viewPager in android-Collection of common programming errors"},"content":{"rendered":"<p>I have a viewPager in which i hold fragments and every fragment has a GifDecoderView<\/p>\n<pre><code>public class GifDecoderView extends ImageView {\n\nprivate boolean mIsPlayingGif = false;\n\nprivate GifDecoder mGifDecoder;\n\nprivate Bitmap mTmpBitmap;\n\n\nfinal Handler mHandler = new Handler();\n\nfinal Runnable mUpdateResults = new Runnable() {\n    public void run() {\n        if (mTmpBitmap != null &amp;&amp; !mTmpBitmap.isRecycled()) {\n            GifDecoderView.this.setImageBitmap(mTmpBitmap);\n        }\n    }\n};\n\npublic GifDecoderView(Context context, InputStream stream) {\n    super(context);\n    playGif(stream);\n}\npublic GifDecoderView(Context context, AttributeSet attrs) {\n    super(context,attrs);\n}\npublic void doNothing(){\n    \/\/do nothing\n    int a = 2+2;\n}\n@Override\nprotected void onDraw(Canvas canvas) {\n    \/\/ TODO Auto-generated method stub\n    super.onDraw(canvas);\n}\n\npublic void playGif(InputStream stream) {\n    mGifDecoder = new GifDecoder();\n    mGifDecoder.read(stream);\n\n    mIsPlayingGif = true;\n\n    new Thread(new Runnable() {\n        public void run() {\n            final int n = mGifDecoder.getFrameCount();\n            final int ntimes = mGifDecoder.getLoopCount();\n            int repetitionCounter = 0;\n            do {\n                for (int i = 0; i &lt; n; i++) {\n                    mTmpBitmap = mGifDecoder.getFrame(i);\n                    int t = mGifDecoder.getDelay(i);\n                    mHandler.post(mUpdateResults);\n                    try {\n                        Thread.sleep(t);\n                    } catch (InterruptedException e) {\n                        e.printStackTrace();\n                    }\n                }\n                if(ntimes != 0) {\n                    repetitionCounter ++;\n                }\n            } while (mIsPlayingGif &amp;&amp; (repetitionCounter<\/code><\/pre>\n<p id=\"rop\"><small>Originally posted 2013-11-27 05:08:53. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>I have a viewPager in which i hold fragments and every fragment has a GifDecoderView public class GifDecoderView extends ImageView { private boolean mIsPlayingGif = false; private GifDecoder mGifDecoder; private Bitmap mTmpBitmap; final Handler mHandler = new Handler(); final Runnable mUpdateResults = new Runnable() { public void run() { if (mTmpBitmap != null &amp;&amp; !mTmpBitmap.isRecycled()) [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1550","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1550","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=1550"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1550\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=1550"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=1550"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=1550"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}