Skip to content

Commit 64e528a

Browse files
committed
Fixed MaterialIconView memory leak
1 parent c76abd9 commit 64e528a

File tree

9 files changed

+96
-163
lines changed

9 files changed

+96
-163
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ You don't have to worry about android including the file twice in your apk. Andr
3030

3131
```groovy
3232
dependencies {
33-
compile 'net.steamcrafted:materialiconlib:1.0.4'
33+
compile 'net.steamcrafted:materialiconlib:1.0.5'
3434
}
3535
```
3636

app/app.iml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@
7373
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/dex-cache" />
7474
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/exploded-aar/com.android.support/appcompat-v7/22.2.0/jars" />
7575
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/exploded-aar/com.android.support/support-v4/22.2.0/jars" />
76+
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/exploded-aar/net.steamcrafted/materialiconlib/1.0.5/jars" />
7677
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/incremental" />
7778
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/jacoco" />
7879
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/javaResources" />
@@ -92,9 +93,9 @@
9293
</content>
9394
<orderEntry type="jdk" jdkName="Android API 22 Platform" jdkType="Android SDK" />
9495
<orderEntry type="sourceFolder" forTests="false" />
96+
<orderEntry type="library" exported="" name="materialiconlib-1.0.5" level="project" />
9597
<orderEntry type="library" exported="" name="support-annotations-22.2.0" level="project" />
9698
<orderEntry type="library" exported="" name="support-v4-22.2.0" level="project" />
9799
<orderEntry type="library" exported="" name="appcompat-v7-22.2.0" level="project" />
98-
<orderEntry type="module" module-name="materialiconlib" exported="" />
99100
</component>
100101
</module>

