Skip to content

Commit 02804e2

Browse files
committed
Set the extensions based on the ext attributes used rather than explicitly.
Draw the background first for animated drawable.
1 parent 9067e89 commit 02804e2

File tree

11 files changed

+249
-134
lines changed

11 files changed

+249
-134
lines changed

app/src/main/res/layout/activity_home.xml

+17-9
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,9 @@
11
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
2-
xmlns:tools="http://schemas.android.com/tools"
2+
xmlns:app="http://schemas.android.com/apk/res-auto"
33
android:orientation="vertical"
44
android:layout_width="match_parent"
55
android:layout_height="match_parent"
6-
android:paddingLeft="@dimen/activity_horizontal_margin"
7-
android:paddingRight="@dimen/activity_horizontal_margin"
8-
android:paddingTop="@dimen/activity_vertical_margin"
9-
android:paddingBottom="@dimen/activity_vertical_margin"
10-
tools:context=".HomeActivity">
11-
6+
android:padding="20dp">
127

138
<ext.TextView
149
android:layout_width="wrap_content"
@@ -23,13 +18,27 @@
2318
android:layout_marginTop="8dp"
2419
android:text="Normal text"/>
2520

21+
<ext.TextView
22+
android:layout_width="wrap_content"
23+
android:layout_height="wrap_content"
24+
android:layout_marginTop="8dp"
25+
android:textAppearance="@null"
26+
android:text="Default font"/>
27+
2628
<ext.Button
2729
android:id="@+id/button"
2830
android:layout_width="wrap_content"
2931
android:layout_height="wrap_content"
3032
android:layout_marginTop="16dp"
3133
android:text="Beautiful Button"/>
3234

35+
<ext.Button
36+
android:layout_width="wrap_content"
37+
android:layout_height="wrap_content"
38+
android:layout_marginTop="16dp"
39+
app:pushDepth="0dp"
40+
android:text="Ugly (no push) Button"/>
41+
3342
<ext.EditText
3443
android:layout_width="match_parent"
3544
android:layout_height="wrap_content"
@@ -40,7 +49,6 @@
4049
android:layout_width="match_parent"
4150
android:layout_height="wrap_content"
4251
android:layout_marginTop="20dp"
43-
android:hint="Second input"
44-
style="@style/EditTextStyle.FromLeft"/>
52+
android:hint="Second input"/>
4553

4654
</LinearLayout>

app/src/main/res/layout/fragment_simple_form.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959
android:gravity="center"
6060
android:hint="mm"/>
6161

62-
<ext.EditText
62+
<EditText
6363
android:id="@+id/birthday_year"
6464
android:layout_width="wrap_content"
6565
android:layout_height="wrap_content"

app/src/main/res/values/colors.xml

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<resources>
2+
<color name="primary">#9c0</color>
3+
<color name="primaryDark">#690</color>
4+
<color name="neutral">#999</color>
5+
</resources>

app/src/main/res/values/dimens.xml

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
11
<resources>
2-
<!-- Default screen margins, per the Android Design guidelines. -->
3-
<dimen name="activity_horizontal_margin">16dp</dimen>
4-
<dimen name="activity_vertical_margin">16dp</dimen>
2+
<dimen name="edit_text_stroke">2dp</dimen>
53
</resources>

app/src/main/res/values/styles.xml

+8-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<resources>
22

33
<style name="AppTheme" parent="android:Theme.Holo.Light.DarkActionBar">
4-
<item name="colorPrimary">#9c0</item>
5-
<item name="colorPrimaryDark">#690</item>
4+
<item name="colorPrimary">@color/primary</item>
5+
<item name="colorPrimaryDark">@color/primaryDark</item>
66
<item name="android:textViewStyle">@style/TextViewStyle</item>
77
<item name="android:editTextStyle">@style/EditTextStyle</item>
88
<item name="android:buttonStyle">@style/ButtonStyle</item>
@@ -44,11 +44,15 @@
4444
</style>
4545

