Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat:[增加用代码设置背景边框] #135

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions androidx/src/main/java/com/noober/androidx/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
21 changes: 21 additions & 0 deletions androidx/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,27 @@
android:layout_height="20dp"
android:layout_marginTop="5dp" />

<LinearLayout
android:layout_width="match_parent"
android:layout_marginTop="5dp"
android:orientation="horizontal"
android:layout_height="wrap_content">

<TextView
android:id="@+id/tvTest5"
android:gravity="center"
android:layout_width="130dp"
android:layout_height="36dp"
android:text="设置全边框" />

<TextView
android:id="@+id/tvTest6"
android:gravity="center"
android:layout_width="130dp"
android:layout_height="36dp"
android:text="设置右上下边框" />
</LinearLayout>

<FrameLayout
android:id="@+id/fl_content"
android:layout_width="match_parent"
Expand Down
30 changes: 28 additions & 2 deletions app/src/main/java/com/noober/backgroudlibrary/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
import android.widget.Button;
import android.widget.TextView;

import com.noober.background.BackgroundLibrary;
import com.noober.background.annotation.BLUsed;
import com.noober.background.drawable.DrawableCreator;

public class MainActivity extends AppCompatActivity {
Expand Down Expand Up @@ -90,6 +88,34 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
}


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 btnLike = findViewById(R.id.btn_like);
btnLike.setOnClickListener(new View.OnClickListener() {
int i = 1;
Expand Down
21 changes: 21 additions & 0 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -612,6 +612,27 @@
android:layout_height="20dp"
android:layout_marginTop="5dp" />

<LinearLayout
android:layout_width="match_parent"
android:layout_marginTop="5dp"
android:orientation="horizontal"
android:layout_height="wrap_content">

<TextView
android:id="@+id/tvTest5"
android:gravity="center"
android:layout_width="130dp"
android:layout_height="36dp"
android:text="设置全边框" />

<TextView
android:id="@+id/tvTest6"
android:gravity="center"
android:layout_width="130dp"
android:layout_height="36dp"
android:text="设置右上下边框" />
</LinearLayout>

<FrameLayout
android:id="@+id/fl_content"
android:layout_width="match_parent"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,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;
Expand Down Expand Up @@ -52,6 +53,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;
Expand Down Expand Up @@ -80,6 +88,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;
Expand Down Expand Up @@ -255,6 +264,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;
Expand Down Expand Up @@ -508,28 +522,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() {
Expand Down Expand Up @@ -911,6 +937,10 @@ StateListDrawable getStateListDrawable(StateListDrawable stateListDrawable) {
}
return stateListDrawable;
}

private static boolean hasStatus(int flag, int position) {
return (flag & position) == position;
}
}

// 设置drawable的位置
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -910,6 +936,9 @@ StateListDrawable getStateListDrawable(StateListDrawable stateListDrawable) {
return stateListDrawable;
}

private static boolean hasStatus(int flag, int position) {
return (flag & position) == position;
}
}


Expand Down