1 /* 2 * Copyright (C) 2009 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.apis.graphics; 18 19 import android.content.Context; 20 import android.graphics.Bitmap; 21 import android.graphics.BitmapFactory; 22 import android.graphics.Canvas; 23 import android.graphics.Color; 24 import android.graphics.Paint; 25 import android.graphics.BitmapFactory.Options; 26 import android.view.View; 27 28 import java.io.ByteArrayOutputStream; 29 30 /** 31 * PurgeableBitmapView works with PurgeableBitmap to demonstrate the effects of setting 32 * Bitmaps as being purgeable. 33 * 34 * PurgeableBitmapView decodes an encoded bitstream to a Bitmap each time update() 35 * is invoked(), and its onDraw() draws the Bitmap and a number to screen. 36 * The number is used to indicate the number of Bitmaps that has been decoded. 37 */ 38 public class PurgeableBitmapView extends View { 39 private final byte[] bitstream; 40 41 private Bitmap mBitmap; 42 private final int mArraySize = 200; 43 private final Bitmap[] mBitmapArray = new Bitmap [mArraySize]; 44 private final Options mOptions = new Options(); 45 private static final int WIDTH = 150; 46 private static final int HEIGHT = 450; 47 private static final int STRIDE = 320; // must be >= WIDTH 48 private int mDecodingCount = 0; 49 private final Paint mPaint = new Paint(); 50 private final int textSize = 32; 51 private static int delay = 100; 52 PurgeableBitmapView(Context context, boolean isPurgeable)53 public PurgeableBitmapView(Context context, boolean isPurgeable) { 54 super(context); 55 setFocusable(true); 56 mOptions.inPurgeable = isPurgeable; 57 58 int[] colors = createColors(); 59 Bitmap src = Bitmap.createBitmap(colors, 0, STRIDE, WIDTH, HEIGHT, 60 Bitmap.Config.ARGB_8888); 61 bitstream = generateBitstream(src, Bitmap.CompressFormat.JPEG, 80); 62 63 mPaint.setTextSize(textSize); 64 mPaint.setColor(Color.GRAY); 65 } 66 createColors()67 private int[] createColors() { 68 int[] colors = new int[STRIDE * HEIGHT]; 69 for (int y = 0; y < HEIGHT; y++) { 70 for (int x = 0; x < WIDTH; x++) { 71 int r = x * 255 / (WIDTH - 1); 72 int g = y * 255 / (HEIGHT - 1); 73 int b = 255 - Math.min(r, g); 74 int a = Math.max(r, g); 75 colors[y * STRIDE + x] = (a << 24) | (r << 16) | (g << 8) | b; 76 } 77 } 78 return colors; 79 } 80 update(PurgeableBitmap.RefreshHandler handler)81 public int update(PurgeableBitmap.RefreshHandler handler) { 82 try { 83 mBitmapArray[mDecodingCount] = BitmapFactory.decodeByteArray( 84 bitstream, 0, bitstream.length, mOptions); 85 mBitmap = mBitmapArray[mDecodingCount]; 86 mDecodingCount++; 87 if (mDecodingCount < mArraySize) { 88 handler.sleep(delay); 89 return 0; 90 } else { 91 return -mDecodingCount; 92 } 93 94 } catch (OutOfMemoryError error) { 95 for (int i = 0; i < mDecodingCount; i++) { 96 mBitmapArray[i].recycle(); 97 } 98 return mDecodingCount + 1; 99 } 100 } 101 onDraw(Canvas canvas)102 @Override protected void onDraw(Canvas canvas) { 103 canvas.drawColor(Color.WHITE); 104 canvas.drawBitmap(mBitmap, 0, 0, null); 105 canvas.drawText(String.valueOf(mDecodingCount), WIDTH / 2 - 20, 106 HEIGHT / 2, mPaint); 107 } 108 generateBitstream(Bitmap src, Bitmap.CompressFormat format, int quality)109 private byte[] generateBitstream(Bitmap src, Bitmap.CompressFormat format, 110 int quality) { 111 ByteArrayOutputStream os = new ByteArrayOutputStream(); 112 src.compress(format, quality, os); 113 return os.toByteArray(); 114 } 115 116 } 117