Owen Lin | f9a0a43 | 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 | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 19 | import android.graphics.Bitmap; |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 20 | import android.graphics.Bitmap.Config; |
Owen Lin | 2b3ee0e | 2012-03-14 17:27:24 +0800 | [diff] [blame^] | 21 | import android.graphics.Canvas; |
| 22 | |
| 23 | import com.android.gallery3d.common.BitmapUtils; |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 24 | |
| 25 | import java.util.ArrayList; |
| 26 | |
| 27 | public class BitmapTileProvider implements TileImageView.Model { |
| 28 | private final Bitmap mBackup; |
| 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 | |
| 47 | mBackup = list.remove(list.size() - 1); |
| 48 | mMipmaps = list.toArray(new Bitmap[list.size()]); |
| 49 | mConfig = Config.ARGB_8888; |
| 50 | } |
| 51 | |
| 52 | public Bitmap getBackupImage() { |
| 53 | return mBackup; |
| 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 | |
| 68 | public Bitmap getTile(int level, int x, int y, int tileSize) { |
| 69 | Bitmap result = Bitmap.createBitmap(tileSize, tileSize, mConfig); |
| 70 | Canvas canvas = new Canvas(result); |
| 71 | canvas.drawBitmap(mMipmaps[level], -(x >> level), -(y >> level), null); |
| 72 | return result; |
| 73 | } |
| 74 | |
| 75 | public void recycle() { |
| 76 | if (mRecycled) return; |
| 77 | mRecycled = true; |
| 78 | for (Bitmap bitmap : mMipmaps) { |
| 79 | BitmapUtils.recycleSilently(bitmap); |
| 80 | } |
| 81 | BitmapUtils.recycleSilently(mBackup); |
| 82 | } |
| 83 | |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 84 | public boolean isFailedToLoad() { |
| 85 | return false; |
| 86 | } |
| 87 | } |