|
1 | 1 | package com.gc.materialdesign.views;
|
2 | 2 |
|
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 |
| - |
8 | 3 | import android.content.Context;
|
9 | 4 | import android.graphics.Bitmap;
|
10 | 5 | import android.graphics.Canvas;
|
|
19 | 14 | import android.view.View;
|
20 | 15 | import android.widget.RelativeLayout;
|
21 | 16 |
|
| 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 | + |
22 | 22 | public class Switch extends CustomView {
|
23 | 23 |
|
24 |
| - int backgroundColor = Color.parseColor("#4CAF50"); |
| 24 | + private int backgroundColor = Color.parseColor("#4CAF50"); |
25 | 25 |
|
26 |
| - Ball ball; |
| 26 | + private Ball ball; |
27 | 27 |
|
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; |
31 | 31 |
|
32 |
| - OnCheckListener onCheckListener; |
| 32 | + private OnCheckListener onCheckListener; |
| 33 | + private Bitmap bitmap; |
33 | 34 |
|
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() { |
38 | 39 |
|
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 | + } |
48 | 49 |
|
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) { |
51 | 52 |
|
52 |
| - setBackgroundResource(R.drawable.background_transparent); |
| 53 | + setBackgroundResource(R.drawable.background_transparent); |
53 | 54 |
|
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())); |
57 | 58 |
|
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) { |
63 | 64 | setBackgroundColor(getResources().getColor(bacgroundColor));
|
64 | 65 | } else {
|
65 | 66 | // Color by hexadecimal
|
@@ -121,12 +122,15 @@ public boolean onTouchEvent(MotionEvent event) {
|
121 | 122 | @Override
|
122 | 123 | protected void onDraw(Canvas canvas) {
|
123 | 124 | super.onDraw(canvas);
|
124 |
| - if (!placedBall) |
125 |
| - placeBall(); |
| 125 | + if (!placedBall) { |
| 126 | + placeBall(); |
| 127 | + } |
126 | 128 |
|
127 | 129 | // 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 | + } |
130 | 134 | Canvas temp = new Canvas(bitmap);
|
131 | 135 | Paint paint = new Paint();
|
132 | 136 | paint.setAntiAlias(true);
|
|
0 commit comments