Skip to content

Commit d973a3d

Browse files
committed
Initial commit
1 parent 0ec4252 commit d973a3d

File tree

5 files changed

+42
-24
lines changed

5 files changed

+42
-24
lines changed

.gitignore

100644100755
File mode changed.

LICENSE

100644100755
File mode changed.

README.md

100644100755
File mode changed.

app/src/main/java/com/wuyr/pathlayoutmanagertest/MainActivity.java

+7-10
Original file line numberDiff line numberDiff line change
@@ -84,17 +84,14 @@ public void handleOnClick(View view) {
8484
if (path != null && !path.isEmpty()) {
8585
mCanvasView.setVisibility(View.INVISIBLE);
8686
mTrackView.setPath(mCanvasView.getPath());
87-
if (isShowPath) {
88-
mTrackView.setVisibility(View.VISIBLE);
89-
}
90-
87+
mTrackView.setVisibility(isShowPath ? View.VISIBLE : View.INVISIBLE);
9188
mPathLayoutManager.updatePath(mCanvasView.getPath());
9289
}
9390
break;
9491
case R.id.add:
95-
List<String> data = new ArrayList<>();
96-
for (int i = mAdapter.getItemCount(); i < mAdapter.getItemCount() + 10; i++) {
97-
data.add("" + i);
92+
List<Object> data = new ArrayList<>();
93+
for (int i = 0; i < 10; i++) {
94+
data.add(null);
9895
}
9996
mAdapter.addData(data);
10097
break;
@@ -159,11 +156,11 @@ public void handleOnClick(View view) {
159156
}
160157
break;
161158
case R.id.cache_count:
162-
try{
163-
int count = Integer.parseInt(((TextView)findViewById(R.id.cache_count_text)).getText().toString());
159+
try {
160+
int count = Integer.parseInt(((TextView) findViewById(R.id.cache_count_text)).getText().toString());
164161
mPathLayoutManager.setCacheCount(count);
165162
mToast.setText(R.string.success);
166-
}catch (Exception e){
163+
} catch (Exception e) {
167164
mToast.setText(e.toString());
168165
}
169166
mToast.show();

app/src/main/java/com/wuyr/pathlayoutmanagertest/PathAdapter.java

+35-14
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,15 @@
2020
/**
2121
* Created by wuyr on 18-5-20 上午4:09.
2222
*/
23-
public class PathAdapter extends BaseAdapter<String, PathAdapter.ViewHolder> {
23+
public class PathAdapter extends BaseAdapter<Object, PathAdapter.ViewHolder> {
2424

2525
public static final int TYPE_CARD = 0, TYPE_J20 = 1, TYPE_DRAGON = 2;
2626
private Toast mToast;
2727
private int mCurrentType;
2828
private Random mRandom = new Random();
2929
private List<SoftReference<Bitmap>> mBitmapList;
3030

31-
public PathAdapter(Context context, List<String> data) {
31+
public PathAdapter(Context context, List<Object> data) {
3232
super(context, data, R.layout.adapter_item_view, ViewHolder.class);
3333
mToast = Toast.makeText(context, "", Toast.LENGTH_SHORT);
3434
initBitmaps();
@@ -94,7 +94,6 @@ private SoftReference<Bitmap> getBitmapById(int id) {
9494
return new SoftReference<>(decodeSampledBitmapFromResource(mContext.getResources(), id));
9595
}
9696

97-
9897
@Override
9998
public void onDetachedFromRecyclerView(@NonNull RecyclerView recyclerView) {
10099
super.onDetachedFromRecyclerView(recyclerView);
@@ -162,11 +161,13 @@ private void initDragonHolder(ViewHolder holder, int position) {
162161

163162
holder.imageView2.getLayoutParams().width = 135;
164163
holder.imageView2.requestLayout();
165-
if (position == 0) {
164+
if (position == 10) {
166165
holder.imageView2.setImageBitmap(getBitmap(7));
166+
} else if (position < 10) {
167+
holder.imageView2.setImageBitmap(null);
167168
} else if (position == mData.size() - 1) {
168169
holder.imageView2.setImageBitmap(getBitmap(4));
169-
} else {
170+
} else if (position < mData.size() - 1) {
170171
holder.imageView2.setImageBitmap(getBitmap(mRandom.nextBoolean() ? 5 : 6));
171172
}
172173
}
@@ -181,9 +182,36 @@ private Bitmap getBitmap(int index) {
181182
}
182183

183184
public void setType(int type) {
184-
mCurrentType = type;
185-
notifyItemRangeChanged(0, getItemCount());
185+
if (mCurrentType != type) {
186+
if (type == TYPE_DRAGON) {
187+
List<Object> tempList = new ArrayList<>();
188+
for (int i = 0; i < 10; i++) {
189+
tempList.add(null);
190+
}
191+
mData.addAll(tempList);
192+
} else {
193+
if (mCurrentType == TYPE_DRAGON) {
194+
for (int i = 0; i < 10; i++) {
195+
int index = mData.size() - 1;
196+
if (index >= 0) {
197+
mData.remove(index);
198+
}
199+
}
200+
}
201+
}
202+
mCurrentType = type;
203+
notifyItemRangeChanged(0, getItemCount());
204+
}
186205
}
206+
207+
@Override
208+
public void addData(@NonNull List<Object> data) {
209+
super.addData(data);
210+
if (mCurrentType == TYPE_DRAGON && mData.size() < 20) {
211+
super.addData(data);
212+
}
213+
}
214+
187215
private int calculateInSampleSize(BitmapFactory.Options options) {
188216
int reqWidth = 0;
189217
int reqHeight = 0;
@@ -203,29 +231,22 @@ private int calculateInSampleSize(BitmapFactory.Options options) {
203231
default:
204232
break;
205233
}
206-
// 源图片的高度和宽度
207234
final int height = options.outHeight;
208235
final int width = options.outWidth;
209236
int inSampleSize = 1;
210237
if (height > reqHeight || width > reqWidth) {
211-
// 计算出实际宽高和目标宽高的比率
212238
final int heightRatio = Math.round((float) height / (float) reqHeight);
213239
final int widthRatio = Math.round((float) width / (float) reqWidth);
214-
// 选择宽和高中最小的比率作为inSampleSize的值,这样可以保证最终图片的宽和高
215-
// 一定都会大于等于目标的宽和高。
216240
inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
217241
}
218242
return inSampleSize;
219243
}
220244

221245
private Bitmap decodeSampledBitmapFromResource(Resources res, int resId) {
222-
// 第一次解析将inJustDecodeBounds设置为true,来获取图片大小
223246
final BitmapFactory.Options options = new BitmapFactory.Options();
224247
options.inJustDecodeBounds = true;
225248
BitmapFactory.decodeResource(res, resId, options);
226-
// 调用上面定义的方法计算inSampleSize值
227249
options.inSampleSize = calculateInSampleSize(options);
228-
// 使用获取到的inSampleSize值再次解析图片
229250
options.inJustDecodeBounds = false;
230251
try {
231252
return BitmapFactory.decodeResource(res, resId, options);

0 commit comments

Comments
 (0)