MainActivity.java revision 9c6ac19d4a3d39b7c2992060957920118ff56a65
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.drawable.BasicBitmapDrawable; 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 mListView = (ListView) findViewById(R.id.list); 41 mListView.setAdapter(new MyAdapter()); 42 } 43 44 private class MyAdapter extends BaseAdapter { 45 46 private final String[] mItems; 47 48 private final String[] ITEMS = new String[]{ 49 "https://www.google.com/images/srpr/logo4w.png", 50 "http://www.google.com/logos/2012/celibidache12-hp.jpg", 51 "http://www.google.com/logos/2012/clara_schuman-2012-hp.jpg", 52 "http://www.google.com/logos/2011/royalwedding11-hp.png", 53 "http://www.google.com/logos/2012/vets_day-12-hp.jpg", 54 "http://www.google.com/logos/2011/firstmaninspace11-hp-js.jpg", 55 "http://www.google.com/logos/2011/nat-sov-and-childrens-turkey-hp.png", 56 "http://www.google.com/logos/2012/First_Day_Of_School_Isreal-2012-hp.jpg", 57 "http://www.google.com/logos/2012/celibidache12-hp.jpg", 58 "http://www.google.com/logos/2012/korea12-hp.png" 59 }; 60 61 private static final int COPIES = 50; 62 63 public MyAdapter() { 64 mItems = new String[ITEMS.length * COPIES]; 65 for (int i = 0; i < COPIES; i++) { 66 for (int j = 0; j < ITEMS.length; j++) { 67 mItems[i * ITEMS.length + j] = ITEMS[j]; 68 } 69 } 70 } 71 72 @Override 73 public int getCount() { 74 return mItems.length; 75 } 76 77 @Override 78 public Object getItem(int position) { 79 return mItems[position]; 80 } 81 82 @Override 83 public long getItemId(int position) { 84 return position; 85 } 86 87 @Override 88 public View getView(int position, View convertView, ViewGroup parent) { 89 BitmapView v; 90 if (convertView != null) { 91 v = (BitmapView) convertView; 92 } else { 93 v = new BitmapView(MainActivity.this); 94 final BasicBitmapDrawable drawable = new BasicBitmapDrawable(getResources(), mCache, 95 true /* limit density */); 96 v.setBasicBitmapDrawable(drawable); 97 } 98 v.getBasicBitmapDrawable().bind(new BitmapRequestKeyImpl(mItems[position])); 99 return v; 100 } 101 } 102} 103