Skip to content

Commit

Permalink
适配 Drawable 渐变色布局反方向特性
Browse files Browse the repository at this point in the history
  • Loading branch information
getActivity committed Jul 14, 2024
1 parent 5c87c61 commit 36f6780
Show file tree
Hide file tree
Showing 11 changed files with 98 additions and 76 deletions.
20 changes: 4 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

* 项目地址:[Github](https://github.com/getActivity/ShapeDrawable)

* 可以扫码下载 Demo 进行演示或者测试,如果扫码下载不了的,[点击此处可直接下载](https://github.com/getActivity/ShapeDrawable/releases/download/3.0/ShapeDrawable.apk)
* 可以扫码下载 Demo 进行演示或者测试,如果扫码下载不了的,[点击此处可直接下载](https://github.com/getActivity/ShapeDrawable/releases/download/3.2/ShapeDrawable.apk)

![](picture/demo_code.png)

Expand Down Expand Up @@ -45,7 +45,7 @@ android {
dependencies {
// ShapeDrawable:https://github.com/getActivity/ShapeDrawable
implementation 'com.github.getActivity:ShapeDrawable:3.0'
implementation 'com.github.getActivity:ShapeDrawable:3.2'
}
```

Expand Down Expand Up @@ -80,7 +80,7 @@ setSolidColor(@ColorInt int... colors)
setSolidGradientType(@ShapeGradientTypeLimit int type)

// 设置填充色渐变方向
setSolidGradientOrientation(@ShapeGradientOrientationLimit int orientation)
setSolidGradientOrientation(ShapeGradientOrientation orientation)

// 设置填充色渐变中心 X 点坐标的相对位置(默认值为 0.5)
setSolidGradientCenterX(float centerX)
Expand All @@ -100,7 +100,7 @@ setStrokeColor(@ColorInt int startColor, @ColorInt int centerColor, @ColorInt in
setStrokeColor(@ColorInt int... colors)

// 设置边框色渐变方向
setStrokeGradientOrientation(@ShapeGradientOrientationLimit int orientation)
setStrokeGradientOrientation(ShapeGradientOrientation orientation)

// 设置边框大小
setStrokeSize(int size)
Expand Down Expand Up @@ -215,18 +215,6 @@ intoBackground(View view)

#### [点击查看捐赠列表](https://github.com/getActivity/Donate)

#### 广告区

* 我现在任腾讯云服务器推广大使,大家如果有购买服务器的需求,可以通过下面的链接购买

[![](https://upload-dianshi-1255598498.file.myqcloud.com/upload/nodir/345X200-9ae456f58874df499adf7c331c02cb0fed12b81d.jpg)](https://curl.qcloud.com/A6cYskvv)

[【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中](https://curl.qcloud.com/A6cYskvv)

[![](https://upload-dianshi-1255598498.file.myqcloud.com/345-200-b28f7dee9552f4241ea6a543f15a9798049701d4.jpg)](https://curl.qcloud.com/up4fQsdn)

[【腾讯云】中小企业福利专场,多款刚需产品,满足企业通用场景需求](https://curl.qcloud.com/up4fQsdn)

## License

```text
Expand Down
10 changes: 5 additions & 5 deletions app/build.gradle
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ android {
applicationId "com.hjq.shape.drawable.demo"
minSdkVersion 16
targetSdkVersion 28
versionCode 30
versionName "3.0"
versionCode 32
versionName "3.2"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}

Expand Down Expand Up @@ -42,9 +42,9 @@ android {
}
}

applicationVariants.all { variant ->
applicationVariants.configureEach { variant ->
// apk 输出文件名配置
variant.outputs.all { output ->
variant.outputs.configureEach { output ->
outputFileName = rootProject.getName() + '.apk'
}
}
Expand All @@ -63,5 +63,5 @@ dependencies {
implementation 'com.github.getActivity:TitleBar:10.5'

// 吐司框架:https://github.com/getActivity/Toaster
implementation 'com.github.getActivity:Toaster:12.3'
implementation 'com.github.getActivity:Toaster:12.5'
}
1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
Expand Down
21 changes: 18 additions & 3 deletions build.gradle
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,25 @@ allprojects {
jcenter()
}

// 将构建文件统一输出到项目根目录下的 build 文件夹
setBuildDir(new File(rootDir, "build/${path.replaceAll(':', '/')}"))
// 读取 local.properties 文件配置
def properties = new Properties()
def localPropertiesFile = rootProject.file("local.properties")
if (localPropertiesFile.exists()) {
localPropertiesFile.withInputStream { inputStream ->
properties.load(inputStream)
}
}

String buildDirPath = properties.getProperty("build.dir")
if (buildDirPath != null && buildDirPath != "") {
// 将构建文件统一输出到指定的目录下
setBuildDir(new File(buildDirPath, rootProject.name + "/build/${path.replaceAll(':', '/')}"))
} else {
// 将构建文件统一输出到项目根目录下的 build 文件夹
setBuildDir(new File(rootDir, "build/${path.replaceAll(':', '/')}"))
}
}

task clean(type: Delete) {
tasks.register('clean', Delete) {
delete rootProject.buildDir
}
8 changes: 4 additions & 4 deletions library/build.gradle
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ android {

defaultConfig {
minSdkVersion 16
versionCode 30
versionName "3.0"
versionCode 32
versionName "3.2"
}

android.libraryVariants.all { variant ->
android.libraryVariants.configureEach { variant ->
// aar 输出文件名配置
variant.outputs.all { output ->
variant.outputs.configureEach { output ->
outputFileName = "${rootProject.name}-${android.defaultConfig.versionName}.aar"
}
}
Expand Down
12 changes: 8 additions & 4 deletions library/src/main/java/com/hjq/shape/drawable/ShapeDrawable.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ public class ShapeDrawable extends Drawable {
private Path mRingPath;
private boolean mPathDirty = true;

/** 当前布局方向 */
private int mLayoutDirection;

public ShapeDrawable() {
this(new ShapeState());
}
Expand Down Expand Up @@ -194,7 +197,7 @@ public ShapeDrawable setSolidGradientType(@ShapeGradientTypeLimit int type) {
/**
* 设置填充色渐变方向
*/
public ShapeDrawable setSolidGradientOrientation(@ShapeGradientOrientationLimit int orientation) {
public ShapeDrawable setSolidGradientOrientation(ShapeGradientOrientation orientation) {
mShapeState.solidGradientOrientation = orientation;
mRectDirty = true;
invalidateSelf();
Expand Down Expand Up @@ -259,7 +262,7 @@ public ShapeDrawable setStrokeColor(@ColorInt int... colors) {
/**
* 设置边框色渐变方向
*/
public ShapeDrawable setStrokeGradientOrientation(@ShapeGradientOrientationLimit int orientation) {
public ShapeDrawable setStrokeGradientOrientation(ShapeGradientOrientation orientation) {
mShapeState.strokeGradientOrientation = orientation;
mRectDirty = true;
invalidateSelf();
Expand Down Expand Up @@ -681,6 +684,7 @@ of the fill (if any) without worrying about blending artifacts.

@Override
public boolean onLayoutDirectionChanged(int layoutDirection) {
mLayoutDirection = layoutDirection;
return mShapeState.shapeType == ShapeType.LINE;
}

Expand Down Expand Up @@ -862,7 +866,7 @@ private boolean ensureValidRect() {
case ShapeGradientType.LINEAR_GRADIENT: {
final float level = st.useLevel ? getLevel() / 10000f : 1f;
float[] coordinate = ShapeDrawableUtils.computeLinearGradientCoordinate(
mRect, level, st.solidGradientOrientation);
mLayoutDirection, mRect, level, st.solidGradientOrientation);
mSolidPaint.setShader(new LinearGradient(coordinate[0], coordinate[1], coordinate[2], coordinate[3],
st.solidColors, st.positions, Shader.TileMode.CLAMP));
break;
Expand Down Expand Up @@ -930,7 +934,7 @@ private boolean ensureValidRect() {
if (st.strokeColors != null) {
final float level = st.useLevel ? getLevel() / 10000f : 1f;
float[] coordinate = ShapeDrawableUtils.computeLinearGradientCoordinate(
mRect, level, st.strokeGradientOrientation);
mLayoutDirection, mRect, level, st.strokeGradientOrientation);
mStrokePaint.setShader(new LinearGradient(coordinate[0], coordinate[1], coordinate[2], coordinate[3],
st.strokeColors, st.positions, Shader.TileMode.CLAMP));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import android.support.annotation.ColorInt;
import android.support.annotation.IntRange;
import android.support.annotation.Nullable;
import android.view.View;

/**
* author : Android 轮子哥
Expand All @@ -24,38 +25,62 @@ static void saveCanvasLayer(Canvas canvas, float left, float top, float right, f
}
}

static float[] computeLinearGradientCoordinate(RectF r, float level, int orientation) {
static float[] computeLinearGradientCoordinate(int layoutDirection, RectF r, float level, ShapeGradientOrientation orientation) {
float x0, x1, y0, y1;
switch (orientation) {
case ShapeGradientOrientation.TOP_TO_BOTTOM:
case START_TO_END:
return computeLinearGradientCoordinate(layoutDirection, r, level,
layoutDirection == View.LAYOUT_DIRECTION_RTL ?
ShapeGradientOrientation.RIGHT_TO_LEFT : ShapeGradientOrientation.LEFT_TO_RIGHT);
case END_TO_START:
return computeLinearGradientCoordinate(layoutDirection, r, level,
layoutDirection == View.LAYOUT_DIRECTION_RTL ?
ShapeGradientOrientation.LEFT_TO_RIGHT : ShapeGradientOrientation.RIGHT_TO_LEFT);
case TOP_START_TO_BOTTOM_END:
return computeLinearGradientCoordinate(layoutDirection, r, level,
layoutDirection == View.LAYOUT_DIRECTION_RTL ?
ShapeGradientOrientation.TOP_RIGHT_TO_BOTTOM_LEFT : ShapeGradientOrientation.TOP_LEFT_TO_BOTTOM_RIGHT);
case TOP_END_TO_BOTTOM_START:
return computeLinearGradientCoordinate(layoutDirection, r, level,
layoutDirection == View.LAYOUT_DIRECTION_RTL ?
ShapeGradientOrientation.TOP_LEFT_TO_BOTTOM_RIGHT : ShapeGradientOrientation.TOP_RIGHT_TO_BOTTOM_LEFT);
case BOTTOM_START_TO_TOP_END:
return computeLinearGradientCoordinate(layoutDirection, r, level,
layoutDirection == View.LAYOUT_DIRECTION_RTL ?
ShapeGradientOrientation.BOTTOM_RIGHT_TO_TOP_LEFT : ShapeGradientOrientation.BOTTOM_LEFT_TO_TOP_RIGHT);
case BOTTOM_END_TO_TOP_START:
return computeLinearGradientCoordinate(layoutDirection, r, level,
layoutDirection == View.LAYOUT_DIRECTION_RTL ?
ShapeGradientOrientation.BOTTOM_LEFT_TO_TOP_RIGHT : ShapeGradientOrientation.BOTTOM_RIGHT_TO_TOP_LEFT);
case TOP_TO_BOTTOM:
x0 = r.left; y0 = r.top;
x1 = x0; y1 = level * r.bottom;
break;
case ShapeGradientOrientation.TOP_RIGHT_TO_BOTTOM_LEFT:
case TOP_RIGHT_TO_BOTTOM_LEFT:
x0 = r.right; y0 = r.top;
x1 = level * r.left; y1 = level * r.bottom;
break;
case ShapeGradientOrientation.RIGHT_TO_LEFT:
case RIGHT_TO_LEFT:
x0 = r.right; y0 = r.top;
x1 = level * r.left; y1 = y0;
break;
case ShapeGradientOrientation.BOTTOM_RIGHT_TO_TOP_LEFT:
case BOTTOM_RIGHT_TO_TOP_LEFT:
x0 = r.right; y0 = r.bottom;
x1 = level * r.left; y1 = level * r.top;
break;
case ShapeGradientOrientation.BOTTOM_TO_TOP:
case BOTTOM_TO_TOP:
x0 = r.left; y0 = r.bottom;
x1 = x0; y1 = level * r.top;
break;
case ShapeGradientOrientation.BOTTOM_LEFT_TO_TOP_RIGHT:
case BOTTOM_LEFT_TO_TOP_RIGHT:
x0 = r.left; y0 = r.bottom;
x1 = level * r.right; y1 = level * r.top;
break;
case ShapeGradientOrientation.LEFT_TO_RIGHT:
case LEFT_TO_RIGHT:
x0 = r.left; y0 = r.top;
x1 = level * r.right; y1 = y0;
break;
case ShapeGradientOrientation.TOP_LEFT_TO_BOTTOM_RIGHT:
case TOP_LEFT_TO_BOTTOM_RIGHT:
default:
x0 = r.left; y0 = r.top;
x1 = level * r.right; y1 = level * r.bottom;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,37 @@
* time : 2021/08/15
* desc : Shape 渐变方向
*/
public final class ShapeGradientOrientation {
public enum ShapeGradientOrientation {

/** 从左到右绘制渐变(0 度) */
public static final int LEFT_TO_RIGHT = 0;
LEFT_TO_RIGHT,
START_TO_END,

/** 从右到左绘制渐变(180 度) */
public static final int RIGHT_TO_LEFT = 180;
RIGHT_TO_LEFT,
END_TO_START,

/** 从下到上绘制渐变(90 度) */
public static final int BOTTOM_TO_TOP = 90;
BOTTOM_TO_TOP,

/** 从上到下绘制渐变(270 度) */
public static final int TOP_TO_BOTTOM = 270;
TOP_TO_BOTTOM,

// ------------------------------ //

/** 从左上角到右下角绘制渐变(315 度) */
public static final int TOP_LEFT_TO_BOTTOM_RIGHT = 315;

/** 从左下角到右上角绘制渐变(45 度) */
public static final int BOTTOM_LEFT_TO_TOP_RIGHT = 45;
TOP_LEFT_TO_BOTTOM_RIGHT,
TOP_START_TO_BOTTOM_END,

/** 从右上角到左下角绘制渐变(225 度) */
public static final int TOP_RIGHT_TO_BOTTOM_LEFT = 225;
TOP_RIGHT_TO_BOTTOM_LEFT,
TOP_END_TO_BOTTOM_START,

/** 从左下角到右上角绘制渐变(45 度) */
BOTTOM_LEFT_TO_TOP_RIGHT,
BOTTOM_START_TO_TOP_END,

/** 从右下角到左上角绘制渐变(135 度) */
public static final int BOTTOM_RIGHT_TO_TOP_LEFT = 135;
BOTTOM_RIGHT_TO_TOP_LEFT,
BOTTOM_END_TO_TOP_START
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
* time : 2023/07/16
* desc : Shape 渐变类型赋值限制
*/
@IntDef({ShapeGradientType.LINEAR_GRADIENT, ShapeGradientType.RADIAL_GRADIENT, ShapeGradientType.SWEEP_GRADIENT})
@IntDef({
ShapeGradientType.LINEAR_GRADIENT,
ShapeGradientType.RADIAL_GRADIENT,
ShapeGradientType.SWEEP_GRADIENT
})
@Retention(RetentionPolicy.SOURCE)
public @interface ShapeGradientTypeLimit {}
6 changes: 2 additions & 4 deletions library/src/main/java/com/hjq/shape/drawable/ShapeState.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ public class ShapeState extends Drawable.ConstantState {
public int shapeType = ShapeType.RECTANGLE;
@ShapeGradientTypeLimit
public int solidGradientType = ShapeGradientType.LINEAR_GRADIENT;
@ShapeGradientOrientationLimit
public int solidGradientOrientation = ShapeGradientOrientation.TOP_TO_BOTTOM;
public ShapeGradientOrientation solidGradientOrientation = ShapeGradientOrientation.TOP_TO_BOTTOM;
public int[] solidColors;
public int[] strokeColors;
public int[] tempSolidColors; // no need to copy
Expand All @@ -29,8 +28,7 @@ public class ShapeState extends Drawable.ConstantState {
public boolean hasStrokeColor;
public int solidColor;
public int strokeSize = -1; // if >= 0 use stroking.
@ShapeGradientOrientationLimit
public int strokeGradientOrientation = ShapeGradientOrientation.TOP_TO_BOTTOM;
public ShapeGradientOrientation strokeGradientOrientation = ShapeGradientOrientation.TOP_TO_BOTTOM;
public int strokeColor;
public float strokeDashSize;
public float strokeDashGap;
Expand Down

0 comments on commit 36f6780

Please sign in to comment.