• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
19 import android.graphics.Bitmap;
20 import android.graphics.Bitmap.Config;
21 import android.graphics.Canvas;
22 
23 import com.android.gallery3d.common.BitmapUtils;
24 import com.android.gallery3d.data.BitmapPool;
25 
26 import java.util.ArrayList;
27 
28 public class BitmapTileProvider implements TileImageView.Model {
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 
BitmapTileProvider(Bitmap bitmap, int maxBackupSize)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
getScreenNail()54     public ScreenNail getScreenNail() {
55         return mScreenNail;
56     }
57 
58     @Override
getImageHeight()59     public int getImageHeight() {
60         return mImageHeight;
61     }
62 
63     @Override
getImageWidth()64     public int getImageWidth() {
65         return mImageWidth;
66     }
67 
68     @Override
getLevelCount()69     public int getLevelCount() {
70         return mMipmaps.length;
71     }
72 
73     @Override
getTile(int level, int x, int y, int tileSize, int borderSize, BitmapPool pool)74     public Bitmap getTile(int level, int x, int y, int tileSize,
75             int borderSize, BitmapPool pool) {
76         x >>= level;
77         y >>= level;
78         int size = tileSize + 2 * borderSize;
79 
80         Bitmap result = pool == null ? null : pool.getBitmap();
81         if (result == null) {
82             result = Bitmap.createBitmap(size, size, mConfig);
83         } else {
84             result.eraseColor(0);
85         }
86 
87         Bitmap mipmap = mMipmaps[level];
88         Canvas canvas = new Canvas(result);
89         int offsetX = -x + borderSize;
90         int offsetY = -y + borderSize;
91         canvas.drawBitmap(mipmap, offsetX, offsetY, null);
92         return result;
93     }
94 
recycle()95     public void recycle() {
96         if (mRecycled) return;
97         mRecycled = true;
98         for (Bitmap bitmap : mMipmaps) {
99             BitmapUtils.recycleSilently(bitmap);
100         }
101         if (mScreenNail != null) {
102             mScreenNail.recycle();
103         }
104     }
105 }
106