BitmapTileProvider.java revision f5ce6aeba448f418c99736465f7a02dacd7715bb
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;
24
25import java.util.ArrayList;
26
27public class BitmapTileProvider implements TileImageView.Model {
28    private final ScreenNail mScreenNail;
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        mScreenNail = new BitmapScreenNail(list.remove(list.size() - 1));
48        mMipmaps = list.toArray(new Bitmap[list.size()]);
49        mConfig = Config.ARGB_8888;
50    }
51
52    public ScreenNail getScreenNail() {
53        return mScreenNail;
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            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];
75        Canvas canvas = new Canvas(result);
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        }
90    }
91
92    public void recycle() {
93        if (mRecycled) return;
94        mRecycled = true;
95        for (Bitmap bitmap : mMipmaps) {
96            BitmapUtils.recycleSilently(bitmap);
97        }
98        if (mScreenNail != null) {
99            mScreenNail.recycle();
100        }
101    }
102}
103