diff options
| author | Chet Haase <chet@google.com> | 2013-01-10 16:19:38 -0800 |
|---|---|---|
| committer | Chet Haase <chet@google.com> | 2013-01-14 15:21:15 -0800 |
| commit | 74497f33361e3e9f0cb33d3111b5198444af5794 (patch) | |
| tree | fdd5424398128a61c1c1bf0c150c76fd89919b3c /samples/devbytes/graphics/BitmapAllocation/src/com/example/android/bitmapallocation/BitmapAllocation.java | |
| parent | 7ca2ee364e57d481a212042cb50da48f8c3db374 (diff) | |
First set of demo apps for DevBytes videos
Change-Id: I4ae984cf776604cede32b98db6b1eb3cb6c59c7e
Diffstat (limited to 'samples/devbytes/graphics/BitmapAllocation/src/com/example/android/bitmapallocation/BitmapAllocation.java')
| -rw-r--r-- | samples/devbytes/graphics/BitmapAllocation/src/com/example/android/bitmapallocation/BitmapAllocation.java | 99 |
1 files changed, 99 insertions, 0 deletions
diff --git a/samples/devbytes/graphics/BitmapAllocation/src/com/example/android/bitmapallocation/BitmapAllocation.java b/samples/devbytes/graphics/BitmapAllocation/src/com/example/android/bitmapallocation/BitmapAllocation.java new file mode 100644 index 000000000..cc6b1e00a --- /dev/null +++ b/samples/devbytes/graphics/BitmapAllocation/src/com/example/android/bitmapallocation/BitmapAllocation.java @@ -0,0 +1,99 @@ +/* + * Copyright (C) 2013 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.android.bitmapallocation; + +import android.app.Activity; +import android.graphics.Bitmap; +import android.graphics.BitmapFactory; +import android.os.Bundle; +import android.view.View; +import android.widget.CheckBox; +import android.widget.ImageView; +import android.widget.TextView; + +/** + * This example shows how to speed up bitmap loading and reduce garbage collection + * by reusing existing bitmaps. + * + * Watch the associated video for this demo on the DevBytes channel of developer.android.com, + * or on YouTube at https://www.youtube.com/watch?v=rsQet4nBVi8. + */ +public class BitmapAllocation extends Activity { + + // There are some assumptions in this demo app that don't carry over well to the real world: + // it assumes that all bitmaps are the same size and that loading all bitmaps as the activity + // starts is good enough. A real application would be take a more flexible and robust + // approach. But these assumptions are good enough for the purposes of this tutorial, + // which is about reusing existing bitmaps of the same size. + + int mCurrentIndex = 0; + Bitmap mCurrentBitmap = null; + BitmapFactory.Options mBitmapOptions; + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_bitmap_allocation); + + final int[] imageIDs = {R.drawable.a, R.drawable.b, R.drawable.c, R.drawable.d, + R.drawable.e, R.drawable.f}; + + final CheckBox checkbox = (CheckBox) findViewById(R.id.checkbox); + final TextView durationTextview = (TextView) findViewById(R.id.loadDuration); + final ImageView imageview = (ImageView) findViewById(R.id.imageview); + + // Create bitmap to be re-used, based on the size of one of the bitmaps + mBitmapOptions = new BitmapFactory.Options(); + mBitmapOptions.inJustDecodeBounds = true; + BitmapFactory.decodeResource(getResources(), R.drawable.a, mBitmapOptions); + mCurrentBitmap = Bitmap.createBitmap(mBitmapOptions.outWidth, + mBitmapOptions.outHeight, Bitmap.Config.ARGB_8888); + mBitmapOptions.inJustDecodeBounds = false; + mBitmapOptions.inBitmap = mCurrentBitmap; + mBitmapOptions.inSampleSize = 1; + BitmapFactory.decodeResource(getResources(), R.drawable.a, mBitmapOptions); + imageview.setImageBitmap(mCurrentBitmap); + + // When the user clicks on the image, load the next one in the list + imageview.setOnClickListener(new View.OnClickListener() { + + @Override + public void onClick(View v) { + mCurrentIndex = (mCurrentIndex + 1) % imageIDs.length; + BitmapFactory.Options bitmapOptions = null; + if (checkbox.isChecked()) { + // Re-use the bitmap by using BitmapOptions.inBitmap + bitmapOptions = mBitmapOptions; + bitmapOptions.inBitmap = mCurrentBitmap; + } + long startTime = System.currentTimeMillis(); + mCurrentBitmap = BitmapFactory.decodeResource(getResources(), + imageIDs[mCurrentIndex], bitmapOptions); + imageview.setImageBitmap(mCurrentBitmap); + + // One way you can see the difference between reusing and not is through the + // timing reported here. But you can also see a huge impact in the garbage + // collector if you look at logcat with and without reuse. Avoiding garbage + // collection when possible, especially for large items like bitmaps, + // is always a good idea. + durationTextview.setText("Load took " + + (System.currentTimeMillis() - startTime)); + } + }); + } + +} |
