MainActivity.java revision 25ad6f98516a7af1ca71cfa07fb503d46dc8a7f1
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
17package com.example.bitmapsample;
18
19import android.app.Activity;
20import android.os.Bundle;
21import android.view.View;
22import android.view.ViewGroup;
23import android.widget.BaseAdapter;
24import android.widget.ListView;
25
26import com.android.bitmap.BitmapCache;
27import com.android.bitmap.UnrefedBitmapCache;
28import com.android.bitmap.util.Trace;
29
30public class MainActivity extends Activity {
31    private ListView mListView;
32
33    private static final int TARGET_CACHE_SIZE_BYTES = 5 * 1024 * 1024;
34    private final BitmapCache mCache = new UnrefedBitmapCache(TARGET_CACHE_SIZE_BYTES, 0.1f, 0);
35
36    @Override
37    protected void onCreate(Bundle savedInstanceState) {
38        super.onCreate(savedInstanceState);
39        setContentView(R.layout.activity_main);
40        Trace.init();
41        mListView = (ListView) findViewById(R.id.list);
42        mListView.setAdapter(new MyAdapter());
43    }
44
45    private class MyAdapter extends BaseAdapter {
46
47        private final String[] mItems;
48
49        private final String[] ITEMS = new String[]{
50                "https://www.google.com/images/srpr/logo4w.png",
51                "http://www.google.com/logos/2012/celibidache12-hp.jpg",
52                "http://www.google.com/logos/2012/clara_schuman-2012-hp.jpg",
53                "http://www.google.com/logos/2011/royalwedding11-hp.png",
54                "http://www.google.com/logos/2012/vets_day-12-hp.jpg",
55                "http://www.google.com/logos/2011/firstmaninspace11-hp-js.jpg",
56                "http://www.google.com/logos/2011/nat-sov-and-childrens-turkey-hp.png",
57                "http://www.google.com/logos/2012/First_Day_Of_School_Isreal-2012-hp.jpg",
58                "http://www.google.com/logos/2012/celibidache12-hp.jpg",
59                "http://www.google.com/logos/2012/korea12-hp.png"
60        };
61
62        private static final int COPIES = 50;
63
64        public MyAdapter() {
65            mItems = new String[ITEMS.length * COPIES];
66            for (int i = 0; i < COPIES; i++) {
67                for (int j = 0; j < ITEMS.length; j++) {
68                    mItems[i * ITEMS.length + j] = ITEMS[j];
69                }
70            }
71        }
72
73        @Override
74        public int getCount() {
75            return mItems.length;
76        }
77
78        @Override
79        public Object getItem(int position) {
80            return mItems[position];
81        }
82
83        @Override
84        public long getItemId(int position) {
85            return position;
86        }
87
88        @Override
89        public View getView(int position, View convertView, ViewGroup parent) {
90            BitmapView v;
91            if (convertView != null) {
92                v = (BitmapView) convertView;
93            } else {
94                v = new BitmapView(MainActivity.this);
95                v.initialize(mCache);
96            }
97            v.setImage(mItems[position]);
98            return v;
99        }
100    }
101}
102