3
3
import android .animation .ObjectAnimator ;
4
4
import android .content .Context ;
5
5
import android .content .res .TypedArray ;
6
+ import android .graphics .Bitmap ;
7
+ import android .graphics .Bitmap .Config ;
6
8
import android .graphics .Canvas ;
7
9
import android .graphics .Color ;
8
10
import android .graphics .ColorFilter ;
@@ -41,23 +43,24 @@ public AnimatedStrokeDrawable(Context context, AttributeSet attrs, int defStyleA
41
43
42
44
array = context .obtainStyledAttributes (attrs , R .styleable .AnimatedStrokeDrawable , defStyleAttr , defStyleRes );
43
45
mStrokeWidth = (int ) TypedValue .applyDimension (TypedValue .COMPLEX_UNIT_DIP , 1 , context .getResources ().getDisplayMetrics ());
44
- mStrokeWidth = array .getDimensionPixelSize (R .styleable .AnimatedStrokeDrawable_animatedStrokeWidth , mStrokeWidth );
46
+ mStrokeWidth = array .getDimensionPixelSize (R .styleable .AnimatedStrokeDrawable_strokeWidth , mStrokeWidth );
45
47
mAnimateFromCenter = array .getBoolean (R .styleable .AnimatedStrokeDrawable_animateFromCenter , mAnimateFromCenter );
46
48
array .recycle ();
47
49
48
50
mPaint = new Paint (Paint .ANTI_ALIAS_FLAG );
49
51
mPaint .setStyle (Style .FILL );
50
- mPaint .setColor (colorPrimary );
52
+ mPaint .setColor (colorPrimaryDark );
51
53
52
54
mFocusedPaint = new Paint (Paint .ANTI_ALIAS_FLAG );
53
55
mFocusedPaint .setStyle (Style .FILL );
54
- mFocusedPaint .setColor (colorPrimaryDark );
56
+ mFocusedPaint .setColor (colorPrimary );
55
57
56
58
mBounds = new Rect ();
57
59
}
58
60
59
61
@ Override
60
62
protected void onBoundsChange (Rect bounds ) {
63
+ super .onBoundsChange (bounds );
61
64
mBounds .set (bounds );
62
65
if (mStartAfterBoundsSet ) {
63
66
animateFocus ();
@@ -77,32 +80,31 @@ public int getIntrinsicWidth() {
77
80
78
81
@ Override
79
82
public void draw (Canvas canvas ) {
83
+
80
84
final Rect bounds = getBounds ();
81
85
final int saveCount = canvas .save ();
86
+
82
87
canvas .translate (bounds .left , bounds .top );
83
- canvas .drawRect (getBounds ().left , getBounds ().bottom - mStrokeWidth , getBounds ().right , getBounds ().bottom , mPaint );
88
+ if (mBackground == null ) {
89
+ canvas .drawRect (bounds .left , bounds .bottom - mStrokeWidth , bounds .right , bounds .bottom , mPaint );
90
+ } else {
91
+ Bitmap bitmap = Bitmap .createBitmap (mBounds .width (), mStrokeWidth , Config .ARGB_8888 );
92
+ Canvas bgCanvas = new Canvas (bitmap );
93
+ mBackground .draw (bgCanvas );
94
+ canvas .drawBitmap (bitmap , bounds .left , bounds .bottom - mStrokeWidth , mPaint );
95
+ }
84
96
85
97
if (mAnimateFromCenter ) {
86
98
int w = getIntrinsicWidth ();
87
99
float left = ((float ) (w - mWidth )) / 2.0f ;
88
100
float right = left + mWidth ;
89
- canvas .drawRect (left , getBounds () .bottom - mStrokeWidth , right , getBounds () .bottom , mFocusedPaint );
101
+ canvas .drawRect (left , bounds .bottom - mStrokeWidth , right , bounds .bottom , mFocusedPaint );
90
102
} else {
91
- canvas .drawRect (getBounds () .left , getBounds () .bottom - mStrokeWidth , getBounds () .left + mWidth , getBounds () .bottom , mFocusedPaint );
103
+ canvas .drawRect (bounds .left , bounds .bottom - mStrokeWidth , bounds .left + mWidth , bounds .bottom , mFocusedPaint );
92
104
}
93
105
canvas .restoreToCount (saveCount );
94
106
}
95
107
96
- @ Override
97
- public void setAlpha (int alpha ) {
98
- // nothing
99
- }
100
-
101
- @ Override
102
- public void setColorFilter (ColorFilter cf ) {
103
- // nothing
104
- }
105
-
106
108
@ Override
107
109
public int getOpacity () {
108
110
return PixelFormat .UNKNOWN ;
@@ -136,6 +138,16 @@ private void stopAnimation() {
136
138
}
137
139
}
138
140
141
+ @ Override
142
+ public void setAlpha (int alpha ) {
143
+ // nothing
144
+ }
145
+
146
+ @ Override
147
+ public void setColorFilter (ColorFilter cf ) {
148
+ // nothing
149
+ }
150
+
139
151
@ Override
140
152
public void onViewAttachedToWindow (View view ) {
141
153
// nothing
0 commit comments