Skip to content

Commit 608234a

Browse files
committed
[M]优化内存占用,解决资源回收问题 & update README.md
1 parent 4b05f26 commit 608234a

File tree

11 files changed

+92
-62
lines changed

11 files changed

+92
-62
lines changed

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ transferee 可以帮助你完成从缩略视图到原视图的无缝过渡转变
2626
# Sample
2727
[demo.apk](https://github.com/Hitomis/transferee/tree/master/preview/app-release.apk)
2828

29+
<img src="preview/memory.png" />
30+
2931

3032
# Dependency
3133
step1.
@@ -111,6 +113,11 @@ step 3: 显示 transferee
111113
transferee.apply(config).show();
112114
```
113115

116+
step 4: 离开页面的时候需要销毁 transferee 中的资源,防止内存泄漏 (建议写在 onDestroy 方法中)
117+
```
118+
transferee.destroy()
119+
```
120+
114121
# Config
115122
| 属性 | 说明 |
116123
| :--: | :--: |
@@ -150,6 +157,7 @@ transferee.apply(config).show();
150157
| isShown() | transferee 是否显示 |
151158
| dismiss() | 关闭 transferee |
152159
| clear() | 清除图片和视频等所有缓存文件 |
160+
| destroy() | 销毁 transferee 使用到的资源, 防止内存泄漏 |
153161
| getImageFile(url) | 获取与 url 对应的缓存图片 |
154162
| setOnTransfereeStateChangeListener(listener) | 设置 transferee 显示/关闭状态改变的监听器 |
155163

@@ -164,6 +172,8 @@ transferee.apply(config).show();
164172
- 修复打开和关闭的状态不能保持一致的 bug
165173
- 下滑手势优化
166174
- 重置不再当前显示区域的图片状态
175+
- 优化了内存占用,当浏览多图相册时,保证内存稳定
176+
- 解决了因为 Config 资源未回收而导致的内存泄漏问题
167177

168178
+ v1.6.0
169179
- 新增视频播放以及视频配套功能的支持

Transferee/src/main/java/com/hitomi/tilibrary/transfer/TransferAdapter.java

Lines changed: 46 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
import androidx.annotation.NonNull;
1212
import androidx.viewpager.widget.PagerAdapter;
1313

14-
import com.hitomi.tilibrary.view.video.ExoVideoView;
1514
import com.hitomi.tilibrary.view.image.TransferImage;
15+
import com.hitomi.tilibrary.view.video.ExoVideoView;
1616

1717
import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;
1818
import static android.view.ViewGroup.LayoutParams.WRAP_CONTENT;
@@ -25,7 +25,6 @@
2525
* email: 196425254@qq.com
2626
*/
2727
class TransferAdapter extends PagerAdapter {
28-
2928
private TransferLayout transfer;
3029
private int showIndex;
3130
private int imageSize;
@@ -38,8 +37,7 @@ class TransferAdapter extends PagerAdapter {
3837
this.imageSize = imageSize;
3938
this.showIndex = nowThumbnailIndex + 1 == imageSize
4039
? nowThumbnailIndex - 1 : nowThumbnailIndex + 1;
41-
this.showIndex = showIndex < 0 ? 0 : showIndex;
42-
40+
this.showIndex = Math.max(showIndex, 0);
4341
containLayoutArray = new SparseArray<>();
4442
}
4543

@@ -56,63 +54,24 @@ public boolean isViewFromObject(View view, Object object) {
5654
@Override
5755
public void destroyItem(ViewGroup container, int position, Object object) {
5856
container.removeView((View) object);
59-
}
60-
61-
/**
62-
* 获取指定索引页面中的 TransferImage
63-
*
64-
* @param position
65-
* @return
66-
*/
67-
TransferImage getImageItem(int position) {
68-
FrameLayout parentLayout = containLayoutArray.get(position);
69-
if (parentLayout != null && parentLayout.getChildAt(0) instanceof TransferImage) {
70-
return ((TransferImage) parentLayout.getChildAt(0));
71-
}
72-
return null;
73-
}
74-
75-
/**
76-
* 获取指定页面中的 ExoVideoView
77-
*
78-
* @param position
79-
* @return
80-
*/
81-
ExoVideoView getVideoItem(int position) {
82-
FrameLayout parentLayout = containLayoutArray.get(position);
83-
if (parentLayout != null && parentLayout.getChildAt(0) instanceof ExoVideoView) {
84-
return ((ExoVideoView) parentLayout.getChildAt(0));
85-
}
86-
return null;
87-
}
88-
89-
SparseArray<FrameLayout> getCacheItems() {
90-
return containLayoutArray;
91-
}
92-
93-
FrameLayout getParentItem(int position) {
94-
return containLayoutArray.get(position);
95-
}
96-
97-
void setOnInstantListener(OnInstantiateItemListener listener) {
98-
this.onInstantListener = listener;
57+
containLayoutArray.remove(position);
58+
transfer.loadedIndexSet.remove(position);
9959
}
10060

10161
@NonNull
10262
@Override
10363
public Object instantiateItem(@NonNull ViewGroup container, int position) {
10464
// ViewPager instantiateItem 顺序:按 position 递减 OffscreenPageLimit,
10565
// 再从 position 递增 OffscreenPageLimit 的次序创建页面
106-
FrameLayout parentLayout = containLayoutArray.get(position);
10766

67+
FrameLayout parentLayout = containLayoutArray.get(position);
10868
if (parentLayout == null) {
10969
parentLayout = newParentLayout(container, position);
110-
containLayoutArray.put(position, parentLayout);
70+
containLayoutArray.append(position, parentLayout);
11171

11272
if (position == showIndex && onInstantListener != null)
11373
onInstantListener.onComplete();
11474
}
115-
11675
container.addView(parentLayout);
11776
return parentLayout;
11877
}
@@ -147,6 +106,46 @@ private FrameLayout newParentLayout(ViewGroup container, final int pos) {
147106
return parentLayout;
148107
}
149108

109+
/**
110+
* 获取指定索引页面中的 TransferImage
111+
*
112+
* @param position
113+
* @return
114+
*/
115+
TransferImage getImageItem(int position) {
116+
FrameLayout parentLayout = containLayoutArray.get(position);
117+
if (parentLayout != null && parentLayout.getChildAt(0) instanceof TransferImage) {
118+
return ((TransferImage) parentLayout.getChildAt(0));
119+
}
120+
return null;
121+
}
122+
123+
/**
124+
* 获取指定页面中的 ExoVideoView
125+
*
126+
* @param position
127+
* @return
128+
*/
129+
ExoVideoView getVideoItem(int position) {
130+
FrameLayout parentLayout = containLayoutArray.get(position);
131+
if (parentLayout != null && parentLayout.getChildAt(0) instanceof ExoVideoView) {
132+
return ((ExoVideoView) parentLayout.getChildAt(0));
133+
}
134+
return null;
135+
}
136+
137+
SparseArray<FrameLayout> getCacheItems() {
138+
return containLayoutArray;
139+
}
140+
141+
FrameLayout getParentItem(int position) {
142+
return containLayoutArray.get(position);
143+
}
144+
145+
void setOnInstantListener(OnInstantiateItemListener listener) {
146+
this.onInstantListener = listener;
147+
}
148+
150149
interface OnInstantiateItemListener {
151150
void onComplete();
152151
}

Transferee/src/main/java/com/hitomi/tilibrary/transfer/TransferConfig.java

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -216,10 +216,6 @@ public void setSourceUrlList(List<String> sourceUrlList) {
216216
this.sourceUrlList = sourceUrlList;
217217
}
218218

219-
public List<Uri> getSourceUriList() {
220-
return sourceUriList;
221-
}
222-
223219
public void setSourceUriList(List<Uri> sourceUriList) {
224220
this.sourceUriList = sourceUriList;
225221
}
@@ -333,6 +329,21 @@ public void setFooterSize(int footerSize) {
333329
this.footerSize = footerSize;
334330
}
335331

332+
public void destroy() {
333+
setImageView(null);
334+
setCustomView(null);
335+
setListView(null);
336+
setRecyclerView(null);
337+
setProgressIndicator(null);
338+
setIndexIndicator(null);
339+
setImageLoader(null);
340+
setOriginImageList(null);
341+
setSourceUrlList(null);
342+
setSourceUriList(null);
343+
setMissDrawable(null);
344+
setErrorDrawable(null);
345+
}
346+
336347
public static class Builder {
337348
private int nowThumbnailIndex;
338349
private int offscreenPageLimit;
@@ -517,14 +528,6 @@ public Builder setSourceUriList(List<Uri> sourceUriList) {
517528
return this;
518529
}
519530

520-
// /**
521-
// * 缩略图地址集合
522-
// */
523-
// public Builder setThumbnailImageList(List<String> thumbnailImageList) {
524-
// this.thumbnailImageList = thumbnailImageList;
525-
// return this;
526-
// }
527-
528531
/**
529532
* 过渡前原始的 ImageView 集合
530533
*/

Transferee/src/main/java/com/hitomi/tilibrary/transfer/TransferLayout.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,11 @@ class TransferLayout extends FrameLayout {
4343

4444
private TransferChangeListener transChangeListener;
4545
private OnLayoutResetListener layoutResetListener;
46-
private Set<Integer> loadedIndexSet;
4746

4847
TransferImage transImage;
4948
TransferAdapter transAdapter;
5049
ViewPager transViewPager;
50+
Set<Integer> loadedIndexSet;
5151
float alpha; // [0.f , 255.f]
5252
boolean isAnimationRunning;
5353

Transferee/src/main/java/com/hitomi/tilibrary/transfer/Transferee.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,16 @@ public void setOnTransfereeStateChangeListener(OnTransfereeStateChangeListener l
248248
transListener = listener;
249249
}
250250

251+
/**
252+
* 资源销毁,防止内存泄漏
253+
*/
254+
public void destroy() {
255+
if (transConfig != null) {
256+
transConfig.destroy();
257+
transConfig = null;
258+
}
259+
}
260+
251261
/**
252262
* Transferee 显示的时候调用 {@link OnTransfereeStateChangeListener#onShow()}
253263
* <p>

app/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,5 @@ dependencies {
5151
implementation libGlide
5252
implementation libPicasso
5353
implementation libImmersionbar
54+
debugImplementation libLeakcanary
5455
}

app/src/main/java/com/hitomi/transferimage/activity/BaseActivity.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
4343
testTransferee();
4444
}
4545

46+
@Override
47+
protected void onDestroy() {
48+
super.onDestroy();
49+
transferee.destroy();
50+
}
51+
4652
@Override
4753
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
4854
if (requestCode != WRITE_EXTERNAL_STORAGE) {

app/src/main/java/com/hitomi/transferimage/activity/LocalImageActivity.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
import com.hitomi.tilibrary.style.index.NumberIndexIndicator;
1414
import com.hitomi.tilibrary.style.progress.ProgressBarIndicator;
1515
import com.hitomi.tilibrary.transfer.TransferConfig;
16-
import com.hitomi.transferimage.SourceConfig;
1716
import com.hitomi.transferimage.R;
17+
import com.hitomi.transferimage.SourceConfig;
1818
import com.vansz.glideimageloader.GlideImageLoader;
1919
import com.zhy.adapter.abslistview.CommonAdapter;
2020
import com.zhy.adapter.abslistview.ViewHolder;

build.gradle

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,14 @@ project.ext {
4949
libBaseAdapter = "com.zhy:base-adapter:3.0.3"
5050
libRecyclerviewAdpater = "com.zhy:base-rvadapter:3.0.3"
5151
libImmersionbar = "com.gyf.immersionbar:immersionbar:3.0.0"
52+
libLeakcanary = "com.squareup.leakcanary:leakcanary-android:2.3"
5253
// Transferee 核心库所依赖项目
5354
libAppcompat = "androidx.appcompat:appcompat:1.1.0"
5455
libSupportRecyclerview = "androidx.recyclerview:recyclerview:1.0.0"
5556
libExoplayer = "com.google.android.exoplayer:exoplayer:2.10.4"
5657
libExoExtensionRtmp = "com.google.android.exoplayer:extension-rtmp:2.10.4"
5758
libProgresspieview = "com.github.filippudak.progresspieview:library:1.0.4"
5859
libGifDrawable = "pl.droidsonroids.gif:android-gif-drawable:1.2.19"
59-
libImmersionbar ="com.gyf.immersionbar:immersionbar:3.0.0"
60+
libImmersionbar = "com.gyf.immersionbar:immersionbar:3.0.0"
6061
libLuban = "top.zibin:Luban:1.1.8"
6162
}

preview/app-release.apk

3.24 KB
Binary file not shown.

preview/memory.png

312 KB
Loading

0 commit comments

Comments
 (0)