• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2013 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.example.android.bitmapallocation;
18 
19 import android.app.Activity;
20 import android.graphics.Bitmap;
21 import android.graphics.BitmapFactory;
22 import android.os.Bundle;
23 import android.view.View;
24 import android.widget.CheckBox;
25 import android.widget.ImageView;
26 import android.widget.TextView;
27 
28 /**
29  * This example shows how to speed up bitmap loading and reduce garbage collection
30  * by reusing existing bitmaps.
31  *
32  * Watch the associated video for this demo on the DevBytes channel of developer.android.com,
33  * or on YouTube at https://www.youtube.com/watch?v=rsQet4nBVi8.
34  */
35 public class BitmapAllocation extends Activity {
36 
37     // There are some assumptions in this demo app that don't carry over well to the real world:
38     // it assumes that all bitmaps are the same size and that loading all bitmaps as the activity
39     // starts is good enough. A real application would be take a more flexible and robust
40     // approach. But these assumptions are good enough for the purposes of this tutorial,
41     // which is about reusing existing bitmaps of the same size.
42 
43     int mCurrentIndex = 0;
44     Bitmap mCurrentBitmap = null;
45     BitmapFactory.Options mBitmapOptions;
46 
47     @Override
onCreate(Bundle savedInstanceState)48     public void onCreate(Bundle savedInstanceState) {
49         super.onCreate(savedInstanceState);
50         setContentView(R.layout.activity_bitmap_allocation);
51 
52         final int[] imageIDs = {R.drawable.a, R.drawable.b, R.drawable.c, R.drawable.d,
53                 R.drawable.e, R.drawable.f};
54 
55         final CheckBox checkbox = (CheckBox) findViewById(R.id.checkbox);
56         final TextView durationTextview = (TextView) findViewById(R.id.loadDuration);
57         final ImageView imageview = (ImageView) findViewById(R.id.imageview);
58 
59         // Create bitmap to be re-used, based on the size of one of the bitmaps
60         mBitmapOptions = new BitmapFactory.Options();
61         mBitmapOptions.inJustDecodeBounds = true;
62         BitmapFactory.decodeResource(getResources(), R.drawable.a, mBitmapOptions);
63         mCurrentBitmap = Bitmap.createBitmap(mBitmapOptions.outWidth,
64                 mBitmapOptions.outHeight, Bitmap.Config.ARGB_8888);
65         mBitmapOptions.inJustDecodeBounds = false;
66         mBitmapOptions.inBitmap = mCurrentBitmap;
67         mBitmapOptions.inSampleSize = 1;
68         BitmapFactory.decodeResource(getResources(), R.drawable.a, mBitmapOptions);
69         imageview.setImageBitmap(mCurrentBitmap);
70 
71         // When the user clicks on the image, load the next one in the list
72         imageview.setOnClickListener(new View.OnClickListener() {
73 
74             @Override
75             public void onClick(View v) {
76                 mCurrentIndex = (mCurrentIndex + 1) % imageIDs.length;
77                 BitmapFactory.Options bitmapOptions = null;
78                 if (checkbox.isChecked()) {
79                     // Re-use the bitmap by using BitmapOptions.inBitmap
80                     bitmapOptions = mBitmapOptions;
81                     bitmapOptions.inBitmap = mCurrentBitmap;
82                 }
83                 long startTime = System.currentTimeMillis();
84                 mCurrentBitmap = BitmapFactory.decodeResource(getResources(),
85                         imageIDs[mCurrentIndex], bitmapOptions);
86                 imageview.setImageBitmap(mCurrentBitmap);
87 
88                 // One way you can see the difference between reusing and not is through the
89                 // timing reported here. But you can also see a huge impact in the garbage
90                 // collector if you look at logcat with and without reuse. Avoiding garbage
91                 // collection when possible, especially for large items like bitmaps,
92                 // is always a good idea.
93                 durationTextview.setText("Load took " +
94                         (System.currentTimeMillis() - startTime));
95             }
96         });
97     }
98 
99 }
100