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.bitmapsample;
18 
19 import android.app.Activity;
20 import android.content.res.Resources;
21 import android.graphics.Color;
22 import android.graphics.drawable.Drawable;
23 import android.os.Bundle;
24 import android.view.View;
25 import android.view.ViewGroup;
26 import android.widget.BaseAdapter;
27 import android.widget.ListView;
28 
29 import com.android.bitmap.BitmapCache;
30 import com.android.bitmap.DecodeAggregator;
31 import com.android.bitmap.UnrefedBitmapCache;
32 import com.android.bitmap.drawable.ExtendedBitmapDrawable;
33 import com.android.bitmap.drawable.ExtendedBitmapDrawable.ExtendedOptions;
34 
35 public class MainActivity extends Activity {
36 
37     private ListView mListView;
38     private final BitmapCache mCache = new UnrefedBitmapCache(TARGET_CACHE_SIZE_BYTES, 0, 0);
39     private final DecodeAggregator mDecodeAggregator = new DecodeAggregator();
40 
41     private static Drawable PLACEHOLDER;
42     private static Drawable PROGRESS;
43 
44     private static final float NORMAL_PARALLAX_MULTIPLIER = 1.5f;
45     private static final int TARGET_CACHE_SIZE_BYTES = 5 * 1024 * 1024;
46 
47     @Override
onCreate(Bundle savedInstanceState)48     protected void onCreate(Bundle savedInstanceState) {
49         super.onCreate(savedInstanceState);
50         setContentView(R.layout.activity_main);
51 
52         if (PLACEHOLDER == null) {
53             Resources res = getResources();
54             PLACEHOLDER = res.getDrawable(R.drawable.ic_placeholder);
55             PROGRESS = res.getDrawable(R.drawable.progress);
56         }
57 
58         mListView = (ListView) findViewById(R.id.list);
59         mListView.setAdapter(new MyAdapter());
60     }
61 
62     private class MyAdapter extends BaseAdapter {
63 
64         private final String[] mItems;
65 
66         private final String[] ITEMS = new String[]{
67                 "https://www.google.com/images/srpr/logo4w.png",
68                 "http://www.google.com/logos/2012/celibidache12-hp.jpg",
69                 "http://www.google.com/logos/2012/clara_schuman-2012-hp.jpg",
70                 "http://www.google.com/logos/2011/royalwedding11-hp.png",
71                 "http://www.google.com/logos/2012/vets_day-12-hp.jpg",
72                 "http://www.google.com/logos/2011/firstmaninspace11-hp-js.jpg",
73                 "http://www.google.com/logos/2011/nat-sov-and-childrens-turkey-hp.png",
74                 "http://www.google.com/logos/2012/First_Day_Of_School_Isreal-2012-hp.jpg",
75                 "http://www.google.com/logos/2012/celibidache12-hp.jpg",
76                 "http://www.google.com/logos/2012/korea12-hp.png"
77         };
78 
79         private static final int COPIES = 50;
80 
MyAdapter()81         public MyAdapter() {
82             mItems = new String[ITEMS.length * COPIES];
83             for (int i = 0; i < COPIES; i++) {
84                 for (int j = 0; j < ITEMS.length; j++) {
85                     mItems[i * ITEMS.length + j] = ITEMS[j];
86                 }
87             }
88         }
89 
90         @Override
getCount()91         public int getCount() {
92             return mItems.length;
93         }
94 
95         @Override
getItem(int position)96         public Object getItem(int position) {
97             return mItems[position];
98         }
99 
100         @Override
getItemId(int position)101         public long getItemId(int position) {
102             return position;
103         }
104 
105         @Override
getView(int position, View convertView, ViewGroup parent)106         public View getView(int position, View convertView, ViewGroup parent) {
107             BitmapView v;
108             if (convertView != null) {
109                 v = (BitmapView) convertView;
110             } else {
111                 v = new BitmapView(MainActivity.this);
112                 ExtendedOptions opts = new ExtendedOptions(
113                         ExtendedOptions.FEATURE_ORDERED_DISPLAY | ExtendedOptions.FEATURE_PARALLAX
114                                 | ExtendedOptions.FEATURE_STATE_CHANGES, PLACEHOLDER, PROGRESS);
115                 opts.decodeAggregator = mDecodeAggregator;
116                 opts.parallaxSpeedMultiplier = NORMAL_PARALLAX_MULTIPLIER;
117                 opts.backgroundColor = Color.LTGRAY;
118                 final ExtendedBitmapDrawable drawable = new ExtendedBitmapDrawable(getResources(),
119                         mCache, true /* limit density */, opts);
120 
121                 v.setTypedDrawable(drawable);
122                 v.setListView(mListView);
123             }
124             v.getTypedDrawable().bind(new BitmapRequestKeyImpl(mItems[position]));
125             return v;
126         }
127     }
128 }
129