diff --git a/README.md b/README.md index 5d707ae..a3e9745 100755 --- a/README.md +++ b/README.md @@ -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) @@ -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' } ``` @@ -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) @@ -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) @@ -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 diff --git a/app/build.gradle b/app/build.gradle old mode 100755 new mode 100644 index 3dbee7a..c6468dc --- a/app/build.gradle +++ b/app/build.gradle @@ -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" } @@ -42,9 +42,9 @@ android { } } - applicationVariants.all { variant -> + applicationVariants.configureEach { variant -> // apk 输出文件名配置 - variant.outputs.all { output -> + variant.outputs.configureEach { output -> outputFileName = rootProject.getName() + '.apk' } } @@ -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' } \ No newline at end of file diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 166bd1f..35650a8 100755 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -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"> diff --git a/build.gradle b/build.gradle old mode 100755 new mode 100644 index 3ddc64c..80d7df3 --- a/build.gradle +++ b/build.gradle @@ -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 } \ No newline at end of file diff --git a/library/build.gradle b/library/build.gradle old mode 100755 new mode 100644 index 1f86f6e..5648d8f --- a/library/build.gradle +++ b/library/build.gradle @@ -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" } } diff --git a/library/src/main/java/com/hjq/shape/drawable/ShapeDrawable.java b/library/src/main/java/com/hjq/shape/drawable/ShapeDrawable.java index cf2006b..556b518 100644 --- a/library/src/main/java/com/hjq/shape/drawable/ShapeDrawable.java +++ b/library/src/main/java/com/hjq/shape/drawable/ShapeDrawable.java @@ -52,6 +52,9 @@ public class ShapeDrawable extends Drawable { private Path mRingPath; private boolean mPathDirty = true; + /** 当前布局方向 */ + private int mLayoutDirection; + public ShapeDrawable() { this(new ShapeState()); } @@ -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(); @@ -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(); @@ -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; } @@ -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; @@ -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)); diff --git a/library/src/main/java/com/hjq/shape/drawable/ShapeDrawableUtils.java b/library/src/main/java/com/hjq/shape/drawable/ShapeDrawableUtils.java index e9e1f8e..cc86217 100644 --- a/library/src/main/java/com/hjq/shape/drawable/ShapeDrawableUtils.java +++ b/library/src/main/java/com/hjq/shape/drawable/ShapeDrawableUtils.java @@ -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 轮子哥 @@ -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; diff --git a/library/src/main/java/com/hjq/shape/drawable/ShapeGradientOrientation.java b/library/src/main/java/com/hjq/shape/drawable/ShapeGradientOrientation.java index 25c0f79..d71cc2d 100644 --- a/library/src/main/java/com/hjq/shape/drawable/ShapeGradientOrientation.java +++ b/library/src/main/java/com/hjq/shape/drawable/ShapeGradientOrientation.java @@ -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 } \ No newline at end of file diff --git a/library/src/main/java/com/hjq/shape/drawable/ShapeGradientOrientationLimit.java b/library/src/main/java/com/hjq/shape/drawable/ShapeGradientOrientationLimit.java deleted file mode 100644 index 6fb8bfa..0000000 --- a/library/src/main/java/com/hjq/shape/drawable/ShapeGradientOrientationLimit.java +++ /dev/null @@ -1,19 +0,0 @@ -package com.hjq.shape.drawable; - -import android.support.annotation.IntDef; - -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; - -/** - * author : Android 轮子哥 - * github : https://github.com/getActivity/ShapeDrawable - * time : 2023/07/16 - * desc : Shape 渐变方向赋值限制 - */ -@IntDef({ShapeGradientOrientation.LEFT_TO_RIGHT, ShapeGradientOrientation.RIGHT_TO_LEFT, - ShapeGradientOrientation.BOTTOM_TO_TOP, ShapeGradientOrientation.TOP_TO_BOTTOM, - ShapeGradientOrientation.TOP_LEFT_TO_BOTTOM_RIGHT, ShapeGradientOrientation.BOTTOM_LEFT_TO_TOP_RIGHT, - ShapeGradientOrientation.TOP_RIGHT_TO_BOTTOM_LEFT, ShapeGradientOrientation.BOTTOM_RIGHT_TO_TOP_LEFT}) -@Retention(RetentionPolicy.SOURCE) -public @interface ShapeGradientOrientationLimit {} \ No newline at end of file diff --git a/library/src/main/java/com/hjq/shape/drawable/ShapeGradientTypeLimit.java b/library/src/main/java/com/hjq/shape/drawable/ShapeGradientTypeLimit.java index 03c8266..d8c0047 100644 --- a/library/src/main/java/com/hjq/shape/drawable/ShapeGradientTypeLimit.java +++ b/library/src/main/java/com/hjq/shape/drawable/ShapeGradientTypeLimit.java @@ -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 {} \ No newline at end of file diff --git a/library/src/main/java/com/hjq/shape/drawable/ShapeState.java b/library/src/main/java/com/hjq/shape/drawable/ShapeState.java index a0c0574..1050a46 100644 --- a/library/src/main/java/com/hjq/shape/drawable/ShapeState.java +++ b/library/src/main/java/com/hjq/shape/drawable/ShapeState.java @@ -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 @@ -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;