diff --git a/androidx/src/main/java/com/noober/androidx/MainActivity.java b/androidx/src/main/java/com/noober/androidx/MainActivity.java
index b7ee03c..57cd8b8 100644
--- a/androidx/src/main/java/com/noober/androidx/MainActivity.java
+++ b/androidx/src/main/java/com/noober/androidx/MainActivity.java
@@ -80,7 +80,32 @@ public void onClick(View v) {
// tvTest4.setBackgroundDrawable(drawable4);
// }
+ TextView tvTest5 = findViewById(R.id.tvTest5);
+ Drawable drawable5 = new DrawableCreator.Builder()
+ .setSolidColor(Color.parseColor("#E3B666"))
+ .setStrokeColor(Color.parseColor("#8c6822"))
+ .setStrokeWidth(dip2px(0.5f))
+ .build();
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
+ tvTest5.setBackground(drawable5);
+ } else {
+ tvTest5.setBackgroundDrawable(drawable5);
+ }
+ TextView tvTest6 = findViewById(R.id.tvTest6);
+ Drawable drawable6 = new DrawableCreator.Builder()
+ .setSolidColor(Color.parseColor("#E3B666"))
+ .setStrokeColor(Color.parseColor("#8c6822"))
+ .setStrokeWidth(dip2px(0.5f))
+ .setStrokePosition(DrawableCreator.StrokePosition.Right
+ | DrawableCreator.StrokePosition.Top
+ | DrawableCreator.StrokePosition.Bottom)
+ .build();
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
+ tvTest6.setBackground(drawable6);
+ } else {
+ tvTest6.setBackgroundDrawable(drawable6);
+ }
final Button btnEnable = findViewById(R.id.btn_setEnable);
final TextView tvMulti = findViewById(R.id.tv_multi);
btnEnable.setOnClickListener(new View.OnClickListener() {
diff --git a/androidx/src/main/res/layout/activity_main.xml b/androidx/src/main/res/layout/activity_main.xml
index bc4eb07..40968d6 100644
--- a/androidx/src/main/res/layout/activity_main.xml
+++ b/androidx/src/main/res/layout/activity_main.xml
@@ -374,6 +374,27 @@
android:layout_height="20dp"
android:layout_marginTop="5dp" />
+
+
+
+
+
+
+
= Build.VERSION_CODES.JELLY_BEAN) {
+ tvTest5.setBackground(drawable5);
+ } else {
+ tvTest5.setBackgroundDrawable(drawable5);
+ }
+
+ TextView tvTest6 = findViewById(R.id.tvTest6);
+ Drawable drawable6 = new DrawableCreator.Builder()
+ .setSolidColor(Color.parseColor("#E3B666"))
+ .setStrokeColor(Color.parseColor("#8c6822"))
+ .setStrokeWidth(dip2px(0.5f))
+ .setStrokePosition(DrawableCreator.StrokePosition.Right
+ | DrawableCreator.StrokePosition.Top
+ | DrawableCreator.StrokePosition.Bottom)
+ .build();
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
+ tvTest6.setBackground(drawable6);
+ } else {
+ tvTest6.setBackgroundDrawable(drawable6);
+ }
+
+
final Button btnLike = findViewById(R.id.btn_like);
btnLike.setOnClickListener(new View.OnClickListener() {
int i = 1;
diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml
index 23b2105..978dc70 100644
--- a/app/src/main/res/layout/activity_main.xml
+++ b/app/src/main/res/layout/activity_main.xml
@@ -612,6 +612,27 @@
android:layout_height="20dp"
android:layout_marginTop="5dp" />
+
+
+
+
+
+
+
= Build.VERSION_CODES.LOLLIPOP) {
- Drawable contentDrawable = (stateListDrawable == null ? drawable : stateListDrawable);
- return new RippleDrawable(ColorStateList.valueOf(rippleColor), contentDrawable, contentDrawable);
+ Drawable contentDrawable = (stateListDrawable == null ? gradientDrawable : stateListDrawable);
+ drawable = new RippleDrawable(ColorStateList.valueOf(rippleColor), contentDrawable, contentDrawable);
} else {
StateListDrawable resultDrawable = new StateListDrawable();
GradientDrawable unPressDrawable = getGradientDrawable();
unPressDrawable.setColor(rippleColor);
- resultDrawable.addState(new int[]{-android.R.attr.state_pressed}, drawable);
+ resultDrawable.addState(new int[]{-android.R.attr.state_pressed}, gradientDrawable);
resultDrawable.addState(new int[]{android.R.attr.state_pressed}, unPressDrawable);
- return resultDrawable;
+ drawable = resultDrawable;
}
}
- return drawable == null ? stateListDrawable : drawable;
+ if (drawable == null) {
+ drawable = gradientDrawable == null ? stateListDrawable : gradientDrawable;
+ }
+ if (strokePosition != null) {
+ float leftValue = hasStatus(strokePosition, StrokePosition.Left) ? 0 : -strokeWidth;
+ float topValue = hasStatus(strokePosition, StrokePosition.Top) ? 0 : -strokeWidth;
+ float rightValue = hasStatus(strokePosition, StrokePosition.Right) ? 0 : -strokeWidth;
+ float bottomValue = hasStatus(strokePosition, StrokePosition.Bottom) ? 0 : -strokeWidth;
+ drawable = new LayerDrawable(new Drawable[]{drawable});
+ ((LayerDrawable) drawable).setLayerInset(0, (int) leftValue, (int) topValue, (int) rightValue, (int) bottomValue);
+ }
+ return drawable;
}
public ColorStateList buildTextColor() {
@@ -911,6 +937,10 @@ StateListDrawable getStateListDrawable(StateListDrawable stateListDrawable) {
}
return stateListDrawable;
}
+
+ private static boolean hasStatus(int flag, int position) {
+ return (flag & position) == position;
+ }
}
// 设置drawable的位置
diff --git a/libraryx/src/main/java/com/noober/background/drawable/DrawableCreator.java b/libraryx/src/main/java/com/noober/background/drawable/DrawableCreator.java
index 82ef1dc..868163a 100644
--- a/libraryx/src/main/java/com/noober/background/drawable/DrawableCreator.java
+++ b/libraryx/src/main/java/com/noober/background/drawable/DrawableCreator.java
@@ -5,6 +5,7 @@
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
+import android.graphics.drawable.LayerDrawable;
import android.graphics.drawable.RippleDrawable;
import android.graphics.drawable.StateListDrawable;
import android.os.Build;
@@ -50,6 +51,13 @@ public enum DrawablePosition {
}
+ public static class StrokePosition {
+ public static final int Left = 1 << 1;
+ public static final int Top = 1 << 2;
+ public static final int Right = 1 << 3;
+ public static final int Bottom = 1 << 4;
+ }
+
public static class Builder {
private Shape shape = Shape.Rectangle;
private Integer solidColor;
@@ -78,6 +86,7 @@ public static class Builder {
private float strokeDashGap = 0;
private boolean rippleEnable = false;
private Integer rippleColor;
+ private Integer strokePosition;
private Integer checkableStrokeColor;
private Integer checkedStrokeColor;
@@ -253,6 +262,11 @@ public Builder setRipple(boolean rippleEnable, int rippleColor) {
return this;
}
+ public Builder setStrokePosition(int strokePosition) {
+ this.strokePosition = strokePosition;
+ return this;
+ }
+
public Builder setCheckableStrokeColor(int checkableStrokeColor, int unCheckableStrokeColor) {
this.checkableStrokeColor = checkableStrokeColor;
this.unCheckableStrokeColor = unCheckableStrokeColor;
@@ -506,28 +520,40 @@ public Builder setBaseStateListDrawable(StateListDrawable baseStateListDrawable)
public Drawable build() {
- GradientDrawable drawable = null;
+ Drawable drawable = null;
+ GradientDrawable gradientDrawable = null;
StateListDrawable stateListDrawable = null;
if (hasSelectDrawable) {
stateListDrawable = getStateListDrawable();
} else {
- drawable = getGradientDrawable();
+ gradientDrawable = getGradientDrawable();
}
if (rippleEnable && rippleColor != null) {
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
- Drawable contentDrawable = (stateListDrawable == null ? drawable : stateListDrawable);
- return new RippleDrawable(ColorStateList.valueOf(rippleColor), contentDrawable, contentDrawable);
+ Drawable contentDrawable = (stateListDrawable == null ? gradientDrawable : stateListDrawable);
+ drawable = new RippleDrawable(ColorStateList.valueOf(rippleColor), contentDrawable, contentDrawable);
} else {
StateListDrawable resultDrawable = new StateListDrawable();
GradientDrawable unPressDrawable = getGradientDrawable();
unPressDrawable.setColor(rippleColor);
- resultDrawable.addState(new int[]{-android.R.attr.state_pressed}, drawable);
+ resultDrawable.addState(new int[]{-android.R.attr.state_pressed}, gradientDrawable);
resultDrawable.addState(new int[]{android.R.attr.state_pressed}, unPressDrawable);
- return resultDrawable;
+ drawable = resultDrawable;
}
}
- return drawable == null ? stateListDrawable : drawable;
+ if (drawable == null) {
+ drawable = gradientDrawable == null ? stateListDrawable : gradientDrawable;
+ }
+ if (strokePosition != null) {
+ float leftValue = hasStatus(strokePosition, StrokePosition.Left) ? 0 : -strokeWidth;
+ float topValue = hasStatus(strokePosition, StrokePosition.Top) ? 0 : -strokeWidth;
+ float rightValue = hasStatus(strokePosition, StrokePosition.Right) ? 0 : -strokeWidth;
+ float bottomValue = hasStatus(strokePosition, StrokePosition.Bottom) ? 0 : -strokeWidth;
+ drawable = new LayerDrawable(new Drawable[]{drawable});
+ ((LayerDrawable) drawable).setLayerInset(0, (int) leftValue, (int) topValue, (int) rightValue, (int) bottomValue);
+ }
+ return drawable;
}
public ColorStateList buildTextColor() {
@@ -910,6 +936,9 @@ StateListDrawable getStateListDrawable(StateListDrawable stateListDrawable) {
return stateListDrawable;
}
+ private static boolean hasStatus(int flag, int position) {
+ return (flag & position) == position;
+ }
}