blob: 1e78cfd334ef45bba30c54eb72c9e30c93e13b21 [file] [log] [blame]
Owen Lina2fba682011-08-17 22:07:43 +08001/*
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
Owen Lina2fba682011-08-17 22:07:43 +080019import android.graphics.Bitmap;
Owen Lina2fba682011-08-17 22:07:43 +080020import android.graphics.Bitmap.Config;
Owen Lin73a04ff2012-03-14 17:27:24 +080021import android.graphics.Canvas;
22
23import com.android.gallery3d.common.BitmapUtils;
Owen Lina2fba682011-08-17 22:07:43 +080024
25import java.util.ArrayList;
26
27public class BitmapTileProvider implements TileImageView.Model {
Chih-Chung Chang706a7cf2012-03-15 16:38:45 +080028 private final ScreenNail mScreenNail;
Owen Lina2fba682011-08-17 22:07:43 +080029 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 Chang706a7cf2012-03-15 16:38:45 +080047 mScreenNail = new BitmapScreenNail(list.remove(list.size() - 1), 0);
Owen Lina2fba682011-08-17 22:07:43 +080048 mMipmaps = list.toArray(new Bitmap[list.size()]);
49 mConfig = Config.ARGB_8888;
50 }
51
Chih-Chung Chang706a7cf2012-03-15 16:38:45 +080052 public ScreenNail getScreenNail() {
53 return mScreenNail;
Owen Lina2fba682011-08-17 22:07:43 +080054 }
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 Chang4a01c6e2012-03-21 16:19:21 +080068 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 Lina2fba682011-08-17 22:07:43 +080075 Canvas canvas = new Canvas(result);
Chih-Chung Chang4a01c6e2012-03-21 16:19:21 +080076 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 Lina2fba682011-08-17 22:07:43 +080090 }
91
92 public void recycle() {
93 if (mRecycled) return;
94 mRecycled = true;
95 for (Bitmap bitmap : mMipmaps) {
96 BitmapUtils.recycleSilently(bitmap);
97 }
Chih-Chung Chang706a7cf2012-03-15 16:38:45 +080098 if (mScreenNail != null) {
Chih-Chung Chang037e06c2012-03-22 17:42:33 +080099 mScreenNail.pauseDraw();
Chih-Chung Chang706a7cf2012-03-15 16:38:45 +0800100 }
Owen Lina2fba682011-08-17 22:07:43 +0800101 }
102
Owen Lina2fba682011-08-17 22:07:43 +0800103 public boolean isFailedToLoad() {
104 return false;
105 }
106}