1
1
package com .wuyr .pathlayoutmanagertest ;
2
2
3
3
import android .content .Context ;
4
+ import android .content .res .Resources ;
4
5
import android .graphics .Bitmap ;
5
6
import android .graphics .BitmapFactory ;
6
7
import android .support .annotation .NonNull ;
8
+ import android .support .v7 .widget .CardView ;
7
9
import android .support .v7 .widget .RecyclerView ;
8
10
import android .view .View ;
9
11
import android .widget .ImageView ;
@@ -29,25 +31,70 @@ public class PathAdapter extends BaseAdapter<String, PathAdapter.ViewHolder> {
29
31
public PathAdapter (Context context , List <String > data ) {
30
32
super (context , data , R .layout .adapter_item_view , ViewHolder .class );
31
33
mToast = Toast .makeText (context , "" , Toast .LENGTH_SHORT );
34
+ initBitmaps ();
32
35
}
33
36
34
37
private void initBitmaps () {
35
- mBitmapList = new ArrayList <>();
36
- mBitmapList .add (getBitmapById (R .drawable .ic_1 ));
37
- mBitmapList .add (getBitmapById (R .drawable .ic_2 ));
38
- mBitmapList .add (getBitmapById (R .drawable .ic_3 ));
39
- mBitmapList .add (getBitmapById (R .drawable .ic_j20 ));
40
- mBitmapList .add (getBitmapById (R .drawable .ic_dragon_head ));
41
- mBitmapList .add (getBitmapById (R .drawable .ic_dragon_body_1 ));
42
- mBitmapList .add (getBitmapById (R .drawable .ic_dragon_body_2 ));
43
- mBitmapList .add (getBitmapById (R .drawable .ic_dragon_tail ));
38
+ if (mBitmapList != null ) {
39
+ for (int i = 0 ; i < mBitmapList .size (); i ++) {
40
+ SoftReference <Bitmap > softReference = mBitmapList .get (i );
41
+ if (softReference .get () == null ) {
42
+ mBitmapList .remove (i );
43
+ mBitmapList .add (i , getBitmapById (getIdByIndex (i )));
44
+ }
45
+ }
46
+ } else {
47
+ mBitmapList = new ArrayList <>();
48
+ mBitmapList .add (getBitmapById (R .drawable .ic_1 ));
49
+ mBitmapList .add (getBitmapById (R .drawable .ic_2 ));
50
+ mBitmapList .add (getBitmapById (R .drawable .ic_3 ));
51
+ mBitmapList .add (getBitmapById (R .drawable .ic_j20 ));
52
+ mBitmapList .add (getBitmapById (R .drawable .ic_dragon_head ));
53
+ mBitmapList .add (getBitmapById (R .drawable .ic_dragon_body_1 ));
54
+ mBitmapList .add (getBitmapById (R .drawable .ic_dragon_body_2 ));
55
+ mBitmapList .add (getBitmapById (R .drawable .ic_dragon_tail ));
56
+ }
57
+ }
58
+
59
+ private int getIdByIndex (int index ) {
60
+ int id = -1 ;
61
+ switch (index ) {
62
+ case 0 :
63
+ id = R .drawable .ic_1 ;
64
+ break ;
65
+ case 1 :
66
+ id = R .drawable .ic_2 ;
67
+ break ;
68
+ case 2 :
69
+ id = R .drawable .ic_3 ;
70
+ break ;
71
+ case 3 :
72
+ id = R .drawable .ic_j20 ;
73
+ break ;
74
+ case 4 :
75
+ id = R .drawable .ic_dragon_head ;
76
+ break ;
77
+ case 5 :
78
+ id = R .drawable .ic_dragon_body_1 ;
79
+ break ;
80
+ case 6 :
81
+ id = R .drawable .ic_dragon_body_2 ;
82
+ break ;
83
+ case 7 :
84
+ id = R .drawable .ic_dragon_tail ;
85
+ break ;
86
+ default :
87
+ break ;
88
+ }
89
+ return id ;
44
90
}
45
91
46
92
@ NonNull
47
93
private SoftReference <Bitmap > getBitmapById (int id ) {
48
- return new SoftReference <>(BitmapFactory . decodeResource (mContext .getResources (), id ));
94
+ return new SoftReference <>(decodeSampledBitmapFromResource (mContext .getResources (), id ));
49
95
}
50
96
97
+
51
98
@ Override
52
99
public void onDetachedFromRecyclerView (@ NonNull RecyclerView recyclerView ) {
53
100
super .onDetachedFromRecyclerView (recyclerView );
@@ -82,55 +129,122 @@ public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
82
129
break ;
83
130
}
84
131
holder .itemView .setOnClickListener (v -> {
85
- mToast .setText (String .format (Locale .getDefault (), "item %s clicked" , holder .getAdapterPosition ()));
132
+ mToast .setText (String .format (Locale .getDefault (),
133
+ "item %s clicked" , holder .getAdapterPosition ()));
86
134
mToast .show ();
87
135
});
88
136
}
89
137
90
138
private void initCardHolder (ViewHolder holder ) {
139
+ holder .imageView .setVisibility (View .VISIBLE );
140
+ holder .imageView2 .setImageBitmap (null );
141
+ holder .imageView2 .setVisibility (View .GONE );
142
+
91
143
holder .imageView .getLayoutParams ().width = 360 ;
92
- holder .itemView .requestLayout ();
144
+ holder .imageView .requestLayout ();
93
145
holder .imageView .setImageBitmap (getBitmap (mRandom .nextInt (3 )));
94
146
}
95
147
96
148
private void initJ20Holder (ViewHolder holder ) {
97
- holder .imageView .getLayoutParams ().width = 270 ;
98
- holder .itemView .requestLayout ();
99
- holder .imageView .setImageBitmap (getBitmap (3 ));
149
+ holder .imageView2 .setVisibility (View .VISIBLE );
150
+ holder .imageView .setImageBitmap (null );
151
+ holder .imageView .setVisibility (View .GONE );
152
+
153
+ holder .imageView2 .getLayoutParams ().width = 180 ;
154
+ holder .imageView2 .requestLayout ();
155
+ holder .imageView2 .setImageBitmap (getBitmap (3 ));
100
156
}
101
157
102
158
private void initDragonHolder (ViewHolder holder , int position ) {
103
- holder .imageView .getLayoutParams ().width = 135 ;
104
- holder .itemView .requestLayout ();
159
+ holder .imageView2 .setVisibility (View .VISIBLE );
160
+ holder .imageView .setImageBitmap (null );
161
+ holder .imageView .setVisibility (View .GONE );
162
+
163
+ holder .imageView2 .getLayoutParams ().width = 135 ;
164
+ holder .imageView2 .requestLayout ();
105
165
if (position == 0 ) {
106
- holder .imageView .setImageBitmap (getBitmap (7 ));
166
+ holder .imageView2 .setImageBitmap (getBitmap (7 ));
107
167
} else if (position == mData .size () - 1 ) {
108
- holder .imageView .setImageBitmap (getBitmap (4 ));
168
+ holder .imageView2 .setImageBitmap (getBitmap (4 ));
109
169
} else {
110
- holder .imageView .setImageBitmap (getBitmap (mRandom .nextBoolean () ? 5 : 6 ));
170
+ holder .imageView2 .setImageBitmap (getBitmap (mRandom .nextBoolean () ? 5 : 6 ));
111
171
}
112
172
}
113
173
114
174
private Bitmap getBitmap (int index ) {
115
- if (mBitmapList == null ) {
175
+ Bitmap bitmap = mBitmapList .get (index ).get ();
176
+ if (bitmap == null ) {
116
177
initBitmaps ();
178
+ return mBitmapList .get (index ).get ();
117
179
}
118
- Bitmap bitmap = mBitmapList .get (index ).get ();
119
180
return bitmap ;
120
181
}
121
182
122
183
public void setType (int type ) {
123
184
mCurrentType = type ;
124
- notifyDataSetChanged ();
185
+ notifyItemRangeChanged (0 , getItemCount ());
186
+ }
187
+ private int calculateInSampleSize (BitmapFactory .Options options ) {
188
+ int reqWidth = 0 ;
189
+ int reqHeight = 0 ;
190
+ switch (mCurrentType ) {
191
+ case TYPE_CARD :
192
+ reqWidth = 180 ;
193
+ reqHeight = 180 ;
194
+ break ;
195
+ case TYPE_J20 :
196
+ reqWidth = 135 ;
197
+ reqHeight = 208 ;
198
+ break ;
199
+ case TYPE_DRAGON :
200
+ reqWidth = 68 ;
201
+ reqHeight = 116 ;
202
+ break ;
203
+ default :
204
+ break ;
205
+ }
206
+ // 源图片的高度和宽度
207
+ final int height = options .outHeight ;
208
+ final int width = options .outWidth ;
209
+ int inSampleSize = 1 ;
210
+ if (height > reqHeight || width > reqWidth ) {
211
+ // 计算出实际宽高和目标宽高的比率
212
+ final int heightRatio = Math .round ((float ) height / (float ) reqHeight );
213
+ final int widthRatio = Math .round ((float ) width / (float ) reqWidth );
214
+ // 选择宽和高中最小的比率作为inSampleSize的值,这样可以保证最终图片的宽和高
215
+ // 一定都会大于等于目标的宽和高。
216
+ inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio ;
217
+ }
218
+ return inSampleSize ;
219
+ }
220
+
221
+ private Bitmap decodeSampledBitmapFromResource (Resources res , int resId ) {
222
+ // 第一次解析将inJustDecodeBounds设置为true,来获取图片大小
223
+ final BitmapFactory .Options options = new BitmapFactory .Options ();
224
+ options .inJustDecodeBounds = true ;
225
+ BitmapFactory .decodeResource (res , resId , options );
226
+ // 调用上面定义的方法计算inSampleSize值
227
+ options .inSampleSize = calculateInSampleSize (options );
228
+ // 使用获取到的inSampleSize值再次解析图片
229
+ options .inJustDecodeBounds = false ;
230
+ try {
231
+ return BitmapFactory .decodeResource (res , resId , options );
232
+ } catch (Exception e ) {
233
+ return null ;
234
+ }
125
235
}
126
236
127
237
static class ViewHolder extends RecyclerView .ViewHolder {
128
238
239
+ CardView cardView ;
129
240
ImageView imageView ;
241
+ ImageView imageView2 ;
130
242
131
243
public ViewHolder (View itemView ) {
132
244
super (itemView );
245
+ cardView = itemView .findViewById (R .id .card );
133
246
imageView = itemView .findViewById (R .id .image );
247
+ imageView2 = itemView .findViewById (R .id .image2 );
134
248
}
135
249
}
136
250
}
0 commit comments