4646
<style name="EditTextStyle" parent="android:Widget.Holo.EditText">
47-
<item name="animatedStrokeWidth">2dp</item>
47+
<item name="strokeWidth">2dp</item>
4848
<item name="drawableClass">ext.drawable.AnimatedStrokeDrawable</item>
4949
<item name="animateFromCenter">true</item>
5050
<item name="android:textAppearance">@style/TextAppearance</item>
51-
<item name="android:background">@null</item>
51+
<item name="android:background">@color/neutral</item>
52+
<item name="android:paddingTop">8dp</item>
53+
<item name="android:paddingBottom">4dp</item>
54+
<item name="android:paddingLeft">16dp</item>
55+
<item name="android:paddingRight">16dp</item>
5256
</style>
5357

5458
<style name="EditTextStyle.FromLeft">

lib/src/main/java/ext/drawable/AnimatedDrawable.java

+53-3
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33
import android.content.Context;
44
import android.content.res.TypedArray;
5+
import android.graphics.Canvas;
56
import android.graphics.Rect;
67
import android.graphics.drawable.Drawable;
78
import android.util.AttributeSet;
8-
import android.util.Log;
99
import android.view.View;
1010
import android.view.animation.AccelerateDecelerateInterpolator;
1111
import android.view.animation.Interpolator;
@@ -23,6 +23,7 @@ public abstract class AnimatedDrawable extends Drawable {
2323
protected Context mContext;
2424
protected Interpolator mInterpolator;
2525
protected int mAnimationDuration;
26+
protected Drawable mBackground;
2627

2728
public AnimatedDrawable(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
2829
mContext = context;
@@ -31,6 +32,10 @@ public AnimatedDrawable(Context context, AttributeSet attrs, int defStyleAttr, i
3132
TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.AnimatedBackgroundExtension, defStyleAttr, defStyleRes);
3233
String interpolatorClass = array.getString(R.styleable.AnimatedBackgroundExtension_interpolatorClass);
3334
mAnimationDuration = array.getInt(R.styleable.AnimatedBackgroundExtension_android_animationDuration, mAnimationDuration);
35+
int backgroundId = array.getResourceId(R.styleable.AnimatedBackgroundExtension_android_background, 0);
36+
if (backgroundId != 0) {
37+
mBackground = context.getResources().getDrawable(backgroundId);
38+
}
3439
array.recycle();
3540

3641
mInterpolator = instantiateInterpolator(interpolatorClass);
@@ -56,11 +61,56 @@ public void setInterpolator(Interpolator interpolator) {
5661
mInterpolator = interpolator;
5762
}
5863

59-
// Add below all methods that a drawable might be interested in
60-
6164
public abstract void onViewAttachedToWindow(View view);
6265

6366
public abstract void onViewDetachedToWindow(View view);
6467

6568
public abstract void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect);
69+
70+
@Override
71+
public void draw(Canvas canvas) {
72+
if (mBackground != null) {
73+
mBackground.draw(canvas);
74+
}
75+
}
76+
77+
@Override
78+
public void setBounds(int left, int top, int right, int bottom) {
79+
if (mBackground != null) {
80+
mBackground.setBounds(left, top, right, bottom);
81+
}
82+
super.setBounds(left, top, right, bottom);
83+
}
84+
85+
@Override
86+
public void setBounds(Rect bounds) {
87+
if (mBackground != null) {
88+
mBackground.setBounds(bounds);
89+
}
90+
super.setBounds(bounds);
91+
}
92+
93+
@Override
94+
public void setChangingConfigurations(int configs) {
95+
if (mBackground != null) {
96+
mBackground.setChangingConfigurations(configs);
97+
}
98+
super.setChangingConfigurations(configs);
99+
}
100+
101+
@Override
102+
public boolean setState(int[] stateSet) {
103+
if (mBackground != null) {
104+
mBackground.setState(stateSet);
105+
}
106+
return super.setState(stateSet);
107+
}
108+
109+
@Override
110+
public void jumpToCurrentState() {
111+
if (mBackground != null) {
112+
mBackground.jumpToCurrentState();
113+
}
114+
super.jumpToCurrentState();
115+
}
66116
}

lib/src/main/java/ext/drawable/AnimatedStrokeDrawable.java

