Skip to content

Commit 0ec4252

Browse files
committed
Initial commit
1 parent 9532df1 commit 0ec4252

23 files changed

+541
-294
lines changed

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

+19-5
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,12 @@ this, findViewById(R.id.drawer), findViewById(R.id.toolbar),
6363
((Switch) findViewById(R.id.orientation)).setOnCheckedChangeListener(this);
6464
((Switch) findViewById(R.id.direction_fixed)).setOnCheckedChangeListener(this);
6565
((Switch) findViewById(R.id.auto_select)).setOnCheckedChangeListener(this);
66+
((Switch) findViewById(R.id.disable_fling)).setOnCheckedChangeListener(this);
67+
((Switch) findViewById(R.id.show_path)).setOnCheckedChangeListener(this);
6668

6769
((SeekBar) findViewById(R.id.item_offset)).setOnSeekBarChangeListener(this);
6870
((SeekBar) findViewById(R.id.auto_select_fraction)).setOnSeekBarChangeListener(this);
71+
((SeekBar) findViewById(R.id.fixing_animation_duration)).setOnSeekBarChangeListener(this);
6972
}
7073

7174
public void handleOnClick(View view) {
@@ -100,7 +103,6 @@ public void handleOnClick(View view) {
100103
mAdapter.removeData(mAdapter.getItemCount() - 1);
101104
}
102105
break;
103-
104106
case R.id.card:
105107
view.setEnabled(false);
106108
findViewById(R.id.j20).setEnabled(true);
@@ -119,7 +121,6 @@ public void handleOnClick(View view) {
119121
findViewById(R.id.j20).setEnabled(true);
120122
mAdapter.setType(PathAdapter.TYPE_DRAGON);
121123
break;
122-
123124
case R.id.normal:
124125
view.setEnabled(false);
125126
findViewById(R.id.overflow).setEnabled(true);
@@ -138,7 +139,6 @@ public void handleOnClick(View view) {
138139
findViewById(R.id.normal).setEnabled(true);
139140
mPathLayoutManager.setScrollMode(PathLayoutManager.SCROLL_MODE_LOOP);
140141
break;
141-
142142
case R.id.apply_scale_ratio:
143143
String content = ((TextView) findViewById(R.id.scale_ratio_text)).getText().toString();
144144
if (TextUtils.isEmpty(content)) {
@@ -151,12 +151,23 @@ public void handleOnClick(View view) {
151151
ratios[i] = Float.parseFloat(ratiosString[i]);
152152
}
153153
mPathLayoutManager.setItemScaleRatio(ratios);
154+
mToast.setText(R.string.success);
154155
} catch (Exception e) {
155156
mToast.setText(e.toString());
156-
mToast.show();
157157
}
158+
mToast.show();
158159
}
159160
break;
161+
case R.id.cache_count:
162+
try{
163+
int count = Integer.parseInt(((TextView)findViewById(R.id.cache_count_text)).getText().toString());
164+
mPathLayoutManager.setCacheCount(count);
165+
mToast.setText(R.string.success);
166+
}catch (Exception e){
167+
mToast.setText(e.toString());
168+
}
169+
mToast.show();
170+
break;
160171
default:
161172
break;
162173
}
@@ -194,7 +205,6 @@ public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
194205

195206
@Override
196207
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
197-
mToast.cancel();
198208
switch (seekBar.getId()) {
199209
case R.id.item_offset:
200210
mPathLayoutManager.setItemOffset(progress);
@@ -205,6 +215,10 @@ public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
205215
mPathLayoutManager.setAutoSelectFraction(fraction);
206216
mToast.setText(String.valueOf(fraction));
207217
break;
218+
case R.id.fixing_animation_duration:
219+
mPathLayoutManager.setFixingAnimationDuration(progress);
220+
mToast.setText(String.valueOf(progress));
221+
break;
208222
default:
209223
break;
210224
}

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

+137-23
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package com.wuyr.pathlayoutmanagertest;
22

33
import android.content.Context;
4+
import android.content.res.Resources;
45
import android.graphics.Bitmap;
56
import android.graphics.BitmapFactory;
67
import android.support.annotation.NonNull;
8+
import android.support.v7.widget.CardView;
79
import android.support.v7.widget.RecyclerView;
810
import android.view.View;
911
import android.widget.ImageView;
@@ -29,25 +31,70 @@ public class PathAdapter extends BaseAdapter<String, PathAdapter.ViewHolder> {
2931
public PathAdapter(Context context, List<String> data) {
3032
super(context, data, R.layout.adapter_item_view, ViewHolder.class);
3133
mToast = Toast.makeText(context, "", Toast.LENGTH_SHORT);
34+
initBitmaps();
3235
}
3336

3437
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;
4490
}
4591

4692
@NonNull
4793
private SoftReference<Bitmap> getBitmapById(int id) {
48-
return new SoftReference<>(BitmapFactory.decodeResource(mContext.getResources(), id));
94+
return new SoftReference<>(decodeSampledBitmapFromResource(mContext.getResources(), id));
4995
}
5096

97+
5198
@Override
5299
public void onDetachedFromRecyclerView(@NonNull RecyclerView recyclerView) {
53100
super.onDetachedFromRecyclerView(recyclerView);
@@ -82,55 +129,122 @@ public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
82129
break;
83130
}
84131
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()));
86134
mToast.show();
87135
});
88136
}
89137

