Skip to content

Commit 787489d

Browse files
committed
v1.2优化内存泄漏问题
1 parent 26b3ef5 commit 787489d

5 files changed

Lines changed: 39 additions & 2 deletions

File tree

app/build.gradle

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ android {
2020
}
2121

2222
dependencies {
23+
debugCompile 'com.squareup.leakcanary:leakcanary-android:1.5'
24+
releaseCompile 'com.squareup.leakcanary:leakcanary-android-no-op:1.5'
25+
testCompile 'com.squareup.leakcanary:leakcanary-android-no-op:1.5'
2326
compile fileTree(dir: 'libs', include: ['*.jar'])
2427
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.1', {
2528
exclude group: 'com.android.support', module: 'support-annotations'

app/src/main/AndroidManifest.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
xmlns:android="http://schemas.android.com/apk/res/android">
44

55
<application
6+
android:name=".MyApplication"
67
android:allowBackup="true"
78
android:icon="@mipmap/ic_launcher"
89
android:label="@string/app_name"

app/src/main/java/com/example/liangmutian/randomtextview/MainActivity.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,5 +64,9 @@ public void start4(View v) {
6464

6565
}
6666

67-
67+
@Override
68+
protected void onDestroy() {
69+
super.onDestroy();
70+
mRandomTextView.destroy();
71+
}
6872
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.example.liangmutian.randomtextview;
2+
3+
import android.app.Application;
4+
5+
import com.squareup.leakcanary.LeakCanary;
6+
7+
/**
8+
* Created by wuduogen838 on 16/11/30.
9+
*/
10+
11+
public class MyApplication extends Application {
12+
13+
@Override
14+
public void onCreate() {
15+
super.onCreate();
16+
if (LeakCanary.isInAnalyzerProcess(this)) {
17+
// This process is dedicated to LeakCanary for heap analysis.
18+
// You should not init your app in this process.
19+
return;
20+
}
21+
LeakCanary.install(this);
22+
}
23+
}

app/src/main/java/com/example/liangmutian/randomtextview/view/RandomTextView.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public void setPianyilian(int pianyiliangTpye) {
9696
break;
9797
case ALL:
9898
for (int i = 0; i < text.length(); i++) {
99-
pianyilianglist[i] = 15;
99+
pianyilianglist[i] = 5;
100100
}
101101

102102
break;
@@ -240,6 +240,11 @@ private ArrayList<Integer> getList(String s) {
240240

241241

242242
private static final Handler handler = new Handler();
243+
public void destroy (){
244+
auto=false;
245+
handler.removeCallbacks(task);
246+
247+
}
243248

244249
private final Runnable task = new Runnable() {
245250

@@ -269,4 +274,5 @@ private void drawText(Canvas mCanvas,String text,float x,float y,Paint p){
269274
}
270275

271276

277+
272278
}

0 commit comments

Comments
 (0)