+28-16
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import android.animation.ObjectAnimator;
44
import android.content.Context;
55
import android.content.res.TypedArray;
6+
import android.graphics.Bitmap;
7+
import android.graphics.Bitmap.Config;
68
import android.graphics.Canvas;
79
import android.graphics.Color;
810
import android.graphics.ColorFilter;
@@ -41,23 +43,24 @@ public AnimatedStrokeDrawable(Context context, AttributeSet attrs, int defStyleA
4143

4244
array = context.obtainStyledAttributes(attrs, R.styleable.AnimatedStrokeDrawable, defStyleAttr, defStyleRes);
4345
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);
4547
mAnimateFromCenter = array.getBoolean(R.styleable.AnimatedStrokeDrawable_animateFromCenter, mAnimateFromCenter);
4648
array.recycle();
4749

4850
mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
4951
mPaint.setStyle(Style.FILL);
50-
mPaint.setColor(colorPrimary);
52+
mPaint.setColor(colorPrimaryDark);
5153

5254
mFocusedPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
5355
mFocusedPaint.setStyle(Style.FILL);
54-
mFocusedPaint.setColor(colorPrimaryDark);
56+
mFocusedPaint.setColor(colorPrimary);
5557

5658
mBounds = new Rect();
5759
}
5860

5961
@Override
6062
protected void onBoundsChange(Rect bounds) {
63+
super.onBoundsChange(bounds);
6164
mBounds.set(bounds);
6265
if (mStartAfterBoundsSet) {
6366
animateFocus();
@@ -77,32 +80,31 @@ public int getIntrinsicWidth() {
7780

7881
@Override
7982
public void draw(Canvas canvas) {
83+
8084
final Rect bounds = getBounds();
8185
final int saveCount = canvas.save();
86+
8287
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+
}
8496

8597
if (mAnimateFromCenter) {
8698
int w = getIntrinsicWidth();
8799
float left = ((float) (w - mWidth)) / 2.0f;
88100
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);
90102
} 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);
92104
}
93105
canvas.restoreToCount(saveCount);
94106
}
95107

96-
@Override
97-
public void setAlpha(int alpha) {
98-
// nothing
99-
}
100-
101-
@Override
102-
public void setColorFilter(ColorFilter cf) {
103-
// nothing
104-
}
105-
106108
@Override
107109
public int getOpacity() {
108110
return PixelFormat.UNKNOWN;
@@ -136,6 +138,16 @@ private void stopAnimation() {
136138
}
137139
}
138140

141+
@Override
142+
public void setAlpha(int alpha) {
143+
// nothing
144+
}
145+
146+
@Override
147+
public void setColorFilter(ColorFilter cf) {
148+
// nothing
149+
}
150+
139151
@Override
140152
public void onViewAttachedToWindow(View view) {
141153
// nothing

lib/src/main/java/ext/extensions/ExtensionManager.java

+28-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,34 @@ public static <V extends View> List<ViewExtension<V>> getExtensions(Context cont
4141
for (Entry<int[], Class<? extends ViewExtension>> entry : sExtensionMap.entrySet()) {
4242
TypedArray array = context.obtainStyledAttributes(attrs, entry.getKey(), defStyleAttr, defStyleRes);
4343
if (array.getIndexCount() > 0) {
44-
extensions.add(newExtension(entry.getValue()));
44+
Class<? extends ViewExtension> clazz = entry.getValue();
45+
boolean add = false;
46+
47+
if (FontExtension.class == clazz) {
48+
int fontFamilyAttr = FontExtension.obtainTextAppearanceFontFamily();
49+
if (array.getIndex(fontFamilyAttr) > 0) {
50+
String fontFamily = FontExtension.obtainTextAppearanceFontFamily(context, attrs, defStyleAttr, defStyleRes);
51+
if (fontFamily != null) {
52+
add = true;
53+
}
54+
}
55+
56+
} else if (BorderExtension.class == clazz &&
57+
array.getDimension(R.styleable.BorderExtension_borderWidth, 0) > 0) {
58+
add = true;
59+
60+
} else if (PushButtonExtension.class == clazz &&
61+
array.getDimension(R.styleable.PushButtonExtension_pushDepth, 0) > 0) {
62+
add = true;
63+
64+
} else if (AnimatedBackgroundExtension.class == clazz &&
65+
array.getString(R.styleable.AnimatedBackgroundExtension_drawableClass) != null) {
66+
add = true;
67+
}
68+
69+
if (add) {
70+
extensions.add(newExtension(clazz));
71+
}
4572
}
4673
array.recycle();
4774
}

0 commit comments

Comments
 (0)