90138
private void initCardHolder(ViewHolder holder) {
139+
holder.imageView.setVisibility(View.VISIBLE);
140+
holder.imageView2.setImageBitmap(null);
141+
holder.imageView2.setVisibility(View.GONE);
142+
91143
holder.imageView.getLayoutParams().width = 360;
92-
holder.itemView.requestLayout();
144+
holder.imageView.requestLayout();
93145
holder.imageView.setImageBitmap(getBitmap(mRandom.nextInt(3)));
94146
}
95147

96148
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));
100156
}
101157

102158
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();
105165
if (position == 0) {
106-
holder.imageView.setImageBitmap(getBitmap(7));
166+
holder.imageView2.setImageBitmap(getBitmap(7));
107167
} else if (position == mData.size() - 1) {
108-
holder.imageView.setImageBitmap(getBitmap(4));
168+
holder.imageView2.setImageBitmap(getBitmap(4));
109169
} else {
110-
holder.imageView.setImageBitmap(getBitmap(mRandom.nextBoolean() ? 5 : 6));
170+
holder.imageView2.setImageBitmap(getBitmap(mRandom.nextBoolean() ? 5 : 6));
111171
}
112172
}
113173

114174
private Bitmap getBitmap(int index) {
115-
if (mBitmapList == null) {
175+
Bitmap bitmap = mBitmapList.get(index).get();
176+
if (bitmap == null) {
116177
initBitmaps();
178+
return mBitmapList.get(index).get();
117179
}
118-
Bitmap bitmap = mBitmapList.get(index).get();
119180
return bitmap;
120181
}
121182

122183
public void setType(int type) {
123184
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+
}
125235
}
126236

127237
static class ViewHolder extends RecyclerView.ViewHolder {
128238

239+
CardView cardView;
129240
ImageView imageView;
241+
ImageView imageView2;
130242

131243
public ViewHolder(View itemView) {
132244
super(itemView);
245+
cardView = itemView.findViewById(R.id.card);
133246
imageView = itemView.findViewById(R.id.image);
247+
imageView2 = itemView.findViewById(R.id.image2);
134248
}
135249
}
136250
}

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

+4
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,8 @@ private int fixOverflowIndex(int index, int count) {
383383

384384
@Override
385385
public int scrollHorizontallyBy(int dx, RecyclerView.Recycler recycler, RecyclerView.State state) {
386+
mRecycler = recycler;
387+
mState = state;
386388
checkKeyframes();
387389
detachAndScrapAttachedViews(recycler);
388390
//临时记录上一次的offset
@@ -395,6 +397,8 @@ public int scrollHorizontallyBy(int dx, RecyclerView.Recycler recycler, Recycler
395397

396398
@Override
397399
public int scrollVerticallyBy(int dy, RecyclerView.Recycler recycler, RecyclerView.State state) {
400+
mRecycler = recycler;
401+
mState = state;
398402
checkKeyframes();
399403
detachAndScrapAttachedViews(recycler);
400404
float lastOffset = mOffsetY;

app/src/main/res/drawable/ic_1.png

-336 KB
Binary file not shown.

app/src/main/res/drawable/ic_1.webp

29.4 KB
Binary file not shown.

app/src/main/res/drawable/ic_2.png

-203 KB
Binary file not shown.

app/src/main/res/drawable/ic_2.webp

16.5 KB
Binary file not shown.

app/src/main/res/drawable/ic_3.png

-258 KB
Binary file not shown.

app/src/main/res/drawable/ic_3.webp

19.9 KB
Binary file not shown.
-42.7 KB
Binary file not shown.
12.4 KB
Binary file not shown.
-29.7 KB
Binary file not shown.
8.18 KB
Binary file not shown.
-51.7 KB
Binary file not shown.
13.5 KB
Binary file not shown.
-34.6 KB
Binary file not shown.
9.89 KB
Binary file not shown.

app/src/main/res/drawable/ic_j20.png

-415 KB
Binary file not shown.

app/src/main/res/drawable/ic_j20.webp

22.8 KB
Binary file not shown.
+23-12
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,30 @@
11
<?xml version="1.0" encoding="utf-8"?>
2-
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
2+
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
33
xmlns:app="http://schemas.android.com/apk/res-auto"
4-
android:id="@+id/root_view"
54
android:layout_width="wrap_content"
6-
android:layout_height="wrap_content"
7-
app:cardBackgroundColor="@android:color/white"
8-
app:cardCornerRadius="8dp"
9-
app:cardElevation="4dp"
10-
app:cardPreventCornerOverlap="true"
11-
app:cardUseCompatPadding="true">
5+
android:layout_height="wrap_content">
6+
7+
<android.support.v7.widget.CardView
8+
android:id="@+id/card_view"
9+
android:layout_width="wrap_content"
10+
android:layout_height="wrap_content"
11+
app:cardBackgroundColor="@android:color/white"
12+
app:cardCornerRadius="8dp"
13+
app:cardElevation="4dp"
14+
app:cardPreventCornerOverlap="true"
15+
app:cardUseCompatPadding="true">
16+
17+
<ImageView
18+
android:id="@+id/image"
19+
android:layout_width="wrap_content"
20+
android:layout_height="wrap_content"
21+
android:adjustViewBounds="true" />
22+
23+
</android.support.v7.widget.CardView>
1224

1325
<ImageView
14-
android:id="@+id/image"
15-
android:layout_width="250dp"
26+
android:id="@+id/image2"
27+
android:layout_width="wrap_content"
1628
android:layout_height="wrap_content"
1729
android:adjustViewBounds="true" />
18-
19-
</android.support.v7.widget.CardView>
30+
</FrameLayout>

0 commit comments

Comments
 (0)