-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
TextView 改为弱引用, 但未能解决TextView在内存中不断增多的问题
- Loading branch information
1 parent
02a11e3
commit 6ab893f
Showing
3 changed files
with
18 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 18 additions & 10 deletions
28
themelib/src/main/java/com/jerey/themelib/loader/TextViewRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,46 @@ | ||
package com.jerey.themelib.loader; | ||
|
||
import android.graphics.Typeface; | ||
import android.util.Log; | ||
import android.widget.TextView; | ||
|
||
import com.jerey.themelib.utils.TypefaceUtils; | ||
|
||
import java.lang.ref.WeakReference; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
|
||
/** | ||
* Created by _SOLID | ||
* Date:2016/7/12 | ||
* Time:17:58 | ||
* 字体改变工厂 | ||
*/ | ||
public class TextViewRepository { | ||
private static List<TextView> mTextViews = new ArrayList<>(); | ||
private static final String TAG = "TextViewRepository"; | ||
/** | ||
* 虽使用了弱引用来解决TextView得不到释放的问题,但是没有效果,内存中的TextView依旧在不断增多. | ||
* 即使不加入列表里面,TextView数量依旧是在使用过程中中变多 | ||
*/ | ||
private static ArrayList<WeakReference<TextView>> mTextViewsWeakList = new ArrayList<>(); | ||
|
||
public static void add(TextView textView) { | ||
mTextViews.add(textView); | ||
mTextViewsWeakList.add(new WeakReference<TextView>(textView)); | ||
textView.setTypeface(TypefaceUtils.CURRENT_TYPEFACE); | ||
Log.d(TAG, "TextViewRepository mTextViews count: " + mTextViewsWeakList.size()); | ||
} | ||
|
||
public static void clear() { | ||
mTextViews.clear(); | ||
mTextViewsWeakList.clear(); | ||
} | ||
|
||
public static void remove(TextView textView) { | ||
mTextViews.remove(textView); | ||
mTextViewsWeakList.remove(new WeakReference<TextView>(textView)); | ||
} | ||
|
||
public static void applyFont(Typeface tf) { | ||
for (TextView textView : mTextViews) { | ||
textView.setTypeface(tf); | ||
for (WeakReference<TextView> textViewWeak : mTextViewsWeakList) { | ||
TextView textView = textViewWeak.get(); | ||
if (textView != null) { | ||
textView.setTypeface(tf); | ||
} | ||
} | ||
} | ||
} |