• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2013 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.example.android.activityanim;
18 
19 import java.util.ArrayList;
20 import java.util.HashMap;
21 
22 import android.content.Context;
23 import android.content.res.Resources;
24 import android.graphics.Bitmap;
25 import android.graphics.BitmapFactory;
26 import android.graphics.drawable.BitmapDrawable;
27 import android.widget.ImageView;
28 
29 public class BitmapUtils {
30 
31     int[] mPhotos = {
32             R.drawable.p1,
33             R.drawable.p2,
34             R.drawable.p3,
35             R.drawable.p4
36     };
37 
38     String[] mDescriptions = {
39             "This picture was taken while sunbathing in a natural hot spring, which was " +
40             "unfortunately filled with acid, which is a lasting memory from that trip, whenever I " +
41             "I look at my own skin.",
42             "I took this shot with a pinhole camera mounted on a tripod constructed out of " +
43             "soda straws. I felt that that combination best captured the beauty of the landscape " +
44             "in juxtaposition with the detritus of mankind.",
45             "I don't remember where or when I took this picture. All I know is that I was really " +
46             "drunk at the time, and I woke up without my left sock.",
47             "Right before I took this picture, there was a busload of school children right " +
48             "in my way. I knew the perfect shot was coming, so I quickly yelled 'Free candy!!!' " +
49             "and they scattered.",
50     };
51 
52     static HashMap<Integer, Bitmap> sBitmapResourceMap = new HashMap<Integer, Bitmap>();
53 
54     /**
55      * Load pictures and descriptions. A real app wouldn't do it this way, but that's
56      * not the point of this animation demo. Loading asynchronously is a better way to go
57      * for what can be time-consuming operations.
58      */
loadPhotos(Resources resources)59     public ArrayList<PictureData> loadPhotos(Resources resources) {
60         ArrayList<PictureData> pictures = new ArrayList<PictureData>();
61         for (int i = 0; i < 30; ++i) {
62             int resourceId = mPhotos[(int) (Math.random() * mPhotos.length)];
63             Bitmap bitmap = getBitmap(resources, resourceId);
64             Bitmap thumbnail = getThumbnail(bitmap, 200);
65             String description = mDescriptions[(int) (Math.random() * mDescriptions.length)];
66             pictures.add(new PictureData(resourceId, description, thumbnail));
67         }
68         return pictures;
69     }
70 
71     /**
72      * Utility method to get bitmap from cache or, if not there, load it
73      * from its resource.
74      */
getBitmap(Resources resources, int resourceId)75     static Bitmap getBitmap(Resources resources, int resourceId) {
76         Bitmap bitmap = sBitmapResourceMap.get(resourceId);
77         if (bitmap == null) {
78             bitmap = BitmapFactory.decodeResource(resources, resourceId);
79             sBitmapResourceMap.put(resourceId, bitmap);
80         }
81         return bitmap;
82     }
83 
84     /**
85      * Create and return a thumbnail image given the original source bitmap and a max
86      * dimension (width or height).
87      */
getThumbnail(Bitmap original, int maxDimension)88     private Bitmap getThumbnail(Bitmap original, int maxDimension) {
89         int width = original.getWidth();
90         int height = original.getHeight();
91         int scaledWidth, scaledHeight;
92         if (width >= height) {
93             float scaleFactor = (float) maxDimension / width;
94             scaledWidth = 200;
95             scaledHeight = (int) (scaleFactor * height);
96         } else {
97             float scaleFactor = (float) maxDimension / height;
98             scaledWidth = (int) (scaleFactor * width);
99             scaledHeight = 200;
100         }
101         Bitmap thumbnail = Bitmap.createScaledBitmap(original, scaledWidth, scaledHeight, true);
102 
103         return thumbnail;
104     }
105 
106 
107 }
108