app/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,5 @@ android {
2424
dependencies {
2525
compile fileTree(dir: 'libs', include: ['*.jar'])
2626
compile 'com.android.support:appcompat-v7:22.2.0'
27-
compile project(":materialiconlib")
27+
compile 'net.steamcrafted:materialiconlib:1.0.5'
2828
}

app/src/main/java/net/steamcrafted/materialiconview/ImageAdapter.java

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@
22

33
import android.graphics.Color;
44
import android.support.annotation.NonNull;
5+
import android.util.Log;
56
import android.view.View;
67
import android.view.ViewGroup;
8+
import android.widget.AbsListView;
79
import android.widget.BaseAdapter;
10+
import android.widget.ImageView;
811

912
import net.steamcrafted.materialiconlib.MaterialDrawableBuilder;
1013
import net.steamcrafted.materialiconlib.MaterialIconUtils;
@@ -41,13 +44,17 @@ public long getItemId(int i) {
4144
@Override
4245
public View getView(int i, View view, ViewGroup viewGroup) {
4346
if(view == null){
44-
view = new MaterialIconView(viewGroup.getContext());
45-
view.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
47+
view = new ImageView(viewGroup.getContext());
48+
view.setLayoutParams(new AbsListView.LayoutParams(AbsListView.LayoutParams.WRAP_CONTENT, AbsListView.LayoutParams.WRAP_CONTENT));
4649
}
47-
MaterialIconView v = (MaterialIconView) view;
48-
v.setIcon(icons.get(i));
49-
v.setSizePx(viewGroup.getWidth()/5);
50-
v.setColor(Color.BLACK);
50+
ImageView v = (ImageView) view;
51+
v.setImageDrawable(
52+
MaterialDrawableBuilder.with(viewGroup.getContext())
53+
.setIcon(icons.get(i))
54+
.setColor(Color.BLACK)
55+
.setSizePx(viewGroup.getWidth() / 5)
56+
.build()
57+
);
5158
return v;
5259
}
5360
}

app/src/main/java/net/steamcrafted/materialiconview/MainActivity.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ protected void onCreate(Bundle savedInstanceState) {
3232
ImageAdapter adapt = new ImageAdapter(vals);
3333
mListview.setAdapter(adapt);
3434

35+
36+
3537
/*mIcon = (MaterialIconView) findViewById(R.id.icon);
3638
ImageView imgicon = (ImageView) findViewById(R.id.image_icon);
3739
Lines changed: 68 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,77 @@
1-
<!--<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
1+
<!--
2+
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
23
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
34
xmlns:app="http://schemas.android.com/apk/res-auto"
4-
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
5-
android:paddingRight="@dimen/activity_horizontal_margin"
6-
android:paddingTop="@dimen/activity_vertical_margin"
7-
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
5+
android:layout_height="match_parent">
6+
7+
<RelativeLayout
8+
android:layout_width="match_parent"
9+
android:layout_height="wrap_content" android:paddingLeft="@dimen/activity_horizontal_margin"
10+
android:paddingRight="@dimen/activity_horizontal_margin"
11+
android:paddingTop="@dimen/activity_vertical_margin"
12+
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
13+
14+
<net.steamcrafted.materialiconlib.MaterialIconView
15+
android:layout_width="48dp"
16+
android:layout_height="48dp"
17+
app:materialIcon="clipboard_arrow_down"
18+
android:background="@android:color/darker_gray"
19+
android:textColor="#fff"
20+
android:textSize="24dp"
21+
android:gravity="center"
22+
android:id="@+id/icon"
23+
/>
824
9-
<net.steamcrafted.materialiconlib.MaterialIconView
10-
android:layout_width="48dp"
11-
android:layout_height="48dp"
12-
app:materialIcon="clipboard_arrow_down"
13-
android:background="@android:color/darker_gray"
14-
android:textColor="#fff"
15-
android:textSize="24dp"
16-
android:gravity="center"
17-
android:id="@+id/icon"
18-
/>
25+
<ImageView
26+
android:layout_width="48dp"
27+
android:layout_height="48dp"
28+
android:padding="12dp"
29+
android:background="@android:color/darker_gray"
30+
android:layout_below="@id/icon"
31+
android:layout_marginTop="8dp"
32+
android:id="@+id/image_icon"/>
1933
20-
<ImageView
21-
android:layout_width="48dp"
22-
android:layout_height="48dp"
23-
android:padding="12dp"
24-
android:background="@android:color/darker_gray"
25-
android:layout_below="@id/icon"
26-
android:layout_marginTop="8dp"
27-
android:id="@+id/image_icon"/>
34+
<net.steamcrafted.materialiconlib.MaterialIconView
35+
android:layout_width="104dp"
36+
android:layout_height="104dp"
37+
app:materialIcon="settings"
38+
android:textColor="#333"
39+
android:textSize="48dp"
40+
android:gravity="center"
41+
android:background="#33EE2222"
42+
android:layout_toRightOf="@id/icon"
43+
android:layout_marginLeft="8dp"
44+
android:id="@+id/icon_1"
45+
android:layout_marginBottom="8dp"
46+
/>
47+
<net.steamcrafted.materialiconlib.MaterialIconView
48+
android:layout_width="192dp"
49+
android:layout_height="192dp"
50+
app:materialIcon="console"
51+
android:textColor="#333"
52+
android:textSize="96dp"
53+
android:background="#3322EE22"
54+
android:gravity="center"
55+
android:layout_below="@id/icon_1"
56+
android:id="@+id/icon_2"
57+
/>
2858
29-
<net.steamcrafted.materialiconlib.MaterialIconView
30-
android:layout_width="104dp"
31-
android:layout_height="104dp"
32-
app:materialIcon="settings"
33-
android:textColor="#333"
34-
android:textSize="48dp"
35-
android:gravity="center"
36-
android:background="#33EE2222"
37-
android:layout_toRightOf="@id/icon"
38-
android:layout_marginLeft="8dp"
39-
android:id="@+id/icon_1"
40-
android:layout_marginBottom="8dp"
41-
/>
42-
<net.steamcrafted.materialiconlib.MaterialIconView
43-
android:layout_width="192dp"
44-
android:layout_height="192dp"
45-
app:materialIcon="console"
46-
android:textColor="#333"
47-
android:textSize="96dp"
48-
android:background="#3322EE22"
49-
android:gravity="center"
50-
android:layout_below="@id/icon_1"
51-
android:id="@+id/icon_2"
52-
/>
59+
<net.steamcrafted.materialiconlib.MaterialIconView
60+
android:layout_width="300dp"
61+
android:layout_height="200dp"
62+
android:padding="20dp"
63+
app:materialIcon="flash"
64+
app:materialIconColor="#AA0000"
65+
66+
android:layout_centerInParent="true"
67+
/>
68+
69+
</RelativeLayout>
70+
71+
</ScrollView>-->
5372

54-
<net.steamcrafted.materialiconlib.MaterialIconView
55-
android:layout_width="300dp"
56-
android:layout_height="200dp"
57-
android:padding="20dp"
58-
app:materialIcon="flash"
59-
app:materialIconColor="#AA0000"
6073

61-
android:layout_centerInParent="true"
62-
/>
6374

64-
</RelativeLayout>-->
6575

6676
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
6777
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
@@ -71,4 +81,5 @@
7181
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
7282
android:paddingRight="@dimen/activity_horizontal_margin"
7383
android:paddingTop="@dimen/activity_vertical_margin"
74-
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"/>
84+
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"/>
85+

materialiconlib/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ apply plugin: 'com.android.library'
44
ext {
55
PUBLISH_GROUP_ID = 'net.steamcrafted'
66
PUBLISH_ARTIFACT_ID = 'materialiconlib'
7-
PUBLISH_VERSION = '1.0.4'
7+
PUBLISH_VERSION = '1.0.5'
88
}
99

1010
android {

materialiconlib/src/main/java/net/steamcrafted/materialiconlib/MaterialDrawableBuilder.java

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
import android.graphics.*;
88
import android.graphics.drawable.Drawable;
99
import android.text.TextPaint;
10+
import android.util.Log;
11+
12+
import java.util.HashMap;
13+
import java.util.Map;
1014

1115
/**
1216
* Embed an icon into a Drawable that can be used as TextView icons, or ActionBar icons.
@@ -38,8 +42,6 @@ public static enum IconValue {ACCOUNT,ACCOUNT_ALERT,ACCOUNT_BOX,ACCOUNT_BOX_OUTL
3842

3943
private final Rect bounds = new Rect();
4044

41-
private MaterialDrawable mCachedDrawable;
42-
4345
/**
4446
* Create an IconDrawable.
4547
*
@@ -70,16 +72,7 @@ public MaterialDrawable build() throws IconNotSetException{
7072
}
7173
return new MaterialDrawable(context, icon, paint, size, alpha);
7274
}
73-
/*
74-
public MaterialDrawable buildFromCache() throws IconNotSetException{
75-
if(icon == null){
76-
throw new IconNotSetException();
77-
}
78-
if(mCachedDrawable == null) return build();
79-
mCachedDrawable.setIcon(icon).setTextPaint(paint).setSizePx(size).setAlpha(alpha);
80-
return mCachedDrawable;
81-
}
82-
*/
75+
8376
public MaterialDrawableBuilder setIcon(IconValue iconValue){
8477
icon = iconValue;
8578
return this;

materialiconlib/src/main/java/net/steamcrafted/materialiconlib/MaterialIconView.java

Lines changed: 3 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import android.graphics.Paint;
99
import android.graphics.drawable.Drawable;
1010
import android.util.AttributeSet;
11+
import android.util.Log;
1112
import android.widget.ImageView;
1213

1314
/**
@@ -201,7 +202,7 @@ protected void onDraw(Canvas canvas) {
201202

202203
int scaledWidth = getMeasuredWidth();
203204
int scaledHeight = getMeasuredHeight();
204-
int scaleddimen = Math.min(scaledHeight, scaledWidth);
205+
int scaleddimen = (mOverruledSize >= 0) ? mOverruledSize : Math.min(scaledHeight, scaledWidth);
205206

206207
boolean redraw = false;
207208
if(mDrawable == null){
@@ -226,86 +227,4 @@ protected void onDraw(Canvas canvas) {
226227
}
227228

228229

229-
}
230-
231-
/*
232-
233-
public class MaterialIconView extends TextView {
234-
private Rect bounds = new Rect();
235-
236-
public MaterialIconView(Context context) {
237-
super(context);
238-
239-
init();
240-
}
241-
242-
public MaterialIconView(Context context, AttributeSet attrs) {
243-
super(context, attrs);
244-
245-
init(context, attrs);
246-
}
247-
248-
public MaterialIconView(Context context, AttributeSet attrs, int defStyleAttr) {
249-
super(context, attrs, defStyleAttr);
250-
251-
init(context, attrs);
252-
}
253-
254-
private void init(){
255-
setTypeface(MaterialIconUtils.getTypeFace(getContext()));
256-
setPadding(0,0,0,0);
257-
}
258-
259-
260-
private void init(Context context, AttributeSet attrs){
261-
init();
262-
263-
TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.MaterialIconViewFormat);
264-
try {
265-
int type = array.getInt(R.styleable.MaterialIconViewFormat_materialIcon, 0);
266-
267-
if(type >= 0) setIcon(type);
268-
} finally {
269-
array.recycle();
270-
}
271-
}
272-
273-
private void setIcon(int iconIndex){
274-
setText(MaterialIconUtils.getIconString(iconIndex));
275-
invalidate();
276-
}
277-
278-
public void setIcon(MaterialDrawableBuilder.IconValue iconValue){
279-
setText(MaterialIconUtils.getIconString(iconValue.ordinal()));
280-
invalidate();
281-
}
282-
283-
@Override
284-
public void onDraw(Canvas canvas){
285-
final CharSequence text = getText();
286-
287-
TextPaint textPaint = getPaint();
288-
if(isInEditMode()){
289-
super.onDraw(canvas);
290-
return;
291-
}
292-
293-
textPaint.setColor(getCurrentTextColor());
294-
textPaint.getTextBounds(text.toString(), 0, text.length(), bounds);
295-
296-
int availWidth = canvas.getWidth();// - getTotalPaddingLeft() - getTotalPaddingRight();
297-
int availHeight = canvas.getHeight();// - getTotalPaddingTop() - getTotalPaddingBottom();
298-
299-
int xPos = (availWidth - bounds.width()) / 2;
300-
int yPos = (availHeight - bounds.height()) / 2;
301-
int xOffset = xPos - bounds.left;
302-
int yOffset = yPos - bounds.top;
303-
304-
bounds.offsetTo(xPos, yPos);
305-
306-
canvas.drawText(text.toString(), xOffset, yOffset, textPaint);
307-
308-
setText(text);
309-
}
310-
}
311-
*/
230+
}

0 commit comments

Comments
 (0)