BitmapTileProvider.java revision 6dc3866ac10cc6bf38e73f098fe1988d1a6eecd4
1/* 2 * Copyright (C) 2010 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 17package com.android.gallery3d.ui; 18 19import android.graphics.Bitmap; 20import android.graphics.Bitmap.Config; 21import android.graphics.Canvas; 22 23import com.android.gallery3d.common.BitmapUtils; 24import com.android.gallery3d.data.BitmapPool; 25 26import java.util.ArrayList; 27 28public class BitmapTileProvider implements TileImageView.TileSource { 29 private final ScreenNail mScreenNail; 30 private final Bitmap[] mMipmaps; 31 private final Config mConfig; 32 private final int mImageWidth; 33 private final int mImageHeight; 34 35 private boolean mRecycled = false; 36 37 public BitmapTileProvider(Bitmap bitmap, int maxBackupSize) { 38 mImageWidth = bitmap.getWidth(); 39 mImageHeight = bitmap.getHeight(); 40 ArrayList<Bitmap> list = new ArrayList<Bitmap>(); 41 list.add(bitmap); 42 while (bitmap.getWidth() > maxBackupSize 43 || bitmap.getHeight() > maxBackupSize) { 44 bitmap = BitmapUtils.resizeBitmapByScale(bitmap, 0.5f, false); 45 list.add(bitmap); 46 } 47 48 mScreenNail = new BitmapScreenNail(list.remove(list.size() - 1)); 49 mMipmaps = list.toArray(new Bitmap[list.size()]); 50 mConfig = Config.ARGB_8888; 51 } 52 53 @Override 54 public ScreenNail getScreenNail() { 55 return mScreenNail; 56 } 57 58 @Override 59 public int getImageHeight() { 60 return mImageHeight; 61 } 62 63 @Override 64 public int getImageWidth() { 65 return mImageWidth; 66 } 67 68 @Override 69 public int getLevelCount() { 70 return mMipmaps.length; 71 } 72 73 @Override 74 public Bitmap getTile(int level, int x, int y, int tileSize, 75 BitmapPool pool) { 76 x >>= level; 77 y >>= level; 78 79 Bitmap result = pool == null ? null : pool.getBitmap(); 80 if (result == null) { 81 result = Bitmap.createBitmap(tileSize, tileSize, mConfig); 82 } else { 83 result.eraseColor(0); 84 } 85 86 Bitmap mipmap = mMipmaps[level]; 87 Canvas canvas = new Canvas(result); 88 int offsetX = -x; 89 int offsetY = -y; 90 canvas.drawBitmap(mipmap, offsetX, offsetY, null); 91 return result; 92 } 93 94 public void recycle() { 95 if (mRecycled) return; 96 mRecycled = true; 97 for (Bitmap bitmap : mMipmaps) { 98 BitmapUtils.recycleSilently(bitmap); 99 } 100 if (mScreenNail != null) { 101 mScreenNail.recycle(); 102 } 103 } 104} 105