Owen Lin | a2fba68 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 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 | |
| 17 | package com.android.gallery3d.ui; |
| 18 | |
Owen Lin | a2fba68 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 19 | import android.graphics.Bitmap; |
Owen Lin | a2fba68 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 20 | import android.graphics.Bitmap.Config; |
Owen Lin | 73a04ff | 2012-03-14 17:27:24 +0800 | [diff] [blame] | 21 | import android.graphics.Canvas; |
| 22 | |
| 23 | import com.android.gallery3d.common.BitmapUtils; |
Owen Lin | a2fba68 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 24 | |
| 25 | import java.util.ArrayList; |
| 26 | |
| 27 | public class BitmapTileProvider implements TileImageView.Model { |
Chih-Chung Chang | 706a7cf | 2012-03-15 16:38:45 +0800 | [diff] [blame] | 28 | private final ScreenNail mScreenNail; |
Owen Lin | a2fba68 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 29 | private final Bitmap[] mMipmaps; |
| 30 | private final Config mConfig; |
| 31 | private final int mImageWidth; |
| 32 | private final int mImageHeight; |
| 33 | |
| 34 | private boolean mRecycled = false; |
| 35 | |
| 36 | public BitmapTileProvider(Bitmap bitmap, int maxBackupSize) { |
| 37 | mImageWidth = bitmap.getWidth(); |
| 38 | mImageHeight = bitmap.getHeight(); |
| 39 | ArrayList<Bitmap> list = new ArrayList<Bitmap>(); |
| 40 | list.add(bitmap); |
| 41 | while (bitmap.getWidth() > maxBackupSize |
| 42 | || bitmap.getHeight() > maxBackupSize) { |
| 43 | bitmap = BitmapUtils.resizeBitmapByScale(bitmap, 0.5f, false); |
| 44 | list.add(bitmap); |
| 45 | } |
| 46 | |
Chih-Chung Chang | 706a7cf | 2012-03-15 16:38:45 +0800 | [diff] [blame] | 47 | mScreenNail = new BitmapScreenNail(list.remove(list.size() - 1), 0); |
Owen Lin | a2fba68 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 48 | mMipmaps = list.toArray(new Bitmap[list.size()]); |
| 49 | mConfig = Config.ARGB_8888; |
| 50 | } |
| 51 | |
Chih-Chung Chang | 706a7cf | 2012-03-15 16:38:45 +0800 | [diff] [blame] | 52 | public ScreenNail getScreenNail() { |
| 53 | return mScreenNail; |
Owen Lin | a2fba68 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | public int getImageHeight() { |
| 57 | return mImageHeight; |
| 58 | } |
| 59 | |
| 60 | public int getImageWidth() { |
| 61 | return mImageWidth; |
| 62 | } |
| 63 | |
| 64 | public int getLevelCount() { |
| 65 | return mMipmaps.length; |
| 66 | } |
| 67 | |
Chih-Chung Chang | 4a01c6e | 2012-03-21 16:19:21 +0800 | [diff] [blame] | 68 | public Bitmap getTile(int level, int x, int y, int tileSize, |
| 69 | int borderSize) { |
| 70 | x >>= level; |
| 71 | y >>= level; |
| 72 | int size = tileSize + 2 * borderSize; |
| 73 | Bitmap result = Bitmap.createBitmap(size, size, mConfig); |
| 74 | Bitmap mipmap = mMipmaps[level]; |
Owen Lin | a2fba68 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 75 | Canvas canvas = new Canvas(result); |
Chih-Chung Chang | 4a01c6e | 2012-03-21 16:19:21 +0800 | [diff] [blame] | 76 | int offsetX = -x + borderSize; |
| 77 | int offsetY = -y + borderSize; |
| 78 | canvas.drawBitmap(mipmap, offsetX, offsetY, null); |
| 79 | |
| 80 | // If the valid region (covered by mipmap or border) is smaller than the |
| 81 | // result bitmap, subset it. |
| 82 | int endX = offsetX + mipmap.getWidth() + borderSize; |
| 83 | int endY = offsetY + mipmap.getHeight() + borderSize; |
| 84 | if (endX < size || endY < size) { |
| 85 | return Bitmap.createBitmap(result, 0, 0, Math.min(size, endX), |
| 86 | Math.min(size, endY)); |
| 87 | } else { |
| 88 | return result; |
| 89 | } |
Owen Lin | a2fba68 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | public void recycle() { |
| 93 | if (mRecycled) return; |
| 94 | mRecycled = true; |
| 95 | for (Bitmap bitmap : mMipmaps) { |
| 96 | BitmapUtils.recycleSilently(bitmap); |
| 97 | } |
Chih-Chung Chang | 706a7cf | 2012-03-15 16:38:45 +0800 | [diff] [blame] | 98 | if (mScreenNail != null) { |
Chih-Chung Chang | 037e06c | 2012-03-22 17:42:33 +0800 | [diff] [blame^] | 99 | mScreenNail.pauseDraw(); |
Chih-Chung Chang | 706a7cf | 2012-03-15 16:38:45 +0800 | [diff] [blame] | 100 | } |
Owen Lin | a2fba68 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 101 | } |
| 102 | |
Owen Lin | a2fba68 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 103 | public boolean isFailedToLoad() { |
| 104 | return false; |
| 105 | } |
| 106 | } |