Skip to content

Commit

Permalink
修复绘制虚线在某些机型上面没有生效的问题
Browse files Browse the repository at this point in the history
修复代码动态设置填充色和边框色没有生效的问题
修复设置了渐变色就无法设置填充色选择器的问题
  • Loading branch information
getActivity committed Aug 26, 2021
1 parent 9bbe8da commit 9142a64
Show file tree
Hide file tree
Showing 21 changed files with 464 additions and 71 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ android {
dependencies {
// Shape 框架:https://github.com/getActivity/ShapeView
implementation 'com.github.getActivity:ShapeView:5.0'
implementation 'com.github.getActivity:ShapeView:5.2'
}
```

Expand Down
Binary file modified ShapeView.apk
Binary file not shown.
11 changes: 9 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ android {
minSdkVersion 16
// noinspection ExpiredTargetSdkVersion
targetSdkVersion 28
versionCode 50
versionName "5.0"
versionCode 52
versionName "5.2"
}

// 支持 Java JDK 8
Expand Down Expand Up @@ -41,6 +41,13 @@ android {
signingConfig signingConfigs.config
}
}

applicationVariants.all { variant ->
// apk 输出文件名配置
variant.outputs.all { output ->
outputFileName = rootProject.getName() + '.apk'
}
}
}

dependencies {
Expand Down
17 changes: 16 additions & 1 deletion app/src/main/java/com/hjq/shape/demo/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,27 @@

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

import com.hjq.shape.view.ShapeButton;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

ShapeButton shapeButton = findViewById(R.id.btn_main_test);
shapeButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
shapeButton.setSolidColor(0xFF000000)
.setStrokeColor(0xFF5A8DDF)
.intoBackground();

shapeButton.setText("颜色已经改变啦");
}
});
}
}
}
56 changes: 44 additions & 12 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,22 @@
app:shape_solidColor="#5A8DDF"
app:shape_solidPressedColor="#AA5A8DDF" />

<com.hjq.shape.view.ShapeButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:clickable="true"
android:gravity="center"
android:padding="10dp"
android:text="渐变色按压效果"
android:textColor="@android:color/white"
android:textSize="14sp"
app:shape="rectangle"
app:shape_endColor="#ED58FF"
app:shape_radius="20dp"
app:shape_solidPressedColor="#5A8DDF"
app:shape_startColor="#49DAFA" />

<com.hjq.shape.view.ShapeButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
Expand Down Expand Up @@ -99,6 +115,22 @@
app:shape_textColor="@android:color/black"
app:shape_textDisabledColor="#BBBBBB" />

<com.hjq.shape.view.ShapeButton
android:id="@+id/btn_main_test"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:gravity="center"
android:padding="10dp"
android:text="点我动态改变颜色"
android:textColor="@android:color/white"
android:textSize="14sp"
app:shape="rectangle"
app:shape_radius="20dp"
app:shape_solidColor="#5A8DDF"
app:shape_strokeColor="#000000"
app:shape_strokeWidth="1dp" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
Expand Down Expand Up @@ -591,8 +623,8 @@
android:textSize="14sp"
app:shape="ring"
app:shape_innerRadiusRatio="4"
app:shape_thicknessRatio="4"
app:shape_solidColor="#80ff0000" />
app:shape_solidColor="#80ff0000"
app:shape_thicknessRatio="4" />

<com.hjq.shape.view.ShapeTextView
android:layout_width="200dp"
Expand All @@ -605,9 +637,9 @@
android:textSize="14sp"
app:shape="ring"
app:shape_innerRadiusRatio="4"
app:shape_thicknessRatio="4"
app:shape_strokeColor="#ffff00ff"
app:shape_strokeWidth="2dp"
app:shape_strokeColor="#ffff00ff" />
app:shape_thicknessRatio="4" />

<com.hjq.shape.view.ShapeTextView
android:layout_width="200dp"
Expand All @@ -620,10 +652,10 @@
android:textSize="14sp"
app:shape="ring"
app:shape_innerRadiusRatio="4"
app:shape_thicknessRatio="4"
app:shape_solidColor="#80ff0000"
app:shape_strokeColor="#ffff00ff"
app:shape_strokeWidth="2dp"
app:shape_strokeColor="#ffff00ff" />
app:shape_thicknessRatio="4" />

<com.hjq.shape.view.ShapeTextView
android:layout_width="200dp"
Expand All @@ -635,13 +667,13 @@
android:textColor="@android:color/black"
android:textSize="14sp"
app:shape="ring"
app:shape_innerRadiusRatio="4"
app:shape_thicknessRatio="4"
app:shape_angle="45"
app:shape_centerColor="#80ff0000"
app:shape_endColor="#ffff0000"
app:shape_gradientType="linear"
app:shape_innerRadiusRatio="4"
app:shape_startColor="#00ff0000"
app:shape_gradientType="linear" />
app:shape_thicknessRatio="4" />

<com.hjq.shape.view.ShapeTextView
android:layout_width="200dp"
Expand All @@ -653,12 +685,12 @@
android:textColor="@android:color/black"
android:textSize="14sp"
app:shape="ring"
app:shape_innerRadiusRatio="4"
app:shape_thicknessRatio="4"
app:shape_centerColor="#80ff0000"
app:shape_endColor="#00ff0000"
app:shape_gradientType="sweep"
app:shape_innerRadiusRatio="4"
app:shape_startColor="#ffff0000"
app:shape_gradientType="sweep" />
app:shape_thicknessRatio="4" />

<TextView
android:layout_width="match_parent"
Expand Down
4 changes: 2 additions & 2 deletions library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ android {

defaultConfig {
minSdkVersion 16
versionCode 50
versionName "5.0"
versionCode 52
versionName "5.2"
}

// 支持 Java JDK 8
Expand Down
37 changes: 20 additions & 17 deletions library/src/main/java/com/hjq/shape/core/IShapeDrawable.java
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,10 @@ default int getStrokeCheckedColor() {

int getDashGap();

default boolean isDashLineEnable() {
return getDashGap() > 0;
}

V setInnerRadius(int radius);

int getInnerRadius();
Expand Down Expand Up @@ -229,12 +233,22 @@ default Drawable buildBackgroundDrawable() {
return null;
}

if (isGradientColor() || getSolidColor() == getSolidPressedColor() && getStrokeColor() == getStrokePressedColor() &&
ShapeDrawable defaultDrawable = createShapeDrawable(getSolidColor(), getStrokeColor());
// 判断是否设置了渐变色
if (isGradientColor()) {
if (getCenterColor() == getSolidColor()) {
defaultDrawable.setColors(new int[]{getStartColor(), getEndColor()});
} else {
defaultDrawable.setColors(new int[]{getStartColor(), getCenterColor(), getEndColor()});
}
}

if (getSolidColor() == getSolidPressedColor() && getStrokeColor() == getStrokePressedColor() &&
getSolidColor() == getSolidCheckedColor() && getStrokeColor() == getStrokeCheckedColor() &&
getSolidColor() == getSolidDisabledColor() && getStrokeColor() == getStrokeDisabledColor() &&
getSolidColor() == getSolidFocusedColor() && getStrokeColor() == getStrokeFocusedColor() &&
getSolidColor() == getSolidSelectedColor() && getStrokeColor() == getStrokeSelectedColor()) {
return createShapeDrawable(getSolidColor(), getStrokeColor());
return defaultDrawable;
}

StateListDrawable drawable = new StateListDrawable();
Expand All @@ -253,30 +267,19 @@ default Drawable buildBackgroundDrawable() {
if (getSolidColor() != getSolidSelectedColor() || getStrokeColor() != getStrokeSelectedColor()) {
drawable.addState(new int[]{android.R.attr.state_selected}, createShapeDrawable(getSolidSelectedColor(), getStrokeSelectedColor()));
}
drawable.addState(new int[]{}, createShapeDrawable(getSolidColor(), getStrokeColor()));

drawable.addState(new int[]{}, defaultDrawable);
return drawable;
}

default Drawable createShapeDrawable(int solidColor, int strokeColor) {
default ShapeDrawable createShapeDrawable(int solidColor, int strokeColor) {
ShapeDrawable drawable = new ShapeDrawable();
drawable.setShape(getShapeType());
drawable.setSize(getShapeWidth(), getShapeHeight());
drawable.setCornerRadii(new float[]{getTopLeftRadius(), getTopLeftRadius(), getTopRightRadius(), getTopRightRadius(),
getBottomRightRadius(), getBottomRightRadius(), getBottomLeftRadius(), getBottomLeftRadius()});

int startColor = getStartColor();
int centerColor = getCenterColor();
int endColor = getEndColor();
if (isGradientColor()) {
if (centerColor == solidColor) {
drawable.setColors(new int[]{startColor, endColor});
} else {
drawable.setColors(new int[]{startColor, centerColor, endColor});
}
} else {
drawable.setColor(solidColor);
}

drawable.setColor(solidColor);
drawable.setGradientCenter(getCenterX(), getCenterY());
drawable.setUseLevel(isUseLevel());
drawable.setGradientAngle(getAngle());
Expand Down
6 changes: 3 additions & 3 deletions library/src/main/java/com/hjq/shape/core/ShapeDrawable.java
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ public void setColor(int argb) {
* 设置渐变颜色
*/
public void setColors(int[] colors) {
mShapeState.setColors(colors);
mShapeState.setGradientColor(colors);
mRectIsDirty = true;
invalidateSelf();
}
Expand Down Expand Up @@ -641,7 +641,7 @@ private boolean ensureValidRect() {
mRect.set(bounds.left + inset + mShapeState.mShadowSize, bounds.top + inset + mShapeState.mShadowSize,
bounds.right - inset - mShapeState.mShadowSize, bounds.bottom - inset - mShapeState.mShadowSize);

final int[] colors = st.mColors;
final int[] colors = st.mGradientColors;
if (colors != null) {
RectF r = mRect;
float x0, x1, y0, y1;
Expand Down Expand Up @@ -765,7 +765,7 @@ public Drawable mutate() {
private void initializeWithState(ShapeState state) {
if (state.mHasSolidColor) {
mFillPaint.setColor(state.mSolidColor);
} else if (state.mColors == null) {
} else if (state.mGradientColors == null) {
// If we don't have a solid color and we don't have a gradient,
// the app is stroking the shape, set the color to the default
// value of state.mSolidColor
Expand Down
18 changes: 9 additions & 9 deletions library/src/main/java/com/hjq/shape/core/ShapeState.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class ShapeState extends Drawable.ConstantState {
public int mShapeType = ShapeType.RECTANGLE;
public int mGradient = ShapeGradientType.LINEAR_GRADIENT;
public ShapeGradientOrientation mOrientation;
public int[] mColors;
public int[] mGradientColors;
public int[] mTempColors; // no need to copy
public float[] mTempPositions; // no need to copy
public float[] mPositions;
Expand Down Expand Up @@ -49,16 +49,16 @@ public class ShapeState extends Drawable.ConstantState {

public ShapeState(ShapeGradientOrientation orientation, int[] colors) {
mOrientation = orientation;
setColors(colors);
setGradientColor(colors);
}

public ShapeState(ShapeState state) {
mChangingConfigurations = state.mChangingConfigurations;
mShapeType = state.mShapeType;
mGradient = state.mGradient;
mOrientation = state.mOrientation;
if (state.mColors != null) {
mColors = state.mColors.clone();
if (state.mGradientColors != null) {
mGradientColors = state.mGradientColors.clone();
}
if (state.mPositions != null) {
mPositions = state.mPositions.clone();
Expand Down Expand Up @@ -124,16 +124,16 @@ public void setGradientCenter(float x, float y) {
mCenterY = y;
}

public void setColors(int[] colors) {
public void setGradientColor(int[] colors) {
mHasSolidColor = false;
mColors = colors;
mGradientColors = colors;
computeOpacity();
}

public void setSolidColor(int argb) {
mHasSolidColor = true;
mSolidColor = argb;
mColors = null;
mGradientColors = null;
computeOpacity();
}

Expand All @@ -158,8 +158,8 @@ private void computeOpacity() {
return;
}

if (mColors != null) {
for (int color : mColors) {
if (mGradientColors != null) {
for (int color : mGradientColors) {
if (!isOpaque(color)) {
mOpaque = false;
return;
Expand Down
Loading

0 comments on commit 9142a64

Please sign in to comment.