blob: 870d2795fcfe0f4a0c7f1e2540a7b0c94d1b1964 [file] [log] [blame]
Daniel Olshansky5bcd87e2013-07-15 09:29:51 -07001/*
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.android.insertingcells;
18
19import android.app.Activity;
20import android.content.Context;
21import android.graphics.Bitmap;
22import android.graphics.Bitmap.Config;
23import android.graphics.BitmapFactory;
24import android.graphics.Canvas;
25import android.graphics.Paint;
26import android.graphics.PorterDuff.Mode;
27import android.graphics.PorterDuffXfermode;
28import android.graphics.Rect;
29import android.view.LayoutInflater;
30import android.view.View;
31import android.view.ViewGroup;
32import android.widget.ArrayAdapter;
33import android.widget.ImageView;
34import android.widget.ListView;
35import android.widget.TextView;
36
37import java.util.HashMap;
38import 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 */
46public 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
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
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
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
79 public void addStableIdForDataAtPosition(int position) {
80 mIdMap.put(mData.get(position), ++mCounter);
81 }
82
83 @Override
84 public boolean hasStableIds() {
85 return true;
86 }
87
88 @Override
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 */
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}