Skip to content

Commit 9e94e99

Browse files
committed
Merge pull request #139 from filipc/switchMemoryLeakFix
Switch memory leak fix
2 parents 3feb01c + 8b76586 commit 9e94e99

File tree

1 file changed

+43
-39
lines changed
  • MaterialDesign/src/com/gc/materialdesign/views

1 file changed

+43
-39
lines changed

MaterialDesign/src/com/gc/materialdesign/views/Switch.java

Lines changed: 43 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
package com.gc.materialdesign.views;
22

3-
import com.gc.materialdesign.R;
4-
import com.gc.materialdesign.utils.Utils;
5-
import com.nineoldandroids.animation.ObjectAnimator;
6-
import com.nineoldandroids.view.ViewHelper;
7-
83
import android.content.Context;
94
import android.graphics.Bitmap;
105
import android.graphics.Canvas;
@@ -19,47 +14,53 @@
1914
import android.view.View;
2015
import android.widget.RelativeLayout;
2116

17+
import com.gc.materialdesign.R;
18+
import com.gc.materialdesign.utils.Utils;
19+
import com.nineoldandroids.animation.ObjectAnimator;
20+
import com.nineoldandroids.view.ViewHelper;
21+
2222
public class Switch extends CustomView {
2323

24-
int backgroundColor = Color.parseColor("#4CAF50");
24+
private int backgroundColor = Color.parseColor("#4CAF50");
2525

26-
Ball ball;
26+
private Ball ball;
2727

28-
boolean check = false;
29-
boolean eventCheck = false;
30-
boolean press = false;
28+
private boolean check = false;
29+
private boolean eventCheck = false;
30+
private boolean press = false;
3131

32-
OnCheckListener onCheckListener;
32+
private OnCheckListener onCheckListener;
33+
private Bitmap bitmap;
3334

34-
public Switch(Context context, AttributeSet attrs) {
35-
super(context, attrs);
36-
setAttributes(attrs);
37-
setOnClickListener(new OnClickListener() {
35+
public Switch(Context context, AttributeSet attrs) {
36+
super(context, attrs);
37+
setAttributes(attrs);
38+
setOnClickListener(new OnClickListener() {
3839

39-
@Override
40-
public void onClick(View arg0) {
41-
if (check)
42-
setChecked(false);
43-
else
44-
setChecked(true);
45-
}
46-
});
47-
}
40+
@Override
41+
public void onClick(View arg0) {
42+
if (check)
43+
setChecked(false);
44+
else
45+
setChecked(true);
46+
}
47+
});
48+
}
4849

49-
// Set atributtes of XML to View
50-
protected void setAttributes(AttributeSet attrs) {
50+
// Set atributtes of XML to View
51+
protected void setAttributes(AttributeSet attrs) {
5152

52-
setBackgroundResource(R.drawable.background_transparent);
53+
setBackgroundResource(R.drawable.background_transparent);
5354

54-
// Set size of view
55-
setMinimumHeight(Utils.dpToPx(48, getResources()));
56-
setMinimumWidth(Utils.dpToPx(80, getResources()));
55+
// Set size of view
56+
setMinimumHeight(Utils.dpToPx(48, getResources()));
57+
setMinimumWidth(Utils.dpToPx(80, getResources()));
5758

58-
// Set background Color
59-
// Color by resource
60-
int bacgroundColor = attrs.getAttributeResourceValue(ANDROIDXML,
61-
"background", -1);
62-
if (bacgroundColor != -1) {
59+
// Set background Color
60+
// Color by resource
61+
int bacgroundColor = attrs.getAttributeResourceValue(ANDROIDXML,
62+
"background", -1);
63+
if (bacgroundColor != -1) {
6364
setBackgroundColor(getResources().getColor(bacgroundColor));
6465
} else {
6566
// Color by hexadecimal
@@ -121,12 +122,15 @@ public boolean onTouchEvent(MotionEvent event) {
121122
@Override
122123
protected void onDraw(Canvas canvas) {
123124
super.onDraw(canvas);
124-
if (!placedBall)
125-
placeBall();
125+
if (!placedBall) {
126+
placeBall();
127+
}
126128

127129
// Crop line to transparent effect
128-
Bitmap bitmap = Bitmap.createBitmap(canvas.getWidth(),
129-
canvas.getHeight(), Bitmap.Config.ARGB_8888);
130+
if(null == bitmap) {
131+
bitmap = Bitmap.createBitmap(canvas.getWidth(),
132+
canvas.getHeight(), Bitmap.Config.ARGB_8888);
133+
}
130134
Canvas temp = new Canvas(bitmap);
131135
Paint paint = new Paint();
132136
paint.setAntiAlias(true);

0 commit comments

Comments
 (0)