/
ShapedButton.java
194 lines (171 loc) · 7.31 KB
/
ShapedButton.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package net.fadvisor.funwitharabic;
import android.content.Context;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.PorterDuff;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
public class ShapedButton extends Button implements View.OnTouchListener {
private int buttonColor;
private int pressedShift = 20; // The amount of shift between the normal and pressed state of the button, used to push the text down with the button
private Drawable pressedButton;
private Drawable normalButton;
private Bitmap bitmap;
public ShapedButton(Context context) {
super(context);
setDefaults();
this.setOnTouchListener(this);
}
public ShapedButton(Context context, AttributeSet attrs) {
super(context, attrs);
setDefaults();
parseAttrs(context, attrs);
this.setOnTouchListener(this);
}
public ShapedButton(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
setDefaults();
parseAttrs(context, attrs);
this.setOnTouchListener(this);
}
@Override
protected void onFinishInflate() {
super.onFinishInflate();
// Set the button background and color
updateButton(normalButton);
// this.setPadding(0, 0, 0, 0);
}
private void setDefaults() {
//Init default values
Resources resources = getResources();
if (resources == null) return;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
buttonColor = resources.getColor(R.color.blue, null);
} else {
//noinspection deprecation
buttonColor = resources.getColor(R.color.blue);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
pressedButton = getResources().getDrawable(R.drawable.hex_button_white_pressed, null);
normalButton = getResources().getDrawable(R.drawable.hex_button_white_normal, null);
} else {
//noinspection deprecation
pressedButton = getResources().getDrawable(R.drawable.hex_button_white_pressed);
//noinspection deprecation
normalButton = getResources().getDrawable(R.drawable.hex_button_white_normal);
}
bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.hex_button_white_normal);
}
private void parseAttrs(Context context, AttributeSet attrs) {
//Load from custom attributes
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.ShapedButton);
if (typedArray == null) return;
for (int i = 0; i < typedArray.getIndexCount(); i++) {
int attr = typedArray.getIndex(i);
if (attr == R.styleable.ShapedButton_color) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
buttonColor = typedArray.getColor(attr, getResources().getColor(R.color.blue, null));
} else {
//noinspection deprecation
buttonColor = typedArray.getColor(attr, getResources().getColor(R.color.blue));
}
} else if (attr == R.styleable.ShapedButton_pressedShift) {
pressedShift = typedArray.getDimensionPixelSize(attr, 20);
} else if (attr == R.styleable.ShapedButton_normalBackground) {
Drawable drawable = typedArray.getDrawable(attr);
if (drawable != null) {
normalButton = drawable;
// Try to find away to get this from the xml, so it will be based on whatever the user set
bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.hex_button_white_normal);
}
} else if (attr == R.styleable.ShapedButton_pressedBackground) {
Drawable drawable = typedArray.getDrawable(attr);
if (drawable != null) {
pressedButton = drawable;
}
}
}
typedArray.recycle();
}
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
float x = motionEvent.getX();
float y = motionEvent.getY();
// Get the dimensions used in the view
int realW = this.getWidth();
int realH = this.getHeight();
// Get the dimensions of the actual image
int bitmapW = bitmap.getWidth();
int bitmapH = bitmap.getHeight();
// Scale the coordinates from the view to match the actual image
float scaledX = x * bitmapW / realW;
float scaledY = y * bitmapH / realH;
switch (motionEvent.getAction()) {
case MotionEvent.ACTION_DOWN:
if (scaledX >= 0 & scaledY >= 0 & scaledX < bitmapW & scaledY < bitmapH) {
if (bitmap.getPixel((int) scaledX, (int) scaledY) != 0) {
updateButton(pressedButton);
this.setPadding(0, pressedShift, 0, 0);
return false;
}
}
break;
case MotionEvent.ACTION_MOVE:
Rect rect = new Rect();
view.getLocalVisibleRect(rect);
if (!rect.contains((int) x, (int) y)) {
updateButton(normalButton);
this.setPadding(0, 0, 0, 0);
}
break;
case MotionEvent.ACTION_OUTSIDE:
case MotionEvent.ACTION_CANCEL:
case MotionEvent.ACTION_UP:
updateButton(normalButton);
this.setPadding(0, 0, 0, 0);
if (scaledX >= 0 & scaledY >= 0 & scaledX < bitmap.getWidth() & scaledY < bitmap.getHeight()) {
if (bitmap.getPixel((int) scaledX, (int) scaledY) != 0) {
return false;
}
}
break;
}
return true;
}
private void updateButton(Drawable background) {
if (background == null) return;
//Set button background
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
this.setBackground(background);
} else {
//noinspection deprecation
this.setBackgroundDrawable(background);
}
this.getBackground().setColorFilter(buttonColor, PorterDuff.Mode.MULTIPLY);
// this.setBackgroundColor(buttonColor);
}
@Override
public void setEnabled(boolean enabled) {
super.setEnabled(enabled);
if (this.isEnabled()) {
this.getBackground().setColorFilter(buttonColor, PorterDuff.Mode.MULTIPLY);
} else {
int disabledColor;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
disabledColor = getResources().getColor(R.color.gray_light, null);
} else {
//noinspection deprecation
disabledColor = getResources().getColor(R.color.gray_light);
}
this.getBackground().setColorFilter(disabledColor, PorterDuff.Mode.MULTIPLY);
}
}
}