-
Notifications
You must be signed in to change notification settings - Fork 261
/
CheckableImageView.java
84 lines (70 loc) · 2.39 KB
/
CheckableImageView.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
package com.manuelpeinado.multichoiceadapter;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.drawable.StateListDrawable;
import android.util.AttributeSet;
import android.widget.Checkable;
import android.widget.ImageView;
import com.manuelpeinado.multichoiceadapter.R;
public class CheckableImageView extends ImageView implements Checkable {
private static final int[] CHECKED_STATE_SET = { android.R.attr.state_checked };
private StateListDrawable stateList;
private boolean mChecked;
public CheckableImageView(Context context) {
super(context);
}
public CheckableImageView(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CheckableImageView,
R.attr.checkableImageViewStyle, R.style.MultiChoiceAdapter_DefaultCheckableImageViewStyle);
stateList = (StateListDrawable)a.getDrawable(R.styleable.CheckableImageView_android_foreground);
a.recycle();
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (stateList != null) {
stateList.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
stateList.draw(canvas);
}
}
/**************************/
/** Checkable **/
/**************************/
@Override
public boolean isChecked() {
return mChecked;
}
@Override
public void setChecked(boolean checked) {
if (mChecked != checked) {
mChecked = checked;
refreshDrawableState();
}
}
@Override
public void toggle() {
setChecked(!isChecked());
}
/**************************/
/** Drawable States **/
/**************************/
@Override
public int[] onCreateDrawableState(int extraSpace) {
final int[] drawableState = super.onCreateDrawableState(extraSpace + 1);
if (isChecked()) {
mergeDrawableStates(drawableState, CHECKED_STATE_SET);
}
return drawableState;
}
@Override
protected void drawableStateChanged() {
super.drawableStateChanged();
if (stateList != null) {
int[] myDrawableState = getDrawableState();
stateList.setState(myDrawableState);
invalidate();
}
}
}