Skip to content

Commit

Permalink
优化
Browse files Browse the repository at this point in the history
  • Loading branch information
gedoor committed Jan 8, 2019
1 parent d9a1a97 commit b390bd9
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 187 deletions.
176 changes: 19 additions & 157 deletions app/src/main/java/com/kunfei/bookshelf/utils/Selector.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,12 @@ public static DrawableSelector drawableBuild() {
* created at 2017/12/11 22:26
*/
public static final class ShapeSelector {

// 只支持长方形、圆形
@IntDef({GradientDrawable.RECTANGLE})
@IntDef({GradientDrawable.RECTANGLE, GradientDrawable.OVAL,
GradientDrawable.LINE, GradientDrawable.RING})
private @interface Shape {
}

private int mShape; //the shape of background
private int mCornerRadius; //corner radius

private int mDefaultBgColor; //default background color
private int mDisabledBgColor; //state_enabled = false
private int mPressedBgColor; //state_pressed = true
Expand All @@ -54,29 +51,7 @@ public static final class ShapeSelector {
private int mPressedStrokeColor; //state_pressed = true
private int mSelectedStrokeColor; //state_selected = true
private int mFocusedStrokeColor; //state_focused = true

private int mTLRadius;
private int mTRRadius;
private int mBLRadius;
private int mBRRadius;

// 上边框显示,前提边框的宽度不为0
private boolean mShowTopStroke = true;
// 下边框显示,前提边框的宽度不为0
private boolean mShowBottomStroke = true;
// 左边框显示,前提边框的宽度不为0
private boolean mShowLeftStroke = true;
// 右边框显示,前提边框的宽度不为0
private boolean mShowRightStroke = true;

// 上边框宽度
private int mTopStrokeWidth;
// 下边框宽度
private int mBottomStrokeWidth;
// 左边框宽度
private int mLeftStrokeWidth;
// 右边框宽度
private int mRightStrokeWidth;
private int mCornerRadius; //corner radius

private boolean hasSetDisabledBgColor = false;
private boolean hasSetPressedBgColor = false;
Expand All @@ -88,7 +63,8 @@ public static final class ShapeSelector {
private boolean hasSetSelectedStrokeColor = false;
private boolean hasSetFocusedStrokeColor = false;

private ShapeSelector() {
public ShapeSelector() {
//initialize default values
mShape = GradientDrawable.RECTANGLE;
mDefaultBgColor = Color.TRANSPARENT;
mDisabledBgColor = Color.TRANSPARENT;
Expand All @@ -102,24 +78,6 @@ private ShapeSelector() {
mSelectedStrokeColor = Color.TRANSPARENT;
mFocusedStrokeColor = Color.TRANSPARENT;
mCornerRadius = 0;
mTLRadius = mCornerRadius;
mTRRadius = mCornerRadius;
mBLRadius = mCornerRadius;
mBRRadius = mCornerRadius;
// 刚开始四个边框都要显示
mShowTopStroke = true;
mShowBottomStroke = true;
mShowLeftStroke = true;
mShowRightStroke = true;
// 刚开始四个边框大小
// 上边框宽度
mTopStrokeWidth = mStrokeWidth;
// 下边框宽度
mBottomStrokeWidth = mStrokeWidth;
// 左边框宽度
mLeftStrokeWidth = mStrokeWidth;
// 右边框宽度
mRightStrokeWidth = mStrokeWidth;
}

public ShapeSelector setShape(@Shape int shape) {
Expand All @@ -140,19 +98,6 @@ public ShapeSelector setDefaultBgColor(@ColorInt int color) {
return this;
}

public ShapeSelector setDefaultStrokeColor(@ColorInt int color) {
mDefaultStrokeColor = color;
if (!hasSetDisabledStrokeColor)
mDisabledStrokeColor = color;
if (!hasSetPressedStrokeColor)
mPressedStrokeColor = color;
if (!hasSetSelectedStrokeColor)
mSelectedStrokeColor = color;
if (!hasSetFocusedStrokeColor)
mFocusedStrokeColor = color;
return this;
}

public ShapeSelector setDisabledBgColor(@ColorInt int color) {
mDisabledBgColor = color;
hasSetDisabledBgColor = true;
Expand All @@ -173,20 +118,25 @@ public ShapeSelector setSelectedBgColor(@ColorInt int color) {

public ShapeSelector setFocusedBgColor(@ColorInt int color) {
mFocusedBgColor = color;
hasSetFocusedBgColor = true;
hasSetPressedBgColor = true;
return this;
}

public ShapeSelector setStrokeWidth(@Dimension int width) {
mStrokeWidth = width;
mTopStrokeWidth = mStrokeWidth;
mBottomStrokeWidth = mStrokeWidth;
mLeftStrokeWidth = mStrokeWidth;
mRightStrokeWidth = mStrokeWidth;
mShowTopStroke = true;
mShowBottomStroke = true;
mShowLeftStroke = true;
mShowRightStroke = true;
return this;
}

public ShapeSelector setDefaultStrokeColor(@ColorInt int color) {
mDefaultStrokeColor = color;
if (!hasSetDisabledStrokeColor)
mDisabledStrokeColor = color;
if (!hasSetPressedStrokeColor)
mPressedStrokeColor = color;
if (!hasSetSelectedStrokeColor)
mSelectedStrokeColor = color;
if (!hasSetFocusedStrokeColor)
mFocusedStrokeColor = color;
return this;
}

Expand Down Expand Up @@ -216,86 +166,6 @@ public ShapeSelector setFocusedStrokeColor(@ColorInt int color) {

public ShapeSelector setCornerRadius(@Dimension int radius) {
mCornerRadius = radius;
mTLRadius = mCornerRadius;
mTRRadius = mCornerRadius;
mBLRadius = mCornerRadius;
mBRRadius = mCornerRadius;
return this;
}

public ShapeSelector setTLRadius(@Dimension int tLRadius) {
mTLRadius = tLRadius;
return this;
}

public ShapeSelector setTRRadius(@Dimension int tRadius) {
mTRRadius = tRadius;
return this;
}

public ShapeSelector setBLRadius(@Dimension int bLRadius) {
mBLRadius = bLRadius;
return this;
}

public ShapeSelector setBRRadius(@Dimension int bRRadius) {
mBRRadius = bRRadius;
return this;
}

public ShapeSelector setShowTopStroke(boolean showTopStroke) {
this.mShowTopStroke = showTopStroke;
if (!mShowTopStroke)
mTopStrokeWidth = 0;
return this;
}

public ShapeSelector setShowBottomStroke(boolean showBottomStroke) {
this.mShowBottomStroke = showBottomStroke;
if (!mShowBottomStroke)
mBottomStrokeWidth = 0;
return this;
}

public ShapeSelector setShowLeftStroke(boolean showLeftStroke) {
this.mShowLeftStroke = showLeftStroke;
if (!mShowLeftStroke)
mLeftStrokeWidth = 0;
return this;
}

public ShapeSelector setShowRightStroke(boolean showRightStroke) {
this.mShowRightStroke = showRightStroke;
if (!mShowRightStroke)
mRightStrokeWidth = 0;
return this;
}

public ShapeSelector setTopStrokeWidth(int topStrokeWidth) {
this.mTopStrokeWidth = topStrokeWidth;
if (!mShowTopStroke)
mTopStrokeWidth = 0;
return this;
}

public ShapeSelector setBottomStrokeWidth(int bottomStrokeWidth) {
this.mBottomStrokeWidth = bottomStrokeWidth;
if (!mShowBottomStroke)
mBottomStrokeWidth = 0;
return this;
}

public ShapeSelector setLeftStrokeWidth(int leftStrokeWidth) {
this.mLeftStrokeWidth = leftStrokeWidth;
if (!mShowLeftStroke)
mLeftStrokeWidth = 0;
return this;
}

public ShapeSelector setRightStrokeWidth(int rightStrokeWidth) {
this.mRightStrokeWidth = rightStrokeWidth;
if (!mShowRightStroke)
mRightStrokeWidth = 0;
return this;
}

Expand Down Expand Up @@ -347,14 +217,6 @@ private GradientDrawable getItemShape(int shape, int cornerRadius,
drawable.setColor(solidColor);
return drawable;
}

private GradientDrawable getDrawable(int shape, int solidColor, float[] radii) {
GradientDrawable drawable = new GradientDrawable();
drawable.setShape(shape);
drawable.setCornerRadii(radii);
drawable.setColor(solidColor);
return drawable;
}
}

/**
Expand Down
6 changes: 0 additions & 6 deletions app/src/main/res/color/selector_btn_file_add.xml

This file was deleted.

6 changes: 0 additions & 6 deletions app/src/main/res/color/selector_btn_file_delete.xml

This file was deleted.

6 changes: 0 additions & 6 deletions app/src/main/res/drawable/selector_btn_delete.xml

This file was deleted.

6 changes: 0 additions & 6 deletions app/src/main/res/drawable/shape_corner_accent.xml

This file was deleted.

6 changes: 0 additions & 6 deletions app/src/main/res/drawable/shape_corner_text_color.xml

This file was deleted.

0 comments on commit b390bd9

Please sign in to comment.