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.insertingcells; 18 19 import android.app.Activity; 20 import android.content.Context; 21 import android.graphics.Bitmap; 22 import android.graphics.Bitmap.Config; 23 import android.graphics.BitmapFactory; 24 import android.graphics.Canvas; 25 import android.graphics.Paint; 26 import android.graphics.PorterDuff.Mode; 27 import android.graphics.PorterDuffXfermode; 28 import android.graphics.Rect; 29 import android.view.LayoutInflater; 30 import android.view.View; 31 import android.view.ViewGroup; 32 import android.widget.ArrayAdapter; 33 import android.widget.ImageView; 34 import android.widget.ListView; 35 import android.widget.TextView; 36 37 import java.util.HashMap; 38 import java.util.List; 39 40 /** 41 * This custom array adapter is used to populate the ListView in this application. 42 * This adapter also maintains a map of unique stable ids for each object in the data set. 43 * Since this adapter has to support the addition of a new cell to the 1ist index, it also 44 * provides a mechanism to add a stable ID for new data that was recently inserted. 45 */ 46 public class CustomArrayAdapter extends ArrayAdapter<ListItemObject> { 47 48 HashMap<ListItemObject, Integer> mIdMap = new HashMap<ListItemObject, Integer>(); 49 List<ListItemObject> mData; 50 Context mContext; 51 int mLayoutViewResourceId; 52 int mCounter; 53 CustomArrayAdapter(Context context, int layoutViewResourceId, List <ListItemObject> data)54 public CustomArrayAdapter(Context context, int layoutViewResourceId, 55 List <ListItemObject> data) { 56 super(context, layoutViewResourceId, data); 57 mData = data; 58 mContext = context; 59 mLayoutViewResourceId = layoutViewResourceId; 60 updateStableIds(); 61 } 62 getItemId(int position)63 public long getItemId(int position) { 64 ListItemObject item = getItem(position); 65 if (mIdMap.containsKey(item)) { 66 return mIdMap.get(item); 67 } 68 return -1; 69 } 70 updateStableIds()71 public void updateStableIds() { 72 mIdMap.clear(); 73 mCounter = 0; 74 for (int i = 0; i < mData.size(); ++i) { 75 mIdMap.put(mData.get(i), mCounter++); 76 } 77 } 78 addStableIdForDataAtPosition(int position)79 public void addStableIdForDataAtPosition(int position) { 80 mIdMap.put(mData.get(position), ++mCounter); 81 } 82 83 @Override hasStableIds()84 public boolean hasStableIds() { 85 return true; 86 } 87 88 @Override getView(int position, View convertView, ViewGroup parent)89 public View getView(int position, View convertView, ViewGroup parent) { 90 91 ListItemObject obj = mData.get(position); 92 93 if(convertView == null) { 94 LayoutInflater inflater = ((Activity)mContext).getLayoutInflater(); 95 convertView = inflater.inflate(mLayoutViewResourceId, parent, false); 96 } 97 98 convertView.setLayoutParams(new ListView.LayoutParams(ListView.LayoutParams 99 .MATCH_PARENT, obj.getHeight())); 100 101 ImageView imgView = (ImageView)convertView.findViewById(R.id.image_view); 102 TextView textView = (TextView)convertView.findViewById(R.id.text_view); 103 104 Bitmap bitmap = BitmapFactory.decodeResource(mContext.getResources(), 105 obj.getImgResource(), null); 106 107 textView.setText(obj.getTitle()); 108 imgView.setImageBitmap(CustomArrayAdapter.getCroppedBitmap(bitmap)); 109 110 return convertView; 111 } 112 113 /** 114 * Returns a circular cropped version of the bitmap passed in. 115 */ getCroppedBitmap(Bitmap bitmap)116 public static Bitmap getCroppedBitmap(Bitmap bitmap) { 117 Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), 118 Config.ARGB_8888); 119 120 final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()); 121 122 Canvas canvas = new Canvas(output); 123 124 final Paint paint = new Paint(); 125 paint.setAntiAlias(true); 126 127 int halfWidth = bitmap.getWidth() / 2; 128 int halfHeight = bitmap.getHeight() / 2; 129 130 canvas.drawCircle(halfWidth, halfHeight, Math.max(halfWidth, halfHeight), paint); 131 132 paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN)); 133 134 canvas.drawBitmap(bitmap, rect, rect, paint); 135 136 return output; 137 } 138 }