diff --git a/HelpDoc.md b/HelpDoc.md new file mode 100644 index 0000000..9061176 --- /dev/null +++ b/HelpDoc.md @@ -0,0 +1,15 @@ +#### + +* 框架上线有很多人说框架的侵入性很强?这点我必须承认,我也有看到网上也有用 `LayoutInflater.Factory` 来实现,这样入侵性很低,但是有一个致命的缺点,无法在布局中预览,这样你是不是突然就感觉不香了?入侵强当然有缺点也有优点,我不能光看它坏的一面,那样看待问题就太片面了,不过它的缺点并不是致命的,就好比你使用了一个自定义 View 叫 `XxxTextView`,这种情况下你肯定就没办法再使用 **ShapeTextView** 了,那么这种情况我们该这么办?解决方式大致分为两种: + + * 第一种可以用原生的 Shape 来实现,可以选择在 xml 定义或者代码动态设置的方式,这种方式大家应该都懂,这里不再多说,不过有一个问题,就是原生的 Shape 是不支持设置阴影的,如果你想要用阴影的话,就得用第二种方式。 + + * 第二种就是用框架提供的 **ShapeDrawable** 类了,在 Java 代码中进行动态设置,这个类的用法其实很简单,在布局用哪个属性,在代码中就用哪个方法。 + + * 另外有一个需要注意的点,如果你自己单独使用 **GradientDrawable** 还是 **ShapeDrawable** 在 Java 代码动态设置的话,如果涉及到虚线或者阴影的话,经过验证在有些手机上面是无法生效的,必须要先关闭硬件加速才能生效,当然 **ShapeDrawable** 有对外开放 **intoBackground** 方法,这个方法会帮你判断是否需要关闭硬件加速。 + +* 另外还有人要框架做裁剪子 View 的功能?在此我表示拒绝,因为我对框架定位很明确,只是为了帮助大家少写 xml,你现在让我加一个裁剪的功能进去,这样合适吗?不,这样不合适,我个人建议裁剪子 View 可以考虑使用 Google 支持库提供的 **CardView** 来实现,有必要时可以搭配 **ShapeDrawable** 来食用。 + +* 最后我来跟大家分享一下我的观点,我认为做好一个框架并不意味着什么功能都要做,并不是我实现不了,而是有没有必要那么做,如果那样做到最后框架很可能会变成一个杂货铺,连作者都会分不清楚这个框架到底是干嘛的,比如我做[标题栏框架](https://github.com/getActivity/TitleBar)的时候,有很多人让我做沉浸式状态栏的功能,我全部给拒绝了,并建议他们单独集成沉浸式框架来实现,首先我要声明一点,我做的是标题栏框架,沉浸式状态栏是沉浸式框架应该有的功能,我要是破格做了沉浸式状态栏的功能,后面就会有人找我做沉浸式底部导航栏的功能,再后面就会有让我做一个状态栏字体变色的功能..............,如果这些我都做到了,那么请问你是否还会使用这样的框架,将标题栏和沉浸式相互捆绑的框架?到底应该叫标题栏框架还是沉浸式框架?后面如果有人说我只想用标题栏的功能不想用沉浸式的功能该怎么办? + +* 听到这里,我相信你大概能理解我的想法了,我在做框架时注入了很多思考,改代码的时间远远超过写代码的时间,思考问题的时间远远超过改代码的时间。做好一个框架不仅仅只是写代码那么简单,更多的是我知道大家的想法是什么,也明白大家想要什么效果,同时我会虚心接受各种建议,但是同样的,我也会拒绝一切不合理的需求。 \ No newline at end of file diff --git a/README.md b/README.md index 72b3879..e5d48bb 100644 --- a/README.md +++ b/README.md @@ -36,7 +36,7 @@ android { dependencies { // Shape 框架:https://github.com/getActivity/ShapeView - implementation 'com.github.getActivity:ShapeView:5.2' + implementation 'com.github.getActivity:ShapeView:6.0' } ``` @@ -53,6 +53,30 @@ android.enableJetifier = true #### 框架文档 +* Java 代码设置 + +```java +ShapeButton shapeButton = findViewById(R.id.btn_main_test); +shapeButton.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + + shapeButton.getShapeDrawableBuilder() + .setSolidColor(0xFF000000) + .setStrokeColor(0xFF5A8DDF) + .intoBackground(); + + shapeButton.getTextColorBuilder() + .setTextColor(0xFFFFFFFF) + .intoTextColor(); + + shapeButton.setText("颜色已经改变啦"); + } +}); +``` + +* 布局属性大全 + ```xml @@ -89,7 +113,7 @@ android.enableJetifier = true - + @@ -187,631 +211,31 @@ android.enableJetifier = true - -``` - -#### 使用案例 - -![](picture/shape_select_background.jpg) - -```xml - - - - - - - -``` - -![](picture/shape_shadow_background.jpg) - -```xml - - - - - -``` - -![](picture/shape_select_text_gradient.jpg) - -```xml - - - -``` - -![](picture/shape_rectangle.jpg) - -```xml - - - - - - - -``` - -![](picture/shape_rectangle_round.jpg) - -```xml - - - - - - - - - - - -``` - -![](picture/shape_rectangle_gradient.jpg) - -```xml - - - - - - - - - -``` - -![](picture/shape_oval.jpg) - -```xml - - - - - - - - - - - -``` + + + + + + + + + + + + -![](picture/shape_ring.jpg) - -```xml - - - - - - - - - + ``` -![](picture/shape_line.jpg) +* 目前支持这些属性的控件有: -```xml - - - -``` + * View 的子类:ShapeView、ShapeTextView、ShapeButton、ShapeImageView、ShapeRadioButton、ShapeCheckBox、ShapeEditText -* 目前支持的控件有: + * ViewGroup 的子类:ShapeLinearLayout、ShapeFrameLayout、ShapeRelativeLayout、ShapeConstraintLayout、ShapeRecyclerView - * View:ShapeView、ShapeTextView、ShapeButton、ShapeImageView、ShapeRadioButton、ShapeCheckBox、ShapeEditText +#### [常见疑问解答](HelpDoc.md) - * Layout:ShapeLinearLayout、ShapeFrameLayout、ShapeRelativeLayout、ShapeConstraintLayout、ShapeRecyclerView +#### [使用案例文档]((UseDemo.md)) #### 框架亮点 diff --git a/ShapeView.apk b/ShapeView.apk index db15fa3..3462e42 100644 Binary files a/ShapeView.apk and b/ShapeView.apk differ diff --git a/UseDemo.md b/UseDemo.md new file mode 100644 index 0000000..220902f --- /dev/null +++ b/UseDemo.md @@ -0,0 +1,662 @@ +# 使用案例 + +![](picture/shape_select_background.jpg) + +```xml + + + + + + + + + +``` + +![](picture/shape_shadow_background.jpg) + +```xml + + + + + +``` + +![](picture/shape_select_text_gradient.jpg) + +```xml + + + +``` + +![](picture/shape_select_compound_button.jpg) + +```xml + + + +``` + +![](picture/shape_rectangle.jpg) + +```xml + + + + + + + +``` + +![](picture/shape_rectangle_round.jpg) + +```xml + + + + + + + + + + + +``` + +![](picture/shape_rectangle_gradient.jpg) + +```xml + + + + + + + + + +``` + +![](picture/shape_oval.jpg) + +```xml + + + + + + + + + + + +``` + +![](picture/shape_ring.jpg) + +```xml + + + + + + + + + +``` + +![](picture/shape_line.jpg) + +```xml + + + +``` diff --git a/app/build.gradle b/app/build.gradle index a0b454a..e3f5c96 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -8,8 +8,8 @@ android { minSdkVersion 16 // noinspection ExpiredTargetSdkVersion targetSdkVersion 28 - versionCode 52 - versionName "5.2" + versionCode 60 + versionName "6.0" } // 支持 Java JDK 8 @@ -58,5 +58,8 @@ dependencies { implementation 'com.android.support:design:28.0.0' // 标题栏框架:https://github.com/getActivity/TitleBar - implementation 'com.github.getActivity:TitleBar:8.6' + implementation 'com.github.getActivity:TitleBar:9.0' + + // 内存泄漏检测:https://github.com/square/leakcanary + debugImplementation 'com.squareup.leakcanary:leakcanary-android:2.7' } \ No newline at end of file diff --git a/app/src/main/java/com/hjq/shape/demo/MainActivity.java b/app/src/main/java/com/hjq/shape/demo/MainActivity.java index 102ef7b..c51314b 100644 --- a/app/src/main/java/com/hjq/shape/demo/MainActivity.java +++ b/app/src/main/java/com/hjq/shape/demo/MainActivity.java @@ -17,10 +17,16 @@ protected void onCreate(Bundle savedInstanceState) { shapeButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { - shapeButton.setSolidColor(0xFF000000) + + shapeButton.getShapeDrawableBuilder() + .setSolidColor(0xFF000000) .setStrokeColor(0xFF5A8DDF) .intoBackground(); + shapeButton.getTextColorBuilder() + .setTextColor(0xFFFFFFFF) + .intoTextColor(); + shapeButton.setText("颜色已经改变啦"); } }); diff --git a/app/src/main/res/drawable/checkbox_checked_ic.xml b/app/src/main/res/drawable/checkbox_checked_ic.xml new file mode 100644 index 0000000..84ca960 --- /dev/null +++ b/app/src/main/res/drawable/checkbox_checked_ic.xml @@ -0,0 +1,11 @@ + + + + + \ No newline at end of file diff --git a/app/src/main/res/drawable/checkbox_disable_ic.xml b/app/src/main/res/drawable/checkbox_disable_ic.xml new file mode 100644 index 0000000..808b94f --- /dev/null +++ b/app/src/main/res/drawable/checkbox_disable_ic.xml @@ -0,0 +1,12 @@ + + + + + \ No newline at end of file diff --git a/app/src/main/res/drawable/checkbox_normal_ic.xml b/app/src/main/res/drawable/checkbox_normal_ic.xml new file mode 100644 index 0000000..a51984b --- /dev/null +++ b/app/src/main/res/drawable/checkbox_normal_ic.xml @@ -0,0 +1,11 @@ + + + + + \ No newline at end of file diff --git a/app/src/main/res/drawable/radiobutton_checked_ic.xml b/app/src/main/res/drawable/radiobutton_checked_ic.xml new file mode 100644 index 0000000..d337e79 --- /dev/null +++ b/app/src/main/res/drawable/radiobutton_checked_ic.xml @@ -0,0 +1,11 @@ + + + + + \ No newline at end of file diff --git a/app/src/main/res/drawable/radiobutton_disable_ic.xml b/app/src/main/res/drawable/radiobutton_disable_ic.xml new file mode 100644 index 0000000..c6f40e0 --- /dev/null +++ b/app/src/main/res/drawable/radiobutton_disable_ic.xml @@ -0,0 +1,12 @@ + + + + + \ No newline at end of file diff --git a/app/src/main/res/drawable/radiobutton_normal_ic.xml b/app/src/main/res/drawable/radiobutton_normal_ic.xml new file mode 100644 index 0000000..a51984b --- /dev/null +++ b/app/src/main/res/drawable/radiobutton_normal_ic.xml @@ -0,0 +1,11 @@ + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index b75e913..483dee1 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -11,7 +11,7 @@ @@ -229,6 +228,43 @@ app:shape_textGradientOrientation="vertical" app:shape_textStartColor="#49DAFA" /> + + + + + + 0 && typedArray.hasValue(styleable.getSolidCheckedColorStyleable())) { + mSolidCheckedColor = typedArray.getColor(styleable.getSolidCheckedColorStyleable(), Color.TRANSPARENT); + } + if (typedArray.hasValue(styleable.getSolidDisabledColorStyleable())) { + mSolidDisabledColor = typedArray.getColor(styleable.getSolidDisabledColorStyleable(), Color.TRANSPARENT); + } + if (typedArray.hasValue(styleable.getSolidFocusedColorStyleable())) { + mSolidFocusedColor = typedArray.getColor(styleable.getSolidFocusedColorStyleable(), Color.TRANSPARENT); + } + if (typedArray.hasValue(styleable.getSolidSelectedColorStyleable())) { + mSolidSelectedColor = typedArray.getColor(styleable.getSolidSelectedColorStyleable(), Color.TRANSPARENT); + } + + int radius = typedArray.getDimensionPixelSize(styleable.getRadiusStyleable(), 0); + mTopLeftRadius = typedArray.getDimensionPixelSize(styleable.getTopLeftRadiusStyleable(), radius); + mTopRightRadius = typedArray.getDimensionPixelSize(styleable.getTopRightRadiusStyleable(), radius); + mBottomLeftRadius = typedArray.getDimensionPixelSize(styleable.getBottomLeftRadiusStyleable(), radius); + mBottomRightRadius = typedArray.getDimensionPixelSize(styleable.getBottomRightRadiusStyleable(), radius); + + if (typedArray.hasValue(styleable.getStartColorStyleable()) && typedArray.hasValue(styleable.getEndColorStyleable())) { + if (typedArray.hasValue(styleable.getCenterColorStyleable())) { + mGradientColor = new int[] {typedArray.getColor(styleable.getStartColorStyleable(), Color.TRANSPARENT), + typedArray.getColor(styleable.getCenterColorStyleable(), Color.TRANSPARENT), + typedArray.getColor(styleable.getEndColorStyleable(), Color.TRANSPARENT)}; + } else { + mGradientColor = new int[] {typedArray.getColor(styleable.getStartColorStyleable(), Color.TRANSPARENT), + typedArray.getColor(styleable.getEndColorStyleable(), Color.TRANSPARENT)}; + } + } + + mUseLevel = typedArray.getBoolean(styleable.getUseLevelStyleable(), false); + mAngle = (int) typedArray.getFloat(styleable.getAngleStyleable(), 0); + mGradientType = typedArray.getInt(styleable.getGradientTypeStyleable(), ShapeGradientType.LINEAR_GRADIENT); + mCenterX = typedArray.getFloat(styleable.getCenterXStyleable(), 0.5f); + mCenterY = typedArray.getFloat(styleable.getCenterYStyleable(), 0.5f); + mGradientRadius = typedArray.getDimensionPixelSize(styleable.getGradientRadiusStyleable(), radius); + + mStrokeColor = typedArray.getColor(styleable.getStrokeColorStyleable(), Color.TRANSPARENT); + if (typedArray.hasValue(styleable.getStrokePressedColorStyleable())) { + mStrokePressedColor = typedArray.getColor(styleable.getStrokePressedColorStyleable(), Color.TRANSPARENT); + } + if (styleable.getStrokeCheckedColorStyleable() > 0 && typedArray.hasValue(styleable.getStrokeCheckedColorStyleable())) { + mStrokeCheckedColor = typedArray.getColor(styleable.getStrokeCheckedColorStyleable(), Color.TRANSPARENT); + } + if (typedArray.hasValue(styleable.getStrokeDisabledColorStyleable())) { + mStrokeDisabledColor = typedArray.getColor(styleable.getStrokeDisabledColorStyleable(), Color.TRANSPARENT); + } + if (typedArray.hasValue(styleable.getStrokeFocusedColorStyleable())) { + mStrokeFocusedColor = typedArray.getColor(styleable.getStrokeFocusedColorStyleable(), Color.TRANSPARENT); + } + if (typedArray.hasValue(styleable.getStrokeSelectedColorStyleable())) { + mStrokeSelectedColor = typedArray.getColor(styleable.getStrokeSelectedColorStyleable(), Color.TRANSPARENT); + } + + mStrokeWidth = typedArray.getDimensionPixelSize(styleable.getStrokeWidthStyleable(), 0); + mDashWidth = typedArray.getDimensionPixelSize(styleable.getDashWidthStyleable(), 0); + mDashGap = typedArray.getDimensionPixelSize(styleable.getDashGapStyleable(), 0); + + mInnerRadius = typedArray.getDimensionPixelOffset(styleable.getInnerRadiusStyleable(), -1); + mInnerRadiusRatio = typedArray.getFloat(styleable.getInnerRadiusRatioStyleable(), 3.0f); + mThickness = typedArray.getDimensionPixelOffset(styleable.getThicknessStyleable(), -1); + mThicknessRatio = typedArray.getFloat(styleable.getThicknessRatioStyleable(), 9.0f); + + mShadowSize = typedArray.getDimensionPixelSize(styleable.getShadowSizeStyleable(), 0); + mShadowColor = typedArray.getColor(styleable.getShadowColorStyleable(), 0x10000000); + mShadowOffsetX = typedArray.getDimensionPixelOffset(styleable.getShadowOffsetXStyleable(), 0); + mShadowOffsetY = typedArray.getDimensionPixelOffset(styleable.getShadowOffsetYStyleable(), 0); + } + + public ShapeDrawableBuilder setShape(int shape) { + mShape = shape; + return this; + } + + public int getShape() { + return mShape; + } + + public ShapeDrawableBuilder setShapeWidth(int width) { + mShapeWidth = width; + return this; + } + + public int getShapeWidth() { + return mShapeWidth; + } + + public ShapeDrawableBuilder setShapeHeight(int height) { + mShapeHeight = height; + return this; + } + + public int getShapeHeight() { + return mShapeHeight; + } + + public ShapeDrawableBuilder setSolidColor(int color) { + mSolidColor = color; + clearGradientColor(); + return this; + } + + public int getSolidColor() { + return mSolidColor; + } + + public ShapeDrawableBuilder setSolidPressedColor(Integer color) { + mSolidPressedColor = color; + return this; + } + + public Integer getSolidPressedColor() { + return mSolidPressedColor; + } + + public ShapeDrawableBuilder setSolidCheckedColor(Integer color) { + mSolidCheckedColor = color; + return this; + } + + public Integer getSolidCheckedColor() { + return mSolidCheckedColor; + } + + public ShapeDrawableBuilder setSolidDisabledColor(Integer color) { + mSolidDisabledColor = color; + return this; + } + + public Integer getSolidDisabledColor() { + return mSolidDisabledColor; + } + + public ShapeDrawableBuilder setSolidFocusedColor(Integer color) { + mSolidFocusedColor = color; + return this; + } + + public Integer getSolidFocusedColor() { + return mSolidFocusedColor; + } + + public ShapeDrawableBuilder setSolidSelectedColor(Integer color) { + mSolidSelectedColor = color; + return this; + } + + public Integer getSolidSelectedColor() { + return mSolidSelectedColor; + } + + public ShapeDrawableBuilder setRadius(float radius) { + return setRadius(radius, radius, radius, radius); + } + + public ShapeDrawableBuilder setRadius(float topLeftRadius, float topRightRadius, float bottomLeftRadius, float bottomRightRadius) { + mTopLeftRadius = topLeftRadius; + mTopRightRadius = topRightRadius; + mBottomLeftRadius = bottomLeftRadius; + mBottomRightRadius = bottomRightRadius; + return this; + } + + public float getTopLeftRadius() { + return mTopLeftRadius; + } + + public float getTopRightRadius() { + return mTopRightRadius; + } + + public float getBottomLeftRadius() { + return mBottomLeftRadius; + } + + public float getBottomRightRadius() { + return mBottomRightRadius; + } + + public ShapeDrawableBuilder setGradientColor(int[] color) { + mGradientColor = color; + return this; + } + + public int[] getGradientColor() { + return mGradientColor; + } + + public boolean isGradientColor() { + return mGradientColor != null && + mGradientColor.length > 0; + } + + public void clearGradientColor() { + mGradientColor = null; + } + + public ShapeDrawableBuilder setUseLevel(boolean useLevel) { + mUseLevel = useLevel; + return this; + } + + public boolean isUseLevel() { + return mUseLevel; + } + + public ShapeDrawableBuilder setAngle(int angle) { + mAngle = angle; + return this; + } + + public int getAngle() { + return mAngle; + } + + public ShapeDrawableBuilder setGradientType(int type) { + mGradientType = type; + return this; + } + + public int getGradientType() { + return mGradientType; + } + + public ShapeDrawableBuilder setCenterX(float x) { + mCenterX = x; + return this; + } + + public float getCenterX() { + return mCenterX; + } + + public ShapeDrawableBuilder setCenterY(float y) { + mCenterY = y; + return this; + } + + public float getCenterY() { + return mCenterY; + } + + public ShapeDrawableBuilder setGradientRadius(int radius) { + mGradientRadius = radius; + return this; + } + + public int getGradientRadius() { + return mGradientRadius; + } + + public ShapeDrawableBuilder setStrokeColor(int color) { + mStrokeColor = color; + return this; + } + + public int getStrokeColor() { + return mStrokeColor; + } + + public ShapeDrawableBuilder setStrokePressedColor(Integer color) { + mStrokePressedColor = color; + return this; + } + + public Integer getStrokePressedColor() { + return mStrokePressedColor; + } + + public ShapeDrawableBuilder setStrokeCheckedColor(Integer color) { + mStrokeCheckedColor = color; + return this; + } + + public Integer getStrokeCheckedColor() { + return mStrokeCheckedColor; + } + + public ShapeDrawableBuilder setStrokeDisabledColor(Integer color) { + mStrokeDisabledColor = color; + return this; + } + + public Integer getStrokeDisabledColor() { + return mStrokeDisabledColor; + } + + public ShapeDrawableBuilder setStrokeFocusedColor(Integer color) { + mStrokeFocusedColor = color; + return this; + } + + public Integer getStrokeFocusedColor() { + return mStrokeFocusedColor; + } + + public ShapeDrawableBuilder setStrokeSelectedColor(Integer color) { + mStrokeSelectedColor = color; + return this; + } + + public Integer getStrokeSelectedColor() { + return mStrokeSelectedColor; + } + + public ShapeDrawableBuilder setStrokeWidth(int width) { + mStrokeWidth = width; + return this; + } + + public int getStrokeWidth() { + return mStrokeWidth; + } + + public ShapeDrawableBuilder setDashWidth(int width) { + mDashWidth = width; + return this; + } + + public int getDashWidth() { + return mDashWidth; + } + + public ShapeDrawableBuilder setDashGap(int gap) { + mDashGap = gap; + return this; + } + + public int getDashGap() { + return mDashGap; + } + + public boolean isDashLineEnable() { + return mDashGap > 0; + } + + public ShapeDrawableBuilder setInnerRadius(int radius) { + mInnerRadius = radius; + return this; + } + + public int getInnerRadius() { + return mInnerRadius; + } + + public ShapeDrawableBuilder setInnerRadiusRatio(float ratio) { + mInnerRadiusRatio = ratio; + return this; + } + + public float getInnerRadiusRatio() { + return mInnerRadiusRatio; + } + + public ShapeDrawableBuilder setThickness(int size) { + mThickness = size; + return this; + } + + public int getThickness() { + return mThickness; + } + + public ShapeDrawableBuilder setThicknessRatio(float ratio) { + mThicknessRatio = ratio; + return this; + } + + public float getThicknessRatio() { + return mThicknessRatio; + } + + public boolean isShadowEnable() { + return mShadowSize > 0; + } + + public ShapeDrawableBuilder setShadowSize(int size) { + mShadowSize = size; + return this; + } + + public int getShadowSize() { + return mShadowSize; + } + + public ShapeDrawableBuilder setShadowColor(int color) { + mShadowColor = color; + return this; + } + + public int getShadowColor() { + return mShadowColor; + } + + public ShapeDrawableBuilder setShadowOffsetX(int offsetX) { + mShadowOffsetX = offsetX; + return this; + } + + public int getShadowOffsetX() { + return mShadowOffsetX; + } + + public ShapeDrawableBuilder setShadowOffsetY(int offsetY) { + mShadowOffsetY = offsetY; + return this; + } + + public int getShadowOffsetY() { + return mShadowOffsetY; + } + + public Drawable buildBackgroundDrawable() { + if (!isGradientColor() && mSolidColor == Color.TRANSPARENT && mStrokeColor == Color.TRANSPARENT) { + return null; + } + + ShapeDrawable defaultDrawable = createShapeDrawable(mSolidColor, mStrokeColor); + // 判断是否设置了渐变色 + if (isGradientColor()) { + defaultDrawable.setGradientColors(mGradientColor); + } + + if (mSolidPressedColor != null && mStrokePressedColor != null && + mSolidCheckedColor != null && mStrokeCheckedColor != null && + mSolidDisabledColor != null && mStrokeDisabledColor != null && + mSolidFocusedColor != null && mStrokeFocusedColor != null && + mSolidSelectedColor != null && mStrokeSelectedColor != null) { + return defaultDrawable; + } + + StateListDrawable drawable = new StateListDrawable(); + if (mSolidPressedColor != null || mStrokePressedColor != null) { + drawable.addState(new int[]{android.R.attr.state_pressed}, createShapeDrawable( + mSolidPressedColor != null ? mSolidPressedColor : mSolidColor, + mStrokePressedColor != null ? mStrokePressedColor : mStrokeColor)); + } + if (mSolidCheckedColor != null || mStrokeCheckedColor != null) { + drawable.addState(new int[]{android.R.attr.state_checked}, createShapeDrawable( + mSolidCheckedColor != null ? mSolidCheckedColor : mSolidColor, + mStrokeCheckedColor != null ? mStrokeCheckedColor : mStrokeColor)); + } + if (mSolidDisabledColor != null || mStrokeDisabledColor != null) { + drawable.addState(new int[]{-android.R.attr.state_enabled}, createShapeDrawable( + mSolidDisabledColor != null ? mSolidDisabledColor : mSolidColor, + mStrokeDisabledColor != null ? mStrokeDisabledColor : mStrokeColor)); + } + if (mSolidFocusedColor != null || mStrokeFocusedColor != null) { + drawable.addState(new int[]{android.R.attr.state_focused}, createShapeDrawable( + mSolidFocusedColor != null ? mSolidFocusedColor : mSolidColor, + mStrokeFocusedColor != null ? mStrokeFocusedColor : mStrokeColor)); + } + if (mSolidSelectedColor != null || mStrokeSelectedColor != null) { + drawable.addState(new int[]{android.R.attr.state_selected}, createShapeDrawable( + mSolidSelectedColor != null ? mSolidSelectedColor : mSolidColor, + mStrokeSelectedColor != null ? mStrokeSelectedColor : mStrokeColor)); + } + + drawable.addState(new int[]{}, defaultDrawable); + return drawable; + } + + public ShapeDrawable createShapeDrawable(int solidColor, int strokeColor) { + + ShapeDrawable drawable = new ShapeDrawable(); + + drawable.setShape(mShape) + .setSize(mShapeWidth, mShapeHeight) + .setRadius(mTopLeftRadius, mTopRightRadius, mBottomLeftRadius, mBottomRightRadius) + .setSolidColor(solidColor) + .setUseLevel(isUseLevel()) + .setStroke(mStrokeWidth, strokeColor, mDashWidth, mDashGap); + + drawable.setGradientAngle(mAngle) + .setGradientType(mGradientType) + .setGradientRadius(mGradientRadius) + .setGradientCenter(mCenterX, mCenterY); + + drawable.setInnerRadiusRatio(mInnerRadiusRatio) + .setInnerRadius(mInnerRadius) + .setThicknessRatio(mThicknessRatio) + .setThickness(mThickness); + + drawable.setShadowSize(mShadowSize) + .setShadowColor(mShadowColor) + .setShadowOffsetX(mShadowOffsetX) + .setShadowOffsetY(mShadowOffsetY); + + return drawable; + } + + public void intoBackground() { + Drawable drawable = buildBackgroundDrawable(); + if (drawable == null) { + return; + } + if (isDashLineEnable() || isShadowEnable()) { + // 需要关闭硬件加速,否则虚线或者阴影在某些手机上面无法生效 + mView.setLayerType(View.LAYER_TYPE_SOFTWARE, null); + } + mView.setBackground(drawable); + } +} \ No newline at end of file diff --git a/library/src/main/java/com/hjq/shape/builder/TextColorBuilder.java b/library/src/main/java/com/hjq/shape/builder/TextColorBuilder.java new file mode 100644 index 0000000..40f663e --- /dev/null +++ b/library/src/main/java/com/hjq/shape/builder/TextColorBuilder.java @@ -0,0 +1,216 @@ +package com.hjq.shape.builder; + +import android.content.res.ColorStateList; +import android.content.res.TypedArray; +import android.text.SpannableStringBuilder; +import android.widget.TextView; + +import com.hjq.shape.other.LinearGradientFontSpan; +import com.hjq.shape.styleable.ITextColorStyleable; + +/** + * author : Android 轮子哥 + * github : https://github.com/getActivity/ShapeView + * time : 2021/08/28 + * desc : TextColor 构建类 + */ +public final class TextColorBuilder { + + private final TextView mTextView; + + private int mTextColor; + private Integer mTextPressedColor; + private Integer mTextCheckedColor; + private Integer mTextDisabledColor; + private Integer mTextFocusedColor; + private Integer mTextSelectedColor; + + private int[] mTextGradientColor; + private int mTextGradientOrientation; + + public TextColorBuilder(TextView textView, TypedArray typedArray, ITextColorStyleable styleable) { + mTextView = textView; + mTextColor = typedArray.getColor(styleable.getTextColorStyleable(), textView.getTextColors().getDefaultColor()); + if (typedArray.hasValue(styleable.getTextPressedColorStyleable())) { + mTextPressedColor = typedArray.getColor(styleable.getTextPressedColorStyleable(), mTextColor); + } + if (styleable.getTextCheckedColorStyleable() > 0 && typedArray.hasValue(styleable.getTextCheckedColorStyleable())) { + mTextCheckedColor = typedArray.getColor(styleable.getTextCheckedColorStyleable(), mTextColor); + } + if (typedArray.hasValue(styleable.getTextDisabledColorStyleable())) { + mTextDisabledColor = typedArray.getColor(styleable.getTextDisabledColorStyleable(), mTextColor); + } + if (typedArray.hasValue(styleable.getTextFocusedColorStyleable())) { + mTextFocusedColor = typedArray.getColor(styleable.getTextFocusedColorStyleable(), mTextColor); + } + if (typedArray.hasValue(styleable.getTextSelectedColorStyleable())) { + mTextSelectedColor = typedArray.getColor(styleable.getTextSelectedColorStyleable(), mTextColor); + } + + if (typedArray.hasValue(styleable.getTextStartColorStyleable()) && typedArray.hasValue(styleable.getTextEndColorStyleable())) { + if (typedArray.hasValue(styleable.getTextCenterColorStyleable())) { + mTextGradientColor = new int[] {typedArray.getColor(styleable.getTextStartColorStyleable(), mTextColor), + typedArray.getColor(styleable.getTextCenterColorStyleable(), mTextColor), + typedArray.getColor(styleable.getTextEndColorStyleable(), mTextColor)}; + } else { + mTextGradientColor = new int[] {typedArray.getColor(styleable.getTextStartColorStyleable(), mTextColor), + typedArray.getColor(styleable.getTextEndColorStyleable(), mTextColor)}; + } + } + + mTextGradientOrientation = typedArray.getColor(styleable.getTextGradientOrientationStyleable(), + LinearGradientFontSpan.GRADIENT_ORIENTATION_HORIZONTAL); + } + + public TextColorBuilder setTextColor(Integer color) { + mTextColor = color; + clearTextGradientColor(); + return this; + } + + public Integer getTextColor() { + return mTextColor; + } + + public TextColorBuilder setTextPressedColor(Integer color) { + mTextPressedColor = color; + return this; + } + + public Integer getTextPressedColor() { + return mTextPressedColor; + } + + public TextColorBuilder setTextCheckedColor(Integer color) { + mTextCheckedColor = color; + return this; + } + + public Integer getTextCheckedColor() { + return mTextCheckedColor; + } + + public TextColorBuilder setTextDisabledColor(Integer color) { + mTextDisabledColor = color; + return this; + } + + public Integer getTextDisabledColor() { + return mTextDisabledColor; + } + + public TextColorBuilder setTextFocusedColor(Integer color) { + mTextFocusedColor = color; + return this; + } + + public Integer getTextFocusedColor() { + return mTextFocusedColor; + } + + public TextColorBuilder setTextSelectedColor(Integer color) { + mTextSelectedColor = color; + return this; + } + + public Integer getTextSelectedColor() { + return mTextSelectedColor; + } + + public TextColorBuilder setTextGradientColor(int[] color) { + mTextGradientColor = color; + return this; + } + + public int[] getTextGradientColor() { + return mTextGradientColor; + } + + public TextColorBuilder setTextGradientOrientation(int orientation) { + mTextGradientOrientation = orientation; + return this; + } + + public Integer getTextGradientOrientation() { + return mTextGradientOrientation; + } + + public boolean isTextGradientColor() { + return mTextGradientColor != null && mTextGradientColor.length > 0; + } + + public void clearTextGradientColor() { + mTextGradientColor = null; + } + + public SpannableStringBuilder buildLinearGradientSpannable(CharSequence text) { + return LinearGradientFontSpan.buildLinearGradientSpannable(text, mTextGradientColor, null, mTextGradientOrientation); + } + + public ColorStateList buildColorState() { + if (mTextPressedColor == null && + mTextCheckedColor == null && + mTextDisabledColor == null && + mTextFocusedColor == null && + mTextSelectedColor == null) { + return ColorStateList.valueOf(mTextColor); + } + + int maxSize = 6; + int arraySize = 0; + int[][] statesTemp = new int[maxSize][]; + int[] colorsTemp = new int[maxSize]; + + if (mTextPressedColor != null) { + statesTemp[arraySize] = new int[]{android.R.attr.state_pressed}; + colorsTemp[arraySize] = mTextPressedColor; + arraySize++; + } + if (mTextCheckedColor != null) { + statesTemp[arraySize] = new int[]{android.R.attr.state_checked}; + colorsTemp[arraySize] = mTextCheckedColor; + arraySize++; + } + if (mTextDisabledColor != null) { + statesTemp[arraySize] = new int[]{-android.R.attr.state_enabled}; + colorsTemp[arraySize] = mTextDisabledColor; + arraySize++; + } + if (mTextFocusedColor != null) { + statesTemp[arraySize] = new int[]{android.R.attr.state_focused}; + colorsTemp[arraySize] = mTextFocusedColor; + arraySize++; + } + if (mTextSelectedColor != null) { + statesTemp[arraySize] = new int[]{android.R.attr.state_selected}; + colorsTemp[arraySize] = mTextSelectedColor; + arraySize++; + } + + statesTemp[arraySize] = new int[]{}; + colorsTemp[arraySize] = mTextColor; + arraySize++; + + int[][] states; + int[] colors; + if (arraySize == maxSize) { + states = statesTemp; + colors = colorsTemp; + } else { + states = new int[arraySize][]; + colors = new int[arraySize]; + // 对数组进行拷贝 + System.arraycopy(statesTemp, 0, states, 0, arraySize); + System.arraycopy(colorsTemp, 0, colors, 0, arraySize); + } + return new ColorStateList(states, colors); + } + + public void intoTextColor() { + if (isTextGradientColor()) { + mTextView.setText(buildLinearGradientSpannable(mTextView.getText())); + return; + } + mTextView.setTextColor(buildColorState()); + } +} \ No newline at end of file diff --git a/library/src/main/java/com/hjq/shape/core/IShapeDrawable.java b/library/src/main/java/com/hjq/shape/core/IShapeDrawable.java deleted file mode 100644 index 0ea5003..0000000 --- a/library/src/main/java/com/hjq/shape/core/IShapeDrawable.java +++ /dev/null @@ -1,309 +0,0 @@ -package com.hjq.shape.core; - -import android.graphics.Color; -import android.graphics.drawable.Drawable; -import android.graphics.drawable.StateListDrawable; -import android.view.View; - -/** - * author : Android 轮子哥 - * github : https://github.com/getActivity/ShapeView - * time : 2021/07/17 - * desc : ShapeDrawable 参数接口 - */ -public interface IShapeDrawable { - - int DEFAULT_SHAPE_TYPE = ShapeType.RECTANGLE; - int DEFAULT_SHAPE_WIDTH = -1; - int DEFAULT_SHAPE_HEIGHT = -1; - int DEFAULT_SHAPE_SOLID_COLOR = Color.TRANSPARENT; - int DEFAULT_SHAPE_RADIUS = 0; - boolean DEFAULT_SHAPE_USE_LEVEL = false; - int DEFAULT_SHAPE_GRADIENT_TYPE = ShapeGradientType.LINEAR_GRADIENT; - int DEFAULT_SHAPE_ANGLE = 0; - float DEFAULT_SHAPE_CENTER_X = 0.5f; - float DEFAULT_SHAPE_CENTER_Y = 0.5f; - int DEFAULT_SHAPE_STROKE_COLOR = Color.TRANSPARENT; - int DEFAULT_SHAPE_STROKE_WIDTH = 0; - int DEFAULT_SHAPE_DASH_WIDTH = 0; - int DEFAULT_SHAPE_DASH_GAP = 0; - - float DEFAULT_SHAPE_INNER_RADIUS_RATIO = 3.0f; - int DEFAULT_SHAPE_INNER_RADIUS = -1; - float DEFAULT_SHAPE_THICKNESS_RATIO = 9.0f; - int DEFAULT_SHAPE_THICKNESS = -1; - - int DEFAULT_SHAPE_SHADOW_SIZE = 0; - int DEFAULT_SHAPE_SHADOW_COLOR = 0x10000000; - int DEFAULT_SHAPE_SHADOW_OFFSET_X = 0; - int DEFAULT_SHAPE_SHADOW_OFFSET_Y = 0; - - V setShapeType(int type); - - int getShapeType(); - - V setShapeWidth(int width); - - int getShapeWidth(); - - V setShapeHeight(int height); - - int getShapeHeight(); - - V setSolidColor(int color); - - int getSolidColor(); - - V setSolidPressedColor(int color); - - int getSolidPressedColor(); - - default V setSolidCheckedColor(int color) { - return setSolidColor(color); - } - - default int getSolidCheckedColor() { - return getSolidColor(); - } - - V setSolidDisabledColor(int color); - - int getSolidDisabledColor(); - - V setSolidFocusedColor(int color); - - int getSolidFocusedColor(); - - V setSolidSelectedColor(int color); - - int getSolidSelectedColor(); - - default V setRadius(int radius) { - setTopLeftRadius(radius); - setTopRightRadius(radius); - setBottomLeftRadius(radius); - return setBottomRightRadius(radius); - } - - V setTopLeftRadius(int radius); - - int getTopLeftRadius(); - - V setTopRightRadius(int radius); - - int getTopRightRadius(); - - V setBottomLeftRadius(int radius); - - int getBottomLeftRadius(); - - V setBottomRightRadius(int radius); - - int getBottomRightRadius(); - - V setStartColor(int color); - - int getStartColor(); - - V setCenterColor(int color); - - int getCenterColor(); - - V setEndColor(int color); - - int getEndColor(); - - default boolean isGradientColor() { - return getSolidColor() != getStartColor() && - getSolidColor() != getEndColor(); - } - - default void clearGradientColor() { - setStartColor(getSolidColor()); - setCenterColor(getSolidColor()); - setEndColor(getSolidColor()); - } - - V setUseLevel(boolean useLevel); - - boolean isUseLevel(); - - V setAngle(int angle); - - int getAngle(); - - V setGradientType(int type); - - int getGradientType(); - - V setCenterX(float x); - - float getCenterX(); - - V setCenterY(float y); - - float getCenterY(); - - V setGradientRadius(int radius); - - int getGradientRadius(); - - V setStrokeColor(int color); - - int getStrokeColor(); - - V setStrokePressedColor(int color); - - int getStrokePressedColor(); - - default V setStrokeCheckedColor(int color) { - return setStrokeColor(color); - } - - default int getStrokeCheckedColor() { - return getStrokeColor(); - } - - V setStrokeDisabledColor(int color); - - int getStrokeDisabledColor(); - - V setStrokeFocusedColor(int color); - - int getStrokeFocusedColor(); - - V setStrokeSelectedColor(int color); - - int getStrokeSelectedColor(); - - V setStrokeWidth(int width); - - int getStrokeWidth(); - - V setDashWidth(int width); - - int getDashWidth(); - - V setDashGap(int gap); - - int getDashGap(); - - default boolean isDashLineEnable() { - return getDashGap() > 0; - } - - V setInnerRadius(int radius); - - int getInnerRadius(); - - V setInnerRadiusRatio(float ratio); - - float getInnerRadiusRatio(); - - V setThickness(int size); - - int getThickness(); - - V setThicknessRatio(float ratio); - - float getThicknessRatio(); - - default boolean isShadowEnable() { - return getShadowSize() > 0; - } - - V setShadowSize(int size); - - int getShadowSize(); - - V setShadowColor(int color); - - int getShadowColor(); - - V setShadowOffsetX(int offsetX); - - int getShadowOffsetX(); - - V setShadowOffsetY(int offsetY); - - int getShadowOffsetY(); - - default Drawable buildBackgroundDrawable() { - if (!isGradientColor() && getSolidColor() == DEFAULT_SHAPE_SOLID_COLOR && getStrokeColor() == DEFAULT_SHAPE_STROKE_COLOR) { - return null; - } - - 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 defaultDrawable; - } - - StateListDrawable drawable = new StateListDrawable(); - if (getSolidColor() != getSolidPressedColor() || getStrokeColor() != getStrokePressedColor()) { - drawable.addState(new int[]{android.R.attr.state_pressed}, createShapeDrawable(getSolidPressedColor(), getStrokePressedColor())); - } - if (getSolidColor() != getSolidCheckedColor() || getStrokeColor() != getStrokeCheckedColor()) { - drawable.addState(new int[]{android.R.attr.state_checked}, createShapeDrawable(getSolidCheckedColor(), getStrokeCheckedColor())); - } - if (getSolidColor() != getSolidDisabledColor() || getStrokeColor() != getStrokeDisabledColor()) { - drawable.addState(new int[]{-android.R.attr.state_enabled}, createShapeDrawable(getSolidDisabledColor(), getStrokeDisabledColor())); - } - if (getSolidColor() != getSolidFocusedColor() || getStrokeColor() != getStrokeFocusedColor()) { - drawable.addState(new int[]{android.R.attr.state_focused}, createShapeDrawable(getSolidFocusedColor(), getStrokeFocusedColor())); - } - if (getSolidColor() != getSolidSelectedColor() || getStrokeColor() != getStrokeSelectedColor()) { - drawable.addState(new int[]{android.R.attr.state_selected}, createShapeDrawable(getSolidSelectedColor(), getStrokeSelectedColor())); - } - - drawable.addState(new int[]{}, defaultDrawable); - return drawable; - } - - 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()}); - - drawable.setColor(solidColor); - drawable.setGradientCenter(getCenterX(), getCenterY()); - drawable.setUseLevel(isUseLevel()); - drawable.setGradientAngle(getAngle()); - - drawable.setGradientType(getGradientType()); - drawable.setGradientRadius(getGradientRadius()); - drawable.setStroke(getStrokeWidth(), strokeColor, getDashWidth(), getDashGap()); - - if (getShapeType() == ShapeType.RING) { - drawable.setInnerRadiusRatio(getInnerRadiusRatio()); - drawable.setInnerRadius(getInnerRadius()); - drawable.setThicknessRatio(getThicknessRatio()); - drawable.setThickness(getThickness()); - } - - int shadowSize = getShadowSize(); - if (isShadowEnable()) { - drawable.setShadowSize(shadowSize); - drawable.setShadowColor(getShadowColor()); - drawable.setShadowOffsetX(getShadowOffsetX()); - drawable.setShadowOffsetY(getShadowOffsetY()); - } - return drawable; - } - - void intoBackground(); -} \ No newline at end of file diff --git a/library/src/main/java/com/hjq/shape/core/IShapeTextColor.java b/library/src/main/java/com/hjq/shape/core/IShapeTextColor.java deleted file mode 100644 index d19e14a..0000000 --- a/library/src/main/java/com/hjq/shape/core/IShapeTextColor.java +++ /dev/null @@ -1,107 +0,0 @@ -package com.hjq.shape.core; - -import android.content.res.ColorStateList; -import android.text.SpannableStringBuilder; -import android.text.Spanned; -import android.view.View; -import android.widget.LinearLayout; - -/** - * author : Android 轮子哥 - * github : https://github.com/getActivity/ShapeView - * time : 2021/07/31 - * desc : ShapeTextColor 参数接口 - */ -public interface IShapeTextColor { - - int SHAPE_TEXT_GRADIENT_TYPE_HORIZONTAL = LinearLayout.HORIZONTAL; - int SHAPE_TEXT_GRADIENT_TYPE_VERTICAL = LinearLayout.VERTICAL; - - V setNormalTextColor(int color); - - int getNormalTextColor(); - - V setTextPressedColor(int color); - - int getTextPressedColor(); - - default V setTextCheckedColor(int color) { - return setNormalTextColor(color); - } - - default int getTextCheckedColor() { - return getNormalTextColor(); - } - - V setTextDisabledColor(int color); - - int getTextDisabledColor(); - - V setTextFocusedColor(int color); - - int getTextFocusedColor(); - - V setTextSelectedColor(int color); - - int getTextSelectedColor(); - - V setTextStartColor(int color); - - int getTextStartColor(); - - V setTextCenterColor(int color); - - int getTextCenterColor(); - - V setTextEndColor(int color); - - int getTextEndColor(); - - V setTextGradientOrientation(int orientation); - - int getTextGradientOrientation(); - - default boolean isTextGradientColor() { - return getNormalTextColor() != getTextStartColor() && - getNormalTextColor() != getTextEndColor(); - } - - default void clearTextGradientColor() { - setTextStartColor(getNormalTextColor()); - setTextCenterColor(getNormalTextColor()); - setTextEndColor(getNormalTextColor()); - } - - default SpannableStringBuilder buildLinearGradientSpannable(CharSequence text) { - final int[] colors; - final float[] positions; - if (getNormalTextColor() != getTextCenterColor()) { - colors = new int[]{getTextStartColor(), getTextCenterColor(), getTextEndColor()}; - positions = new float[] {0f, 0.5f, 1f}; - } else { - colors = new int[]{getTextStartColor(), getTextEndColor()}; - positions = new float[] {0f, 1f}; - } - return buildLinearGradientSpannable(text, colors, positions); - } - - default SpannableStringBuilder buildLinearGradientSpannable(CharSequence text, int[] colors, float[] positions) { - SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder(text); - LinearGradientFontSpan linearGradientFontSpan = new LinearGradientFontSpan(); - linearGradientFontSpan.setTextGradientColor(colors); - linearGradientFontSpan.setTextGradientOrientation(getTextGradientOrientation()); - linearGradientFontSpan.setTextGradientPositions(positions); - spannableStringBuilder.setSpan(linearGradientFontSpan, 0, spannableStringBuilder.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); - return spannableStringBuilder; - } - - default ColorStateList buildColorState() { - return new ColorStateList(new int[][]{new int[]{ - android.R.attr.state_pressed}, new int[]{android.R.attr.state_checked}, new int[]{-android.R.attr.state_enabled}, - new int[]{android.R.attr.state_focused}, new int[]{android.R.attr.state_selected}, new int[]{}}, - new int[]{getTextPressedColor(), getTextCheckedColor(), getTextDisabledColor(), - getTextFocusedColor(), getTextSelectedColor(), getNormalTextColor()}); - } - - void intoTextColor(); -} \ No newline at end of file diff --git a/library/src/main/java/com/hjq/shape/core/ShapeDrawable.java b/library/src/main/java/com/hjq/shape/drawable/ShapeDrawable.java similarity index 83% rename from library/src/main/java/com/hjq/shape/core/ShapeDrawable.java rename to library/src/main/java/com/hjq/shape/drawable/ShapeDrawable.java index 85b94fd..f273722 100644 --- a/library/src/main/java/com/hjq/shape/core/ShapeDrawable.java +++ b/library/src/main/java/com/hjq/shape/drawable/ShapeDrawable.java @@ -1,4 +1,4 @@ -package com.hjq.shape.core; +package com.hjq.shape.drawable; import android.annotation.SuppressLint; import android.graphics.Canvas; @@ -16,6 +16,7 @@ import android.graphics.SweepGradient; import android.graphics.drawable.Drawable; import android.os.Build; +import android.view.View; /** * author : Android 轮子哥 @@ -44,20 +45,23 @@ public class ShapeDrawable extends Drawable { private boolean mPathIsDirty = true; public ShapeDrawable() { - this(new ShapeState(ShapeGradientOrientation.TOP_BOTTOM, null)); + this(new ShapeState()); } - public ShapeDrawable(ShapeGradientOrientation orientation, int[] colors) { - this(new ShapeState(orientation, colors)); - } - public ShapeDrawable(ShapeState state) { mShapeState = state; initializeWithState(state); mRectIsDirty = true; mMutated = false; } - + + /** + * 获取 Shape 状态对象 + */ + public ShapeState getShapeState() { + return mShapeState; + } + @Override public boolean getPadding(Rect padding) { if (mPadding != null) { @@ -68,119 +72,161 @@ public boolean getPadding(Rect padding) { } } - public void setPadding(Rect padding) { + public ShapeDrawable setPadding(Rect padding) { mPadding = padding; + return this; } /** - * 为 4 个角中的每一个指定半径。 对于每个角,该数组包含 2 个值, [X_radius, Y_radius] 。 角的顺序是左上角、右上角、右下角、左下角。 仅当形状为RECTANGLE类型时才使用此属性 - * 注意:更改此属性将影响从资源加载的可绘制对象的所有实例。 建议在更改此属性之前调用mutate() + * 设置 Shape 形状 * - * @param radii 每个角的 4 对 X 和 Y 半径,以像素为单位。 此数组的长度必须 >= 8 + * @param shape Shape 形状类型 */ - public void setCornerRadii(float[] radii) { - mShapeState.setCornerRadii(radii); + public ShapeDrawable setShape(int shape) { + mRingPath = null; mPathIsDirty = true; + mShapeState.setShape(shape); invalidateSelf(); + return this; } - + + /** + * 设置 Shape 大小 + * + * @param width Shape 宽度 + * @param height Shape 高度 + */ + public ShapeDrawable setSize(int width, int height) { + mShapeState.setSize(width, height); + mPathIsDirty = true; + invalidateSelf(); + return this; + } + /** * 指定渐变角的半径。 如果这 > 0,则可绘制对象将绘制在圆角矩形中,而不是矩形中。 仅当形状为RECTANGLE类型时才使用此属性。 * 注意:更改此属性将影响从资源加载的可绘制对象的所有实例。 建议在更改此属性之前调用mutate() 。 * * @param radius 矩形角的半径(以像素为单位) */ - public void setCornerRadius(float radius) { + public ShapeDrawable setRadius(float radius) { mShapeState.setCornerRadius(radius); mPathIsDirty = true; invalidateSelf(); + return this; + } + + public ShapeDrawable setRadius(float topLeftRadius, float topRightRadius, float bottomLeftRadius, float bottomRightRadius) { + if (topLeftRadius == topRightRadius && topLeftRadius == bottomLeftRadius && topLeftRadius == bottomRightRadius) { + return setRadius(topLeftRadius); + } + // 为 4 个角中的每一个指定半径,对于每个角,该数组包含 2 个值 [X_radius, Y_radius],角的顺序是左上角、右上角、右下角、左下角 + mShapeState.setCornerRadii(new float[] { + topLeftRadius, topLeftRadius, topRightRadius, topRightRadius, + bottomRightRadius, bottomRightRadius, bottomLeftRadius, bottomLeftRadius}); + mPathIsDirty = true; + invalidateSelf(); + return this; } /** - * 设置边框属性 - * - * @param width 边框宽度 - * @param color 边框颜色 + * 设置填充颜色 */ - public void setStroke(int width, int color) { - setStroke(width, color, 0, 0); + public ShapeDrawable setSolidColor(int color) { + mShapeState.setSolidColor(color); + mFillPaint.setColor(color); + invalidateSelf(); + return this; } /** - * 设置边框属性 - * - * @param width 边框宽度 - * @param color 边框宽度 - * @param dashWidth 边框虚线宽度(为 0 就是实线,大于 0 就是虚线) - * @param dashGap 边框虚线间隔(虚线与虚线之间的间隔) + * 设置边框颜色 + */ + public ShapeDrawable setStrokeColor(int color) { + setStroke(color, mShapeState.mStrokeWidth, mShapeState.mStrokeDashWidth, mShapeState.mStrokeDashGap); + return this; + } + + /** + * 设置边框宽度 + */ + public ShapeDrawable setStrokeWidth(int width) { + setStroke(mShapeState.mStrokeColor, width, mShapeState.mStrokeDashWidth, mShapeState.mStrokeDashGap); + return this; + } + + /** + * 设置边框虚线宽度(为 0 就是实线,大于 0 就是虚线) */ - public void setStroke(int width, int color, float dashWidth, float dashGap) { + public ShapeDrawable setDashWidth(float dashWidth) { + setStroke(mShapeState.mStrokeColor, mShapeState.mStrokeWidth, dashWidth, mShapeState.mStrokeDashGap); + return this; + } + + /** + * 设置虚线间隔(虚线与虚线之间的间隔) + */ + public ShapeDrawable setDashGap(float dashGap) { + setStroke(mShapeState.mStrokeColor, mShapeState.mStrokeWidth, mShapeState.mStrokeDashWidth, dashGap); + return this; + } + + /** + * 初始化边框属性 + */ + public ShapeDrawable setStroke(int width, int color, float dashWidth, float dashGap) { mShapeState.setStroke(width, color, dashWidth, dashGap); - if (mStrokePaint == null) { + if (mStrokePaint == null) { mStrokePaint = new Paint(Paint.ANTI_ALIAS_FLAG); mStrokePaint.setStyle(Paint.Style.STROKE); } mStrokePaint.setStrokeWidth(width); mStrokePaint.setColor(color); - - DashPathEffect e = null; - if (dashWidth > 0) { - e = new DashPathEffect(new float[] { dashWidth, dashGap }, 0); - } - mStrokePaint.setPathEffect(e); - invalidateSelf(); - } - /** - * 设置 Shape 大小 - * - * @param width Shape 宽度 - * @param height Shape 高度 - */ - public void setSize(int width, int height) { - mShapeState.setSize(width, height); - mPathIsDirty = true; + mStrokePaint.setPathEffect(dashWidth > 0 ? new DashPathEffect(new float[] {dashWidth, dashGap}, 0) : null); invalidateSelf(); + return this; } /** - * 设置 Shape 形状 - * - * @param shape Shape 形状类型 + * 设置渐变颜色 */ - public void setShape(int shape) { - mRingPath = null; - mPathIsDirty = true; - mShapeState.setShapeType(shape); + public ShapeDrawable setGradientColors(int[] colors) { + mShapeState.setGradientColor(colors); + mRectIsDirty = true; invalidateSelf(); + return this; } /** * 设置 Shape 渐变类型 */ - public void setGradientType(int type) { + public ShapeDrawable setGradientType(int type) { mShapeState.setGradientType(type); mRectIsDirty = true; invalidateSelf(); + return this; } /** * 设置渐变中心位置 */ - public void setGradientCenter(float x, float y) { + public ShapeDrawable setGradientCenter(float x, float y) { mShapeState.setGradientCenter(x, y); mRectIsDirty = true; invalidateSelf(); + return this; } /** * 设置渐变半径大小 */ - public void setGradientRadius(float radius) { + public ShapeDrawable setGradientRadius(float radius) { mShapeState.setGradientRadius(radius); mRectIsDirty = true; invalidateSelf(); + return this; } /** @@ -196,21 +242,17 @@ public void setGradientRadius(float radius) { * @see #setLevel(int) * @see #getLevel() */ - public void setUseLevel(boolean useLevel) { + public ShapeDrawable setUseLevel(boolean useLevel) { mShapeState.mUseLevel = useLevel; mRectIsDirty = true; invalidateSelf(); + return this; } - private int modulateAlpha(int alpha) { - int scale = mAlpha + (mAlpha >> 7); - return alpha * scale >> 8; - } - /** * 设置渐变角度 */ - public void setGradientAngle(int angle) { + public ShapeDrawable setGradientAngle(int angle) { angle %= 360; // angle 必须为 45 的整数倍 if (angle % 45 == 0) { @@ -243,112 +285,108 @@ public void setGradientAngle(int angle) { break; } } - } - - /** - * 获取渐变方向 - */ - public ShapeGradientOrientation getGradientOrientation() { - return mShapeState.mOrientation; + return this; } /** * 设置渐变方向 */ - public void setGradientOrientation(ShapeGradientOrientation orientation) { - mShapeState.mOrientation = orientation; - mRectIsDirty = true; - invalidateSelf(); - } - - /** - * 设置填充颜色 - */ - public void setColor(int argb) { - mShapeState.setSolidColor(argb); - mFillPaint.setColor(argb); - invalidateSelf(); - } - - /** - * 设置渐变颜色 - */ - public void setColors(int[] colors) { - mShapeState.setGradientColor(colors); + public ShapeDrawable setGradientOrientation(ShapeGradientOrientation orientation) { + mShapeState.mGradientOrientation = orientation; mRectIsDirty = true; invalidateSelf(); + return this; } /** * 设置内环的半径 */ - public void setInnerRadius(int radius) { + public ShapeDrawable setInnerRadius(int radius) { mShapeState.mInnerRadius = radius; mRectIsDirty = true; invalidateSelf(); + return this; } /** * 设置内环的半径比率 */ - public void setInnerRadiusRatio(float radiusRatio) { + public ShapeDrawable setInnerRadiusRatio(float radiusRatio) { mShapeState.mInnerRadiusRatio = radiusRatio; mRectIsDirty = true; invalidateSelf(); + return this; } /** * 设置外环的厚度 */ - public void setThickness(int size) { + public ShapeDrawable setThickness(int size) { mShapeState.mThickness = size; mRectIsDirty = true; invalidateSelf(); + return this; } /** * 设置外环的厚度比率 */ - public void setThicknessRatio(float radiusRatio) { + public ShapeDrawable setThicknessRatio(float radiusRatio) { mShapeState.mThicknessRatio = radiusRatio; mRectIsDirty = true; invalidateSelf(); + return this; } /** * 设置阴影颜色 */ - public void setShadowColor(int color) { + public ShapeDrawable setShadowColor(int color) { mShapeState.setShadowColor(color); mPathIsDirty = true; invalidateSelf(); + return this; } /** * 设置阴影大小 */ - public void setShadowSize(int size) { + public ShapeDrawable setShadowSize(int size) { mShapeState.setShadowSize(size); mPathIsDirty = true; invalidateSelf(); + return this; } /** * 设置阴影水平偏移 */ - public void setShadowOffsetX(int offsetX) { + public ShapeDrawable setShadowOffsetX(int offsetX) { mShapeState.setShadowOffsetX(offsetX); mPathIsDirty = true; invalidateSelf(); + return this; } /** * 阴影垂直偏移 */ - public void setShadowOffsetY(int offsetY) { + public ShapeDrawable setShadowOffsetY(int offsetY) { mShapeState.setShadowOffsetY(offsetY); mPathIsDirty = true; invalidateSelf(); + return this; + } + + /** + * 将当前 Drawable 应用到 View 背景 + */ + public void intoBackground(View view) { + if (mShapeState.mStrokeDashGap > 0 || mShapeState.mShadowSize > 0) { + // 需要关闭硬件加速,否则虚线或者阴影在某些手机上面无法生效 + view.setLayerType(View.LAYER_TYPE_SOFTWARE, null); + } + view.setBackground(this); } @SuppressLint("WrongConstant") @@ -508,6 +546,11 @@ of the fill (if any) without worrying about blending artifacts. } } } + + private int modulateAlpha(int alpha) { + int scale = mAlpha + (mAlpha >> 7); + return alpha * scale >> 8; + } @Override public int getChangingConfigurations() { @@ -646,9 +689,9 @@ private boolean ensureValidRect() { RectF r = mRect; float x0, x1, y0, y1; - if (st.mGradient == ShapeGradientType.LINEAR_GRADIENT) { + if (st.mGradientType == ShapeGradientType.LINEAR_GRADIENT) { final float level = st.mUseLevel ? (float) getLevel() / 10000.0f : 1.0f; - switch (st.mOrientation) { + switch (st.mGradientOrientation) { case TOP_BOTTOM: x0 = r.left; y0 = r.top; x1 = x0; y1 = level * r.bottom; @@ -685,7 +728,7 @@ private boolean ensureValidRect() { mFillPaint.setShader(new LinearGradient(x0, y0, x1, y1, colors, st.mPositions, Shader.TileMode.CLAMP)); - } else if (st.mGradient == ShapeGradientType.RADIAL_GRADIENT) { + } else if (st.mGradientType == ShapeGradientType.RADIAL_GRADIENT) { x0 = r.left + (r.right - r.left) * st.mCenterX; y0 = r.top + (r.bottom - r.top) * st.mCenterY; @@ -694,7 +737,7 @@ private boolean ensureValidRect() { mFillPaint.setShader(new RadialGradient(x0, y0, level * st.mGradientRadius, colors, null, Shader.TileMode.CLAMP)); - } else if (st.mGradient == ShapeGradientType.SWEEP_GRADIENT) { + } else if (st.mGradientType == ShapeGradientType.SWEEP_GRADIENT) { x0 = r.left + (r.right - r.left) * st.mCenterX; y0 = r.top + (r.bottom - r.top) * st.mCenterY; @@ -776,16 +819,7 @@ private void initializeWithState(ShapeState state) { } mPadding = state.mPadding; if (state.mStrokeWidth >= 0) { - mStrokePaint = new Paint(Paint.ANTI_ALIAS_FLAG); - mStrokePaint.setStyle(Paint.Style.STROKE); - mStrokePaint.setStrokeWidth(state.mStrokeWidth); - mStrokePaint.setColor(state.mStrokeColor); - - if (state.mStrokeDashWidth != 0.0f) { - DashPathEffect e = new DashPathEffect( - new float[] { state.mStrokeDashWidth, state.mStrokeDashGap }, 0); - mStrokePaint.setPathEffect(e); - } + setStroke(state.mStrokeWidth, state.mStrokeColor, state.mStrokeDashWidth, state.mStrokeDashGap); } } } \ No newline at end of file diff --git a/library/src/main/java/com/hjq/shape/core/ShapeGradientOrientation.java b/library/src/main/java/com/hjq/shape/drawable/ShapeGradientOrientation.java similarity index 95% rename from library/src/main/java/com/hjq/shape/core/ShapeGradientOrientation.java rename to library/src/main/java/com/hjq/shape/drawable/ShapeGradientOrientation.java index 93fdbd2..483a8e7 100644 --- a/library/src/main/java/com/hjq/shape/core/ShapeGradientOrientation.java +++ b/library/src/main/java/com/hjq/shape/drawable/ShapeGradientOrientation.java @@ -1,4 +1,4 @@ -package com.hjq.shape.core; +package com.hjq.shape.drawable; /** * author : Android 轮子哥 diff --git a/library/src/main/java/com/hjq/shape/core/ShapeGradientType.java b/library/src/main/java/com/hjq/shape/drawable/ShapeGradientType.java similarity index 92% rename from library/src/main/java/com/hjq/shape/core/ShapeGradientType.java rename to library/src/main/java/com/hjq/shape/drawable/ShapeGradientType.java index c4d5ee8..f800f89 100644 --- a/library/src/main/java/com/hjq/shape/core/ShapeGradientType.java +++ b/library/src/main/java/com/hjq/shape/drawable/ShapeGradientType.java @@ -1,4 +1,4 @@ -package com.hjq.shape.core; +package com.hjq.shape.drawable; /** * author : Android 轮子哥 diff --git a/library/src/main/java/com/hjq/shape/core/ShapeState.java b/library/src/main/java/com/hjq/shape/drawable/ShapeState.java similarity index 91% rename from library/src/main/java/com/hjq/shape/core/ShapeState.java rename to library/src/main/java/com/hjq/shape/drawable/ShapeState.java index d248905..2447cc3 100644 --- a/library/src/main/java/com/hjq/shape/core/ShapeState.java +++ b/library/src/main/java/com/hjq/shape/drawable/ShapeState.java @@ -1,4 +1,4 @@ -package com.hjq.shape.core; +package com.hjq.shape.drawable; import android.content.res.Resources; import android.graphics.Rect; @@ -14,8 +14,8 @@ public class ShapeState extends Drawable.ConstantState { public int mChangingConfigurations; public int mShapeType = ShapeType.RECTANGLE; - public int mGradient = ShapeGradientType.LINEAR_GRADIENT; - public ShapeGradientOrientation mOrientation; + public int mGradientType = ShapeGradientType.LINEAR_GRADIENT; + public ShapeGradientOrientation mGradientOrientation = ShapeGradientOrientation.TOP_BOTTOM; public int[] mGradientColors; public int[] mTempColors; // no need to copy public float[] mTempPositions; // no need to copy @@ -47,16 +47,13 @@ public class ShapeState extends Drawable.ConstantState { public int mShadowOffsetX; public int mShadowOffsetY; - public ShapeState(ShapeGradientOrientation orientation, int[] colors) { - mOrientation = orientation; - setGradientColor(colors); - } + public ShapeState() {} public ShapeState(ShapeState state) { mChangingConfigurations = state.mChangingConfigurations; mShapeType = state.mShapeType; - mGradient = state.mGradient; - mOrientation = state.mOrientation; + mGradientType = state.mGradientType; + mGradientOrientation = state.mGradientOrientation; if (state.mGradientColors != null) { mGradientColors = state.mGradientColors.clone(); } @@ -110,13 +107,13 @@ public int getChangingConfigurations() { return mChangingConfigurations; } - public void setShapeType(int type) { - mShapeType = type; + public void setShape(int shape) { + mShapeType = shape; computeOpacity(); } - public void setGradientType(int gradient) { - mGradient = gradient; + public void setGradientType(int gradientType) { + mGradientType = gradientType; } public void setGradientCenter(float x, float y) { diff --git a/library/src/main/java/com/hjq/shape/core/ShapeType.java b/library/src/main/java/com/hjq/shape/drawable/ShapeType.java similarity index 93% rename from library/src/main/java/com/hjq/shape/core/ShapeType.java rename to library/src/main/java/com/hjq/shape/drawable/ShapeType.java index 9ef6409..71ba60e 100644 --- a/library/src/main/java/com/hjq/shape/core/ShapeType.java +++ b/library/src/main/java/com/hjq/shape/drawable/ShapeType.java @@ -1,4 +1,4 @@ -package com.hjq.shape.core; +package com.hjq.shape.drawable; /** * author : Android 轮子哥 diff --git a/library/src/main/java/com/hjq/shape/layout/ShapeConstraintLayout.java b/library/src/main/java/com/hjq/shape/layout/ShapeConstraintLayout.java index bb3eeb9..c1e81f6 100644 --- a/library/src/main/java/com/hjq/shape/layout/ShapeConstraintLayout.java +++ b/library/src/main/java/com/hjq/shape/layout/ShapeConstraintLayout.java @@ -2,13 +2,12 @@ import android.content.Context; import android.content.res.TypedArray; -import android.graphics.drawable.Drawable; import android.support.constraint.ConstraintLayout; import android.util.AttributeSet; -import android.view.View; import com.hjq.shape.R; -import com.hjq.shape.core.IShapeDrawable; +import com.hjq.shape.builder.ShapeDrawableBuilder; +import com.hjq.shape.styleable.ShapeConstraintLayoutStyleable; /** * author : Android 轮子哥 @@ -16,52 +15,11 @@ * time : 2021/07/17 * desc : 支持直接定义 Shape 背景的 ConstraintLayout */ -public class ShapeConstraintLayout extends ConstraintLayout implements IShapeDrawable { +public class ShapeConstraintLayout extends ConstraintLayout { - private int mShapeType; - private int mShapeWidth; - private int mShapeHeight; + private static final ShapeConstraintLayoutStyleable STYLEABLE = new ShapeConstraintLayoutStyleable(); - private int mSolidColor; - private int mSolidPressedColor; - private int mSolidDisabledColor; - private int mSolidFocusedColor; - private int mSolidSelectedColor; - - private int mTopLeftRadius; - private int mTopRightRadius; - private int mBottomLeftRadius; - private int mBottomRightRadius; - - private int mStartColor; - private int mCenterColor; - private int mEndColor; - private boolean mUseLevel; - private int mAngle; - private int mGradientType; - private float mCenterX; - private float mCenterY; - private int mGradientRadius; - - private int mStrokeColor; - private int mStrokePressedColor; - private int mStrokeDisabledColor; - private int mStrokeFocusedColor; - private int mStrokeSelectedColor; - - private int mStrokeWidth; - private int mDashWidth; - private int mDashGap; - - private int mInnerRadius; - private float mInnerRadiusRatio; - private int mThickness; - private float mThicknessRatio; - - private int mShadowSize; - private int mShadowColor; - private int mShadowOffsetX; - private int mShadowOffsetY; + private final ShapeDrawableBuilder mShapeDrawableBuilder; public ShapeConstraintLayout(Context context) { this(context, null); @@ -75,505 +33,13 @@ public ShapeConstraintLayout(Context context, AttributeSet attrs, int defStyleAt super(context, attrs, defStyleAttr); TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.ShapeConstraintLayout); - mShapeType = typedArray.getInt(R.styleable.ShapeConstraintLayout_shape, DEFAULT_SHAPE_TYPE); - mShapeWidth = typedArray.getDimensionPixelSize(R.styleable.ShapeConstraintLayout_shape_width, DEFAULT_SHAPE_WIDTH); - mShapeHeight = typedArray.getDimensionPixelSize(R.styleable.ShapeConstraintLayout_shape_height, DEFAULT_SHAPE_HEIGHT); - - mSolidColor = typedArray.getColor(R.styleable.ShapeConstraintLayout_shape_solidColor, DEFAULT_SHAPE_SOLID_COLOR); - mSolidPressedColor = typedArray.getColor(R.styleable.ShapeConstraintLayout_shape_solidPressedColor, mSolidColor); - mSolidDisabledColor = typedArray.getColor(R.styleable.ShapeConstraintLayout_shape_solidDisabledColor, mSolidColor); - mSolidFocusedColor = typedArray.getColor(R.styleable.ShapeConstraintLayout_shape_solidFocusedColor, mSolidColor); - mSolidSelectedColor = typedArray.getColor(R.styleable.ShapeConstraintLayout_shape_solidSelectedColor, mSolidColor); - - int radius = typedArray.getDimensionPixelSize(R.styleable.ShapeConstraintLayout_shape_radius, DEFAULT_SHAPE_RADIUS); - mTopLeftRadius = typedArray.getDimensionPixelSize(R.styleable.ShapeConstraintLayout_shape_topLeftRadius, radius); - mTopRightRadius = typedArray.getDimensionPixelSize(R.styleable.ShapeConstraintLayout_shape_topRightRadius, radius); - mBottomLeftRadius = typedArray.getDimensionPixelSize(R.styleable.ShapeConstraintLayout_shape_bottomLeftRadius, radius); - mBottomRightRadius = typedArray.getDimensionPixelSize(R.styleable.ShapeConstraintLayout_shape_bottomRightRadius, radius); - - mStartColor = typedArray.getColor(R.styleable.ShapeConstraintLayout_shape_startColor, mSolidColor); - mCenterColor = typedArray.getColor(R.styleable.ShapeConstraintLayout_shape_centerColor, mSolidColor); - mEndColor = typedArray.getColor(R.styleable.ShapeConstraintLayout_shape_endColor, mSolidColor); - mUseLevel = typedArray.getBoolean(R.styleable.ShapeConstraintLayout_shape_useLevel, DEFAULT_SHAPE_USE_LEVEL); - mAngle = (int) typedArray.getFloat(R.styleable.ShapeConstraintLayout_shape_angle, DEFAULT_SHAPE_ANGLE); - mGradientType = typedArray.getInt(R.styleable.ShapeConstraintLayout_shape_gradientType, DEFAULT_SHAPE_GRADIENT_TYPE); - mCenterX = typedArray.getFloat(R.styleable.ShapeConstraintLayout_shape_centerX, DEFAULT_SHAPE_CENTER_X); - mCenterY = typedArray.getFloat(R.styleable.ShapeConstraintLayout_shape_centerY, DEFAULT_SHAPE_CENTER_Y); - mGradientRadius = typedArray.getDimensionPixelSize(R.styleable.ShapeConstraintLayout_shape_gradientRadius, radius); - - mStrokeColor = typedArray.getColor(R.styleable.ShapeConstraintLayout_shape_strokeColor, DEFAULT_SHAPE_STROKE_COLOR); - mStrokePressedColor = typedArray.getColor(R.styleable.ShapeConstraintLayout_shape_strokePressedColor, mStrokeColor); - mStrokeDisabledColor = typedArray.getColor(R.styleable.ShapeConstraintLayout_shape_strokeDisabledColor, mStrokeColor); - mStrokeFocusedColor = typedArray.getColor(R.styleable.ShapeConstraintLayout_shape_strokeFocusedColor, mStrokeColor); - mStrokeSelectedColor = typedArray.getColor(R.styleable.ShapeConstraintLayout_shape_strokeSelectedColor, mStrokeColor); - - mStrokeWidth = typedArray.getDimensionPixelSize(R.styleable.ShapeConstraintLayout_shape_strokeWidth, DEFAULT_SHAPE_STROKE_WIDTH); - mDashWidth = typedArray.getDimensionPixelSize(R.styleable.ShapeConstraintLayout_shape_dashWidth, DEFAULT_SHAPE_DASH_WIDTH); - mDashGap = typedArray.getDimensionPixelSize(R.styleable.ShapeConstraintLayout_shape_dashGap, DEFAULT_SHAPE_DASH_GAP); - - mInnerRadius = typedArray.getDimensionPixelOffset(R.styleable.ShapeConstraintLayout_shape_innerRadius, DEFAULT_SHAPE_INNER_RADIUS); - mInnerRadiusRatio = typedArray.getFloat(R.styleable.ShapeConstraintLayout_shape_innerRadiusRatio, DEFAULT_SHAPE_INNER_RADIUS_RATIO); - mThickness = typedArray.getDimensionPixelOffset(R.styleable.ShapeConstraintLayout_shape_thickness, DEFAULT_SHAPE_THICKNESS); - mThicknessRatio = typedArray.getFloat(R.styleable.ShapeConstraintLayout_shape_thicknessRatio, DEFAULT_SHAPE_THICKNESS_RATIO); - - mShadowSize = typedArray.getDimensionPixelSize(R.styleable.ShapeConstraintLayout_shape_shadowSize, DEFAULT_SHAPE_SHADOW_SIZE); - mShadowColor = typedArray.getColor(R.styleable.ShapeConstraintLayout_shape_shadowColor, DEFAULT_SHAPE_SHADOW_COLOR); - mShadowOffsetX = typedArray.getDimensionPixelOffset(R.styleable.ShapeConstraintLayout_shape_shadowOffsetX, DEFAULT_SHAPE_SHADOW_OFFSET_X); - mShadowOffsetY = typedArray.getDimensionPixelOffset(R.styleable.ShapeConstraintLayout_shape_shadowOffsetY, DEFAULT_SHAPE_SHADOW_OFFSET_Y); - + mShapeDrawableBuilder = new ShapeDrawableBuilder(this, typedArray, STYLEABLE); typedArray.recycle(); - intoBackground(); - } - - /** - * {@link IShapeDrawable} - */ - - @Override - public ShapeConstraintLayout setShapeType(int type) { - mShapeType = type; - return this; - } - - @Override - public int getShapeType() { - return mShapeType; - } - - @Override - public ShapeConstraintLayout setShapeWidth(int width) { - mShapeWidth = width; - return this; - } - - @Override - public int getShapeWidth() { - return mShapeWidth; - } - - @Override - public ShapeConstraintLayout setShapeHeight(int height) { - mShapeHeight = height; - return this; - } - - @Override - public int getShapeHeight() { - return mShapeHeight; - } - - @Override - public ShapeConstraintLayout setSolidColor(int color) { - if (mSolidPressedColor == mSolidColor) { - mSolidPressedColor = color; - } - if (mSolidDisabledColor == mSolidColor) { - mSolidDisabledColor = color; - } - if (mSolidFocusedColor == mSolidColor) { - mSolidFocusedColor = color; - } - if (mSolidSelectedColor == mSolidColor) { - mSolidSelectedColor = color; - } - mSolidColor = color; - mStartColor = color; - mCenterColor = color; - mEndColor = color; - return this; - } - - @Override - public int getSolidColor() { - return mSolidColor; - } - - @Override - public ShapeConstraintLayout setSolidPressedColor(int color) { - mSolidPressedColor = color; - return this; - } - - @Override - public int getSolidPressedColor() { - return mSolidPressedColor; - } - - @Override - public ShapeConstraintLayout setSolidDisabledColor(int color) { - mSolidDisabledColor = color; - return this; - } - - @Override - public int getSolidDisabledColor() { - return mSolidDisabledColor; - } - - @Override - public ShapeConstraintLayout setSolidFocusedColor(int color) { - mSolidFocusedColor = color; - return this; - } - - @Override - public int getSolidFocusedColor() { - return mSolidFocusedColor; - } - - @Override - public ShapeConstraintLayout setSolidSelectedColor(int color) { - mSolidSelectedColor = color; - return this; - } - - @Override - public int getSolidSelectedColor() { - return mSolidSelectedColor; - } - - @Override - public ShapeConstraintLayout setTopLeftRadius(int radius) { - mTopLeftRadius = radius; - return this; - } - - @Override - public int getTopLeftRadius() { - return mTopLeftRadius; - } - - @Override - public ShapeConstraintLayout setTopRightRadius(int radius) { - mTopRightRadius = radius; - return this; - } - - @Override - public int getTopRightRadius() { - return mTopRightRadius; - } - - @Override - public ShapeConstraintLayout setBottomLeftRadius(int radius) { - mBottomLeftRadius = radius; - return this; - } - - @Override - public int getBottomLeftRadius() { - return mBottomLeftRadius; - } - - @Override - public ShapeConstraintLayout setBottomRightRadius(int radius) { - mBottomRightRadius = radius; - return this; - } - - @Override - public int getBottomRightRadius() { - return mBottomRightRadius; - } - - @Override - public ShapeConstraintLayout setStartColor(int color) { - mStartColor = color; - return this; - } - - @Override - public int getStartColor() { - return mStartColor; - } - - @Override - public ShapeConstraintLayout setCenterColor(int color) { - mCenterColor = color; - return this; - } - - @Override - public int getCenterColor() { - return mCenterColor; - } - - @Override - public ShapeConstraintLayout setEndColor(int color) { - mEndColor = color; - return this; - } - - @Override - public int getEndColor() { - return mEndColor; - } - - @Override - public ShapeConstraintLayout setUseLevel(boolean useLevel) { - mUseLevel = useLevel; - return this; - } - - @Override - public boolean isUseLevel() { - return mUseLevel; - } - - @Override - public ShapeConstraintLayout setAngle(int angle) { - mAngle = angle; - return this; - } - - @Override - public int getAngle() { - return mAngle; - } - - @Override - public ShapeConstraintLayout setGradientType(int type) { - mGradientType = type; - return this; - } - - @Override - public int getGradientType() { - return mGradientType; - } - - @Override - public ShapeConstraintLayout setCenterX(float x) { - mCenterX = x; - return this; - } - - @Override - public float getCenterX() { - return mCenterX; - } - - @Override - public ShapeConstraintLayout setCenterY(float y) { - mCenterY = y; - return this; - } - - @Override - public float getCenterY() { - return mCenterY; - } - - @Override - public ShapeConstraintLayout setGradientRadius(int radius) { - mGradientRadius = radius; - return this; - } - - @Override - public int getGradientRadius() { - return mGradientRadius; - } - - @Override - public ShapeConstraintLayout setStrokeColor(int color) { - if (mStrokePressedColor == mStrokeColor) { - mStrokePressedColor = color; - } - if (mStrokeDisabledColor == mStrokeColor) { - mStrokeDisabledColor = color; - } - if (mStrokeFocusedColor == mStrokeColor) { - mStrokeFocusedColor = color; - } - if (mStrokeSelectedColor == mStrokeColor) { - mStrokeSelectedColor = color; - } - mStrokeColor = color; - return this; - } - - @Override - public int getStrokeColor() { - return mStrokeColor; - } - - @Override - public ShapeConstraintLayout setStrokePressedColor(int color) { - mStrokePressedColor = color; - return this; - } - - @Override - public int getStrokePressedColor() { - return mStrokePressedColor; - } - - @Override - public ShapeConstraintLayout setStrokeDisabledColor(int color) { - mStrokeDisabledColor = color; - return this; - } - - @Override - public int getStrokeDisabledColor() { - return mStrokeDisabledColor; - } - - @Override - public ShapeConstraintLayout setStrokeFocusedColor(int color) { - mStrokeFocusedColor = color; - return this; - } - - @Override - public int getStrokeFocusedColor() { - return mStrokeFocusedColor; - } - - @Override - public ShapeConstraintLayout setStrokeSelectedColor(int color) { - mStrokeSelectedColor = color; - return this; - } - - @Override - public int getStrokeSelectedColor() { - return mStrokeSelectedColor; - } - - @Override - public ShapeConstraintLayout setStrokeWidth(int width) { - mStrokeWidth = width; - return this; - } - - @Override - public int getStrokeWidth() { - return mStrokeWidth; - } - - @Override - public ShapeConstraintLayout setDashWidth(int width) { - mDashWidth = width; - return this; - } - - @Override - public int getDashWidth() { - return mDashWidth; - } - - @Override - public ShapeConstraintLayout setDashGap(int gap) { - mDashGap = gap; - return this; - } - - @Override - public int getDashGap() { - return mDashGap; - } - - @Override - public ShapeConstraintLayout setInnerRadius(int radius) { - mInnerRadius = radius; - return this; - } - - @Override - public int getInnerRadius() { - return mInnerRadius; - } - - @Override - public ShapeConstraintLayout setInnerRadiusRatio(float ratio) { - mInnerRadiusRatio = ratio; - return this; - } - - @Override - public float getInnerRadiusRatio() { - return mInnerRadiusRatio; - } - - @Override - public ShapeConstraintLayout setThickness(int size) { - mThickness = size; - return this; - } - - @Override - public int getThickness() { - return mThickness; - } - - @Override - public ShapeConstraintLayout setThicknessRatio(float ratio) { - mThicknessRatio = ratio; - return this; - } - - @Override - public float getThicknessRatio() { - return mThicknessRatio; - } - - @Override - public ShapeConstraintLayout setShadowSize(int size) { - mShadowSize = size; - return this; - } - - @Override - public int getShadowSize() { - return mShadowSize; - } - - @Override - public ShapeConstraintLayout setShadowColor(int color) { - mShadowColor = color; - return this; - } - - @Override - public int getShadowColor() { - return mShadowColor; - } - - @Override - public ShapeConstraintLayout setShadowOffsetX(int offsetX) { - mShadowOffsetX = offsetX; - return this; - } - - @Override - public int getShadowOffsetX() { - return mShadowOffsetX; - } - - @Override - public ShapeConstraintLayout setShadowOffsetY(int offsetY) { - mShadowOffsetY = offsetY; - return this; - } - - @Override - public int getShadowOffsetY() { - return mShadowOffsetY; + mShapeDrawableBuilder.intoBackground(); } - @Override - public void intoBackground() { - Drawable drawable = buildBackgroundDrawable(); - if (drawable == null) { - return; - } - if (isDashLineEnable() || isShadowEnable()) { - // 需要关闭硬件加速,否则虚线或者阴影在某些手机上面无法生效 - setLayerType(View.LAYER_TYPE_SOFTWARE, null); - } - setBackground(drawable); + public ShapeDrawableBuilder getShapeDrawableBuilder() { + return mShapeDrawableBuilder; } } \ No newline at end of file diff --git a/library/src/main/java/com/hjq/shape/layout/ShapeFrameLayout.java b/library/src/main/java/com/hjq/shape/layout/ShapeFrameLayout.java index 151ef6e..a86baee 100644 --- a/library/src/main/java/com/hjq/shape/layout/ShapeFrameLayout.java +++ b/library/src/main/java/com/hjq/shape/layout/ShapeFrameLayout.java @@ -2,13 +2,12 @@ import android.content.Context; import android.content.res.TypedArray; -import android.graphics.drawable.Drawable; import android.util.AttributeSet; -import android.view.View; import android.widget.FrameLayout; import com.hjq.shape.R; -import com.hjq.shape.core.IShapeDrawable; +import com.hjq.shape.builder.ShapeDrawableBuilder; +import com.hjq.shape.styleable.ShapeFrameLayoutStyleable; /** * author : Android 轮子哥 @@ -16,52 +15,11 @@ * time : 2021/07/17 * desc : 支持直接定义 Shape 背景的 FrameLayout */ -public class ShapeFrameLayout extends FrameLayout implements IShapeDrawable { +public class ShapeFrameLayout extends FrameLayout { - private int mShapeType; - private int mShapeWidth; - private int mShapeHeight; + private static final ShapeFrameLayoutStyleable STYLEABLE = new ShapeFrameLayoutStyleable(); - private int mSolidColor; - private int mSolidPressedColor; - private int mSolidDisabledColor; - private int mSolidFocusedColor; - private int mSolidSelectedColor; - - private int mTopLeftRadius; - private int mTopRightRadius; - private int mBottomLeftRadius; - private int mBottomRightRadius; - - private int mStartColor; - private int mCenterColor; - private int mEndColor; - private boolean mUseLevel; - private int mAngle; - private int mGradientType; - private float mCenterX; - private float mCenterY; - private int mGradientRadius; - - private int mStrokeColor; - private int mStrokePressedColor; - private int mStrokeDisabledColor; - private int mStrokeFocusedColor; - private int mStrokeSelectedColor; - - private int mStrokeWidth; - private int mDashWidth; - private int mDashGap; - - private int mInnerRadius; - private float mInnerRadiusRatio; - private int mThickness; - private float mThicknessRatio; - - private int mShadowSize; - private int mShadowColor; - private int mShadowOffsetX; - private int mShadowOffsetY; + private final ShapeDrawableBuilder mShapeDrawableBuilder; public ShapeFrameLayout(Context context) { this(context, null); @@ -75,505 +33,13 @@ public ShapeFrameLayout(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.ShapeFrameLayout); - mShapeType = typedArray.getInt(R.styleable.ShapeFrameLayout_shape, DEFAULT_SHAPE_TYPE); - mShapeWidth = typedArray.getDimensionPixelSize(R.styleable.ShapeFrameLayout_shape_width, DEFAULT_SHAPE_WIDTH); - mShapeHeight = typedArray.getDimensionPixelSize(R.styleable.ShapeFrameLayout_shape_height, DEFAULT_SHAPE_HEIGHT); - - mSolidColor = typedArray.getColor(R.styleable.ShapeFrameLayout_shape_solidColor, DEFAULT_SHAPE_SOLID_COLOR); - mSolidPressedColor = typedArray.getColor(R.styleable.ShapeFrameLayout_shape_solidPressedColor, mSolidColor); - mSolidDisabledColor = typedArray.getColor(R.styleable.ShapeFrameLayout_shape_solidDisabledColor, mSolidColor); - mSolidFocusedColor = typedArray.getColor(R.styleable.ShapeFrameLayout_shape_solidFocusedColor, mSolidColor); - mSolidSelectedColor = typedArray.getColor(R.styleable.ShapeFrameLayout_shape_solidSelectedColor, mSolidColor); - - int radius = typedArray.getDimensionPixelSize(R.styleable.ShapeFrameLayout_shape_radius, DEFAULT_SHAPE_RADIUS); - mTopLeftRadius = typedArray.getDimensionPixelSize(R.styleable.ShapeFrameLayout_shape_topLeftRadius, radius); - mTopRightRadius = typedArray.getDimensionPixelSize(R.styleable.ShapeFrameLayout_shape_topRightRadius, radius); - mBottomLeftRadius = typedArray.getDimensionPixelSize(R.styleable.ShapeFrameLayout_shape_bottomLeftRadius, radius); - mBottomRightRadius = typedArray.getDimensionPixelSize(R.styleable.ShapeFrameLayout_shape_bottomRightRadius, radius); - - mStartColor = typedArray.getColor(R.styleable.ShapeFrameLayout_shape_startColor, mSolidColor); - mCenterColor = typedArray.getColor(R.styleable.ShapeFrameLayout_shape_centerColor, mSolidColor); - mEndColor = typedArray.getColor(R.styleable.ShapeFrameLayout_shape_endColor, mSolidColor); - mUseLevel = typedArray.getBoolean(R.styleable.ShapeFrameLayout_shape_useLevel, DEFAULT_SHAPE_USE_LEVEL); - mAngle = (int) typedArray.getFloat(R.styleable.ShapeFrameLayout_shape_angle, DEFAULT_SHAPE_ANGLE); - mGradientType = typedArray.getInt(R.styleable.ShapeFrameLayout_shape_gradientType, DEFAULT_SHAPE_GRADIENT_TYPE); - mCenterX = typedArray.getFloat(R.styleable.ShapeFrameLayout_shape_centerX, DEFAULT_SHAPE_CENTER_X); - mCenterY = typedArray.getFloat(R.styleable.ShapeFrameLayout_shape_centerY, DEFAULT_SHAPE_CENTER_Y); - mGradientRadius = typedArray.getDimensionPixelSize(R.styleable.ShapeFrameLayout_shape_gradientRadius, radius); - - mStrokeColor = typedArray.getColor(R.styleable.ShapeFrameLayout_shape_strokeColor, DEFAULT_SHAPE_STROKE_COLOR); - mStrokePressedColor = typedArray.getColor(R.styleable.ShapeFrameLayout_shape_strokePressedColor, mStrokeColor); - mStrokeDisabledColor = typedArray.getColor(R.styleable.ShapeFrameLayout_shape_strokeDisabledColor, mStrokeColor); - mStrokeFocusedColor = typedArray.getColor(R.styleable.ShapeFrameLayout_shape_strokeFocusedColor, mStrokeColor); - mStrokeSelectedColor = typedArray.getColor(R.styleable.ShapeFrameLayout_shape_strokeSelectedColor, mStrokeColor); - - mStrokeWidth = typedArray.getDimensionPixelSize(R.styleable.ShapeFrameLayout_shape_strokeWidth, DEFAULT_SHAPE_STROKE_WIDTH); - mDashWidth = typedArray.getDimensionPixelSize(R.styleable.ShapeFrameLayout_shape_dashWidth, DEFAULT_SHAPE_DASH_WIDTH); - mDashGap = typedArray.getDimensionPixelSize(R.styleable.ShapeFrameLayout_shape_dashGap, DEFAULT_SHAPE_DASH_GAP); - - mInnerRadius = typedArray.getDimensionPixelOffset(R.styleable.ShapeFrameLayout_shape_innerRadius, DEFAULT_SHAPE_INNER_RADIUS); - mInnerRadiusRatio = typedArray.getFloat(R.styleable.ShapeFrameLayout_shape_innerRadiusRatio, DEFAULT_SHAPE_INNER_RADIUS_RATIO); - mThickness = typedArray.getDimensionPixelOffset(R.styleable.ShapeFrameLayout_shape_thickness, DEFAULT_SHAPE_THICKNESS); - mThicknessRatio = typedArray.getFloat(R.styleable.ShapeFrameLayout_shape_thicknessRatio, DEFAULT_SHAPE_THICKNESS_RATIO); - - mShadowSize = typedArray.getDimensionPixelSize(R.styleable.ShapeFrameLayout_shape_shadowSize, DEFAULT_SHAPE_SHADOW_SIZE); - mShadowColor = typedArray.getColor(R.styleable.ShapeFrameLayout_shape_shadowColor, DEFAULT_SHAPE_SHADOW_COLOR); - mShadowOffsetX = typedArray.getDimensionPixelOffset(R.styleable.ShapeFrameLayout_shape_shadowOffsetX, DEFAULT_SHAPE_SHADOW_OFFSET_X); - mShadowOffsetY = typedArray.getDimensionPixelOffset(R.styleable.ShapeFrameLayout_shape_shadowOffsetY, DEFAULT_SHAPE_SHADOW_OFFSET_Y); - + mShapeDrawableBuilder = new ShapeDrawableBuilder(this, typedArray, STYLEABLE); typedArray.recycle(); - intoBackground(); - } - - /** - * {@link IShapeDrawable} - */ - - @Override - public ShapeFrameLayout setShapeType(int type) { - mShapeType = type; - return this; - } - - @Override - public int getShapeType() { - return mShapeType; - } - - @Override - public ShapeFrameLayout setShapeWidth(int width) { - mShapeWidth = width; - return this; - } - - @Override - public int getShapeWidth() { - return mShapeWidth; - } - - @Override - public ShapeFrameLayout setShapeHeight(int height) { - mShapeHeight = height; - return this; - } - - @Override - public int getShapeHeight() { - return mShapeHeight; - } - - @Override - public ShapeFrameLayout setSolidColor(int color) { - if (mSolidPressedColor == mSolidColor) { - mSolidPressedColor = color; - } - if (mSolidDisabledColor == mSolidColor) { - mSolidDisabledColor = color; - } - if (mSolidFocusedColor == mSolidColor) { - mSolidFocusedColor = color; - } - if (mSolidSelectedColor == mSolidColor) { - mSolidSelectedColor = color; - } - mSolidColor = color; - mStartColor = color; - mCenterColor = color; - mEndColor = color; - return this; - } - - @Override - public int getSolidColor() { - return mSolidColor; - } - - @Override - public ShapeFrameLayout setSolidPressedColor(int color) { - mSolidPressedColor = color; - return this; - } - - @Override - public int getSolidPressedColor() { - return mSolidPressedColor; - } - - @Override - public ShapeFrameLayout setSolidDisabledColor(int color) { - mSolidDisabledColor = color; - return this; - } - - @Override - public int getSolidDisabledColor() { - return mSolidDisabledColor; - } - - @Override - public ShapeFrameLayout setSolidFocusedColor(int color) { - mSolidFocusedColor = color; - return this; - } - - @Override - public int getSolidFocusedColor() { - return mSolidFocusedColor; - } - - @Override - public ShapeFrameLayout setSolidSelectedColor(int color) { - mSolidSelectedColor = color; - return this; - } - - @Override - public int getSolidSelectedColor() { - return mSolidSelectedColor; - } - - @Override - public ShapeFrameLayout setTopLeftRadius(int radius) { - mTopLeftRadius = radius; - return this; - } - - @Override - public int getTopLeftRadius() { - return mTopLeftRadius; - } - - @Override - public ShapeFrameLayout setTopRightRadius(int radius) { - mTopRightRadius = radius; - return this; - } - - @Override - public int getTopRightRadius() { - return mTopRightRadius; - } - - @Override - public ShapeFrameLayout setBottomLeftRadius(int radius) { - mBottomLeftRadius = radius; - return this; - } - - @Override - public int getBottomLeftRadius() { - return mBottomLeftRadius; - } - - @Override - public ShapeFrameLayout setBottomRightRadius(int radius) { - mBottomRightRadius = radius; - return this; - } - - @Override - public int getBottomRightRadius() { - return mBottomRightRadius; - } - - @Override - public ShapeFrameLayout setStartColor(int color) { - mStartColor = color; - return this; - } - - @Override - public int getStartColor() { - return mStartColor; - } - - @Override - public ShapeFrameLayout setCenterColor(int color) { - mCenterColor = color; - return this; - } - - @Override - public int getCenterColor() { - return mCenterColor; - } - - @Override - public ShapeFrameLayout setEndColor(int color) { - mEndColor = color; - return this; - } - - @Override - public int getEndColor() { - return mEndColor; - } - - @Override - public ShapeFrameLayout setUseLevel(boolean useLevel) { - mUseLevel = useLevel; - return this; - } - - @Override - public boolean isUseLevel() { - return mUseLevel; - } - - @Override - public ShapeFrameLayout setAngle(int angle) { - mAngle = angle; - return this; - } - - @Override - public int getAngle() { - return mAngle; - } - - @Override - public ShapeFrameLayout setGradientType(int type) { - mGradientType = type; - return this; - } - - @Override - public int getGradientType() { - return mGradientType; - } - - @Override - public ShapeFrameLayout setCenterX(float x) { - mCenterX = x; - return this; - } - - @Override - public float getCenterX() { - return mCenterX; - } - - @Override - public ShapeFrameLayout setCenterY(float y) { - mCenterY = y; - return this; - } - - @Override - public float getCenterY() { - return mCenterY; - } - - @Override - public ShapeFrameLayout setGradientRadius(int radius) { - mGradientRadius = radius; - return this; - } - - @Override - public int getGradientRadius() { - return mGradientRadius; - } - - @Override - public ShapeFrameLayout setStrokeColor(int color) { - if (mStrokePressedColor == mStrokeColor) { - mStrokePressedColor = color; - } - if (mStrokeDisabledColor == mStrokeColor) { - mStrokeDisabledColor = color; - } - if (mStrokeFocusedColor == mStrokeColor) { - mStrokeFocusedColor = color; - } - if (mStrokeSelectedColor == mStrokeColor) { - mStrokeSelectedColor = color; - } - mStrokeColor = color; - return this; - } - - @Override - public int getStrokeColor() { - return mStrokeColor; - } - - @Override - public ShapeFrameLayout setStrokePressedColor(int color) { - mStrokePressedColor = color; - return this; - } - - @Override - public int getStrokePressedColor() { - return mStrokePressedColor; - } - - @Override - public ShapeFrameLayout setStrokeDisabledColor(int color) { - mStrokeDisabledColor = color; - return this; - } - - @Override - public int getStrokeDisabledColor() { - return mStrokeDisabledColor; - } - - @Override - public ShapeFrameLayout setStrokeFocusedColor(int color) { - mStrokeFocusedColor = color; - return this; - } - - @Override - public int getStrokeFocusedColor() { - return mStrokeFocusedColor; - } - - @Override - public ShapeFrameLayout setStrokeSelectedColor(int color) { - mStrokeSelectedColor = color; - return this; - } - - @Override - public int getStrokeSelectedColor() { - return mStrokeSelectedColor; - } - - @Override - public ShapeFrameLayout setStrokeWidth(int width) { - mStrokeWidth = width; - return this; - } - - @Override - public int getStrokeWidth() { - return mStrokeWidth; - } - - @Override - public ShapeFrameLayout setDashWidth(int width) { - mDashWidth = width; - return this; - } - - @Override - public int getDashWidth() { - return mDashWidth; - } - - @Override - public ShapeFrameLayout setDashGap(int gap) { - mDashGap = gap; - return this; - } - - @Override - public int getDashGap() { - return mDashGap; - } - - @Override - public ShapeFrameLayout setInnerRadius(int radius) { - mInnerRadius = radius; - return this; - } - - @Override - public int getInnerRadius() { - return mInnerRadius; - } - - @Override - public ShapeFrameLayout setInnerRadiusRatio(float ratio) { - mInnerRadiusRatio = ratio; - return this; - } - - @Override - public float getInnerRadiusRatio() { - return mInnerRadiusRatio; - } - - @Override - public ShapeFrameLayout setThickness(int size) { - mThickness = size; - return this; - } - - @Override - public int getThickness() { - return mThickness; - } - - @Override - public ShapeFrameLayout setThicknessRatio(float ratio) { - mThicknessRatio = ratio; - return this; - } - - @Override - public float getThicknessRatio() { - return mThicknessRatio; - } - - @Override - public ShapeFrameLayout setShadowSize(int size) { - mShadowSize = size; - return this; - } - - @Override - public int getShadowSize() { - return mShadowSize; - } - - @Override - public ShapeFrameLayout setShadowColor(int color) { - mShadowColor = color; - return this; - } - - @Override - public int getShadowColor() { - return mShadowColor; - } - - @Override - public ShapeFrameLayout setShadowOffsetX(int offsetX) { - mShadowOffsetX = offsetX; - return this; - } - - @Override - public int getShadowOffsetX() { - return mShadowOffsetX; - } - - @Override - public ShapeFrameLayout setShadowOffsetY(int offsetY) { - mShadowOffsetY = offsetY; - return this; - } - - @Override - public int getShadowOffsetY() { - return mShadowOffsetY; + mShapeDrawableBuilder.intoBackground(); } - @Override - public void intoBackground() { - Drawable drawable = buildBackgroundDrawable(); - if (drawable == null) { - return; - } - if (isDashLineEnable() || isShadowEnable()) { - // 需要关闭硬件加速,否则虚线或者阴影在某些手机上面无法生效 - setLayerType(View.LAYER_TYPE_SOFTWARE, null); - } - setBackground(drawable); + public ShapeDrawableBuilder getShapeDrawableBuilder() { + return mShapeDrawableBuilder; } } \ No newline at end of file diff --git a/library/src/main/java/com/hjq/shape/layout/ShapeLinearLayout.java b/library/src/main/java/com/hjq/shape/layout/ShapeLinearLayout.java index 7c85468..1678b82 100644 --- a/library/src/main/java/com/hjq/shape/layout/ShapeLinearLayout.java +++ b/library/src/main/java/com/hjq/shape/layout/ShapeLinearLayout.java @@ -2,13 +2,12 @@ import android.content.Context; import android.content.res.TypedArray; -import android.graphics.drawable.Drawable; import android.util.AttributeSet; -import android.view.View; import android.widget.LinearLayout; import com.hjq.shape.R; -import com.hjq.shape.core.IShapeDrawable; +import com.hjq.shape.builder.ShapeDrawableBuilder; +import com.hjq.shape.styleable.ShapeLinearLayoutStyleable; /** * author : Android 轮子哥 @@ -16,52 +15,11 @@ * time : 2021/07/17 * desc : 支持直接定义 Shape 背景的 LinearLayout */ -public class ShapeLinearLayout extends LinearLayout implements IShapeDrawable { +public class ShapeLinearLayout extends LinearLayout { - private int mShapeType; - private int mShapeWidth; - private int mShapeHeight; + private static final ShapeLinearLayoutStyleable STYLEABLE = new ShapeLinearLayoutStyleable(); - private int mSolidColor; - private int mSolidPressedColor; - private int mSolidDisabledColor; - private int mSolidFocusedColor; - private int mSolidSelectedColor; - - private int mTopLeftRadius; - private int mTopRightRadius; - private int mBottomLeftRadius; - private int mBottomRightRadius; - - private int mStartColor; - private int mCenterColor; - private int mEndColor; - private boolean mUseLevel; - private int mAngle; - private int mGradientType; - private float mCenterX; - private float mCenterY; - private int mGradientRadius; - - private int mStrokeColor; - private int mStrokePressedColor; - private int mStrokeDisabledColor; - private int mStrokeFocusedColor; - private int mStrokeSelectedColor; - - private int mStrokeWidth; - private int mDashWidth; - private int mDashGap; - - private int mInnerRadius; - private float mInnerRadiusRatio; - private int mThickness; - private float mThicknessRatio; - - private int mShadowSize; - private int mShadowColor; - private int mShadowOffsetX; - private int mShadowOffsetY; + private final ShapeDrawableBuilder mShapeDrawableBuilder; public ShapeLinearLayout(Context context) { this(context, null); @@ -75,505 +33,13 @@ public ShapeLinearLayout(Context context, AttributeSet attrs, int defStyleAttr) super(context, attrs, defStyleAttr); TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.ShapeLinearLayout); - mShapeType = typedArray.getInt(R.styleable.ShapeLinearLayout_shape, DEFAULT_SHAPE_TYPE); - mShapeWidth = typedArray.getDimensionPixelSize(R.styleable.ShapeLinearLayout_shape_width, DEFAULT_SHAPE_WIDTH); - mShapeHeight = typedArray.getDimensionPixelSize(R.styleable.ShapeLinearLayout_shape_height, DEFAULT_SHAPE_HEIGHT); - - mSolidColor = typedArray.getColor(R.styleable.ShapeLinearLayout_shape_solidColor, DEFAULT_SHAPE_SOLID_COLOR); - mSolidPressedColor = typedArray.getColor(R.styleable.ShapeLinearLayout_shape_solidPressedColor, mSolidColor); - mSolidDisabledColor = typedArray.getColor(R.styleable.ShapeLinearLayout_shape_solidDisabledColor, mSolidColor); - mSolidFocusedColor = typedArray.getColor(R.styleable.ShapeLinearLayout_shape_solidFocusedColor, mSolidColor); - mSolidSelectedColor = typedArray.getColor(R.styleable.ShapeLinearLayout_shape_solidSelectedColor, mSolidColor); - - int radius = typedArray.getDimensionPixelSize(R.styleable.ShapeLinearLayout_shape_radius, DEFAULT_SHAPE_RADIUS); - mTopLeftRadius = typedArray.getDimensionPixelSize(R.styleable.ShapeLinearLayout_shape_topLeftRadius, radius); - mTopRightRadius = typedArray.getDimensionPixelSize(R.styleable.ShapeLinearLayout_shape_topRightRadius, radius); - mBottomLeftRadius = typedArray.getDimensionPixelSize(R.styleable.ShapeLinearLayout_shape_bottomLeftRadius, radius); - mBottomRightRadius = typedArray.getDimensionPixelSize(R.styleable.ShapeLinearLayout_shape_bottomRightRadius, radius); - - mStartColor = typedArray.getColor(R.styleable.ShapeLinearLayout_shape_startColor, mSolidColor); - mCenterColor = typedArray.getColor(R.styleable.ShapeLinearLayout_shape_centerColor, mSolidColor); - mEndColor = typedArray.getColor(R.styleable.ShapeLinearLayout_shape_endColor, mSolidColor); - mUseLevel = typedArray.getBoolean(R.styleable.ShapeLinearLayout_shape_useLevel, DEFAULT_SHAPE_USE_LEVEL); - mAngle = (int) typedArray.getFloat(R.styleable.ShapeLinearLayout_shape_angle, DEFAULT_SHAPE_ANGLE); - mGradientType = typedArray.getInt(R.styleable.ShapeLinearLayout_shape_gradientType, DEFAULT_SHAPE_GRADIENT_TYPE); - mCenterX = typedArray.getFloat(R.styleable.ShapeLinearLayout_shape_centerX, DEFAULT_SHAPE_CENTER_X); - mCenterY = typedArray.getFloat(R.styleable.ShapeLinearLayout_shape_centerY, DEFAULT_SHAPE_CENTER_Y); - mGradientRadius = typedArray.getDimensionPixelSize(R.styleable.ShapeLinearLayout_shape_gradientRadius, radius); - - mStrokeColor = typedArray.getColor(R.styleable.ShapeLinearLayout_shape_strokeColor, DEFAULT_SHAPE_STROKE_COLOR); - mStrokePressedColor = typedArray.getColor(R.styleable.ShapeLinearLayout_shape_strokePressedColor, mStrokeColor); - mStrokeDisabledColor = typedArray.getColor(R.styleable.ShapeLinearLayout_shape_strokeDisabledColor, mStrokeColor); - mStrokeFocusedColor = typedArray.getColor(R.styleable.ShapeLinearLayout_shape_strokeFocusedColor, mStrokeColor); - mStrokeSelectedColor = typedArray.getColor(R.styleable.ShapeLinearLayout_shape_strokeSelectedColor, mStrokeColor); - - mStrokeWidth = typedArray.getDimensionPixelSize(R.styleable.ShapeLinearLayout_shape_strokeWidth, DEFAULT_SHAPE_STROKE_WIDTH); - mDashWidth = typedArray.getDimensionPixelSize(R.styleable.ShapeLinearLayout_shape_dashWidth, DEFAULT_SHAPE_DASH_WIDTH); - mDashGap = typedArray.getDimensionPixelSize(R.styleable.ShapeLinearLayout_shape_dashGap, DEFAULT_SHAPE_DASH_GAP); - - mInnerRadius = typedArray.getDimensionPixelOffset(R.styleable.ShapeLinearLayout_shape_innerRadius, DEFAULT_SHAPE_INNER_RADIUS); - mInnerRadiusRatio = typedArray.getFloat(R.styleable.ShapeLinearLayout_shape_innerRadiusRatio, DEFAULT_SHAPE_INNER_RADIUS_RATIO); - mThickness = typedArray.getDimensionPixelOffset(R.styleable.ShapeLinearLayout_shape_thickness, DEFAULT_SHAPE_THICKNESS); - mThicknessRatio = typedArray.getFloat(R.styleable.ShapeLinearLayout_shape_thicknessRatio, DEFAULT_SHAPE_THICKNESS_RATIO); - - mShadowSize = typedArray.getDimensionPixelSize(R.styleable.ShapeLinearLayout_shape_shadowSize, DEFAULT_SHAPE_SHADOW_SIZE); - mShadowColor = typedArray.getColor(R.styleable.ShapeLinearLayout_shape_shadowColor, DEFAULT_SHAPE_SHADOW_COLOR); - mShadowOffsetX = typedArray.getDimensionPixelOffset(R.styleable.ShapeLinearLayout_shape_shadowOffsetX, DEFAULT_SHAPE_SHADOW_OFFSET_X); - mShadowOffsetY = typedArray.getDimensionPixelOffset(R.styleable.ShapeLinearLayout_shape_shadowOffsetY, DEFAULT_SHAPE_SHADOW_OFFSET_Y); - + mShapeDrawableBuilder = new ShapeDrawableBuilder(this, typedArray, STYLEABLE); typedArray.recycle(); - intoBackground(); - } - - /** - * {@link IShapeDrawable} - */ - - @Override - public ShapeLinearLayout setShapeType(int type) { - mShapeType = type; - return this; - } - - @Override - public int getShapeType() { - return mShapeType; - } - - @Override - public ShapeLinearLayout setShapeWidth(int width) { - mShapeWidth = width; - return this; - } - - @Override - public int getShapeWidth() { - return mShapeWidth; - } - - @Override - public ShapeLinearLayout setShapeHeight(int height) { - mShapeHeight = height; - return this; - } - - @Override - public int getShapeHeight() { - return mShapeHeight; - } - - @Override - public ShapeLinearLayout setSolidColor(int color) { - if (mSolidPressedColor == mSolidColor) { - mSolidPressedColor = color; - } - if (mSolidDisabledColor == mSolidColor) { - mSolidDisabledColor = color; - } - if (mSolidFocusedColor == mSolidColor) { - mSolidFocusedColor = color; - } - if (mSolidSelectedColor == mSolidColor) { - mSolidSelectedColor = color; - } - mSolidColor = color; - mStartColor = color; - mCenterColor = color; - mEndColor = color; - return this; - } - - @Override - public int getSolidColor() { - return mSolidColor; - } - - @Override - public ShapeLinearLayout setSolidPressedColor(int color) { - mSolidPressedColor = color; - return this; - } - - @Override - public int getSolidPressedColor() { - return mSolidPressedColor; - } - - @Override - public ShapeLinearLayout setSolidDisabledColor(int color) { - mSolidDisabledColor = color; - return this; - } - - @Override - public int getSolidDisabledColor() { - return mSolidDisabledColor; - } - - @Override - public ShapeLinearLayout setSolidFocusedColor(int color) { - mSolidFocusedColor = color; - return this; - } - - @Override - public int getSolidFocusedColor() { - return mSolidFocusedColor; - } - - @Override - public ShapeLinearLayout setSolidSelectedColor(int color) { - mSolidSelectedColor = color; - return this; - } - - @Override - public int getSolidSelectedColor() { - return mSolidSelectedColor; - } - - @Override - public ShapeLinearLayout setTopLeftRadius(int radius) { - mTopLeftRadius = radius; - return this; - } - - @Override - public int getTopLeftRadius() { - return mTopLeftRadius; - } - - @Override - public ShapeLinearLayout setTopRightRadius(int radius) { - mTopRightRadius = radius; - return this; - } - - @Override - public int getTopRightRadius() { - return mTopRightRadius; - } - - @Override - public ShapeLinearLayout setBottomLeftRadius(int radius) { - mBottomLeftRadius = radius; - return this; - } - - @Override - public int getBottomLeftRadius() { - return mBottomLeftRadius; - } - - @Override - public ShapeLinearLayout setBottomRightRadius(int radius) { - mBottomRightRadius = radius; - return this; - } - - @Override - public int getBottomRightRadius() { - return mBottomRightRadius; - } - - @Override - public ShapeLinearLayout setStartColor(int color) { - mStartColor = color; - return this; - } - - @Override - public int getStartColor() { - return mStartColor; - } - - @Override - public ShapeLinearLayout setCenterColor(int color) { - mCenterColor = color; - return this; - } - - @Override - public int getCenterColor() { - return mCenterColor; - } - - @Override - public ShapeLinearLayout setEndColor(int color) { - mEndColor = color; - return this; - } - - @Override - public int getEndColor() { - return mEndColor; - } - - @Override - public ShapeLinearLayout setUseLevel(boolean useLevel) { - mUseLevel = useLevel; - return this; - } - - @Override - public boolean isUseLevel() { - return mUseLevel; - } - - @Override - public ShapeLinearLayout setAngle(int angle) { - mAngle = angle; - return this; - } - - @Override - public int getAngle() { - return mAngle; - } - - @Override - public ShapeLinearLayout setGradientType(int type) { - mGradientType = type; - return this; - } - - @Override - public int getGradientType() { - return mGradientType; - } - - @Override - public ShapeLinearLayout setCenterX(float x) { - mCenterX = x; - return this; - } - - @Override - public float getCenterX() { - return mCenterX; - } - - @Override - public ShapeLinearLayout setCenterY(float y) { - mCenterY = y; - return this; - } - - @Override - public float getCenterY() { - return mCenterY; - } - - @Override - public ShapeLinearLayout setGradientRadius(int radius) { - mGradientRadius = radius; - return this; - } - - @Override - public int getGradientRadius() { - return mGradientRadius; - } - - @Override - public ShapeLinearLayout setStrokeColor(int color) { - if (mStrokePressedColor == mStrokeColor) { - mStrokePressedColor = color; - } - if (mStrokeDisabledColor == mStrokeColor) { - mStrokeDisabledColor = color; - } - if (mStrokeFocusedColor == mStrokeColor) { - mStrokeFocusedColor = color; - } - if (mStrokeSelectedColor == mStrokeColor) { - mStrokeSelectedColor = color; - } - mStrokeColor = color; - return this; - } - - @Override - public int getStrokeColor() { - return mStrokeColor; - } - - @Override - public ShapeLinearLayout setStrokePressedColor(int color) { - mStrokePressedColor = color; - return this; - } - - @Override - public int getStrokePressedColor() { - return mStrokePressedColor; - } - - @Override - public ShapeLinearLayout setStrokeDisabledColor(int color) { - mStrokeDisabledColor = color; - return this; - } - - @Override - public int getStrokeDisabledColor() { - return mStrokeDisabledColor; - } - - @Override - public ShapeLinearLayout setStrokeFocusedColor(int color) { - mStrokeFocusedColor = color; - return this; - } - - @Override - public int getStrokeFocusedColor() { - return mStrokeFocusedColor; - } - - @Override - public ShapeLinearLayout setStrokeSelectedColor(int color) { - mStrokeSelectedColor = color; - return this; - } - - @Override - public int getStrokeSelectedColor() { - return mStrokeSelectedColor; - } - - @Override - public ShapeLinearLayout setStrokeWidth(int width) { - mStrokeWidth = width; - return this; - } - - @Override - public int getStrokeWidth() { - return mStrokeWidth; - } - - @Override - public ShapeLinearLayout setDashWidth(int width) { - mDashWidth = width; - return this; - } - - @Override - public int getDashWidth() { - return mDashWidth; - } - - @Override - public ShapeLinearLayout setDashGap(int gap) { - mDashGap = gap; - return this; - } - - @Override - public int getDashGap() { - return mDashGap; - } - - @Override - public ShapeLinearLayout setInnerRadius(int radius) { - mInnerRadius = radius; - return this; - } - - @Override - public int getInnerRadius() { - return mInnerRadius; - } - - @Override - public ShapeLinearLayout setInnerRadiusRatio(float ratio) { - mInnerRadiusRatio = ratio; - return this; - } - - @Override - public float getInnerRadiusRatio() { - return mInnerRadiusRatio; - } - - @Override - public ShapeLinearLayout setThickness(int size) { - mThickness = size; - return this; - } - - @Override - public int getThickness() { - return mThickness; - } - - @Override - public ShapeLinearLayout setThicknessRatio(float ratio) { - mThicknessRatio = ratio; - return this; - } - - @Override - public float getThicknessRatio() { - return mThicknessRatio; - } - - @Override - public ShapeLinearLayout setShadowSize(int size) { - mShadowSize = size; - return this; - } - - @Override - public int getShadowSize() { - return mShadowSize; - } - - @Override - public ShapeLinearLayout setShadowColor(int color) { - mShadowColor = color; - return this; - } - - @Override - public int getShadowColor() { - return mShadowColor; - } - - @Override - public ShapeLinearLayout setShadowOffsetX(int offsetX) { - mShadowOffsetX = offsetX; - return this; - } - - @Override - public int getShadowOffsetX() { - return mShadowOffsetX; - } - - @Override - public ShapeLinearLayout setShadowOffsetY(int offsetY) { - mShadowOffsetY = offsetY; - return this; - } - - @Override - public int getShadowOffsetY() { - return mShadowOffsetY; + mShapeDrawableBuilder.intoBackground(); } - @Override - public void intoBackground() { - Drawable drawable = buildBackgroundDrawable(); - if (drawable == null) { - return; - } - if (isDashLineEnable() || isShadowEnable()) { - // 需要关闭硬件加速,否则虚线或者阴影在某些手机上面无法生效 - setLayerType(View.LAYER_TYPE_SOFTWARE, null); - } - setBackground(drawable); + public ShapeDrawableBuilder getShapeDrawableBuilder() { + return mShapeDrawableBuilder; } } \ No newline at end of file diff --git a/library/src/main/java/com/hjq/shape/layout/ShapeRecyclerView.java b/library/src/main/java/com/hjq/shape/layout/ShapeRecyclerView.java index 3c2a0f2..7e176bb 100644 --- a/library/src/main/java/com/hjq/shape/layout/ShapeRecyclerView.java +++ b/library/src/main/java/com/hjq/shape/layout/ShapeRecyclerView.java @@ -2,13 +2,12 @@ import android.content.Context; import android.content.res.TypedArray; -import android.graphics.drawable.Drawable; import android.support.v7.widget.RecyclerView; import android.util.AttributeSet; -import android.view.View; import com.hjq.shape.R; -import com.hjq.shape.core.IShapeDrawable; +import com.hjq.shape.builder.ShapeDrawableBuilder; +import com.hjq.shape.styleable.ShapeRecyclerViewStyleable; /** * author : Android 轮子哥 @@ -16,52 +15,11 @@ * time : 2021/07/17 * desc : 支持直接定义 Shape 背景的 RecyclerView */ -public class ShapeRecyclerView extends RecyclerView implements IShapeDrawable { +public class ShapeRecyclerView extends RecyclerView { - private int mShapeType; - private int mShapeWidth; - private int mShapeHeight; + private static final ShapeRecyclerViewStyleable STYLEABLE = new ShapeRecyclerViewStyleable(); - private int mSolidColor; - private int mSolidPressedColor; - private int mSolidDisabledColor; - private int mSolidFocusedColor; - private int mSolidSelectedColor; - - private int mTopLeftRadius; - private int mTopRightRadius; - private int mBottomLeftRadius; - private int mBottomRightRadius; - - private int mStartColor; - private int mCenterColor; - private int mEndColor; - private boolean mUseLevel; - private int mAngle; - private int mGradientType; - private float mCenterX; - private float mCenterY; - private int mGradientRadius; - - private int mStrokeColor; - private int mStrokePressedColor; - private int mStrokeDisabledColor; - private int mStrokeFocusedColor; - private int mStrokeSelectedColor; - - private int mStrokeWidth; - private int mDashWidth; - private int mDashGap; - - private int mInnerRadius; - private float mInnerRadiusRatio; - private int mThickness; - private float mThicknessRatio; - - private int mShadowSize; - private int mShadowColor; - private int mShadowOffsetX; - private int mShadowOffsetY; + private final ShapeDrawableBuilder mShapeDrawableBuilder; public ShapeRecyclerView(Context context) { this(context, null); @@ -75,505 +33,13 @@ public ShapeRecyclerView(Context context, AttributeSet attrs, int defStyleAttr) super(context, attrs, defStyleAttr); TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.ShapeRecyclerView); - mShapeType = typedArray.getInt(R.styleable.ShapeRecyclerView_shape, DEFAULT_SHAPE_TYPE); - mShapeWidth = typedArray.getDimensionPixelSize(R.styleable.ShapeRecyclerView_shape_width, DEFAULT_SHAPE_WIDTH); - mShapeHeight = typedArray.getDimensionPixelSize(R.styleable.ShapeRecyclerView_shape_height, DEFAULT_SHAPE_HEIGHT); - - mSolidColor = typedArray.getColor(R.styleable.ShapeRecyclerView_shape_solidColor, DEFAULT_SHAPE_SOLID_COLOR); - mSolidPressedColor = typedArray.getColor(R.styleable.ShapeRecyclerView_shape_solidPressedColor, mSolidColor); - mSolidDisabledColor = typedArray.getColor(R.styleable.ShapeRecyclerView_shape_solidDisabledColor, mSolidColor); - mSolidFocusedColor = typedArray.getColor(R.styleable.ShapeRecyclerView_shape_solidFocusedColor, mSolidColor); - mSolidSelectedColor = typedArray.getColor(R.styleable.ShapeRecyclerView_shape_solidSelectedColor, mSolidColor); - - int radius = typedArray.getDimensionPixelSize(R.styleable.ShapeRecyclerView_shape_radius, DEFAULT_SHAPE_RADIUS); - mTopLeftRadius = typedArray.getDimensionPixelSize(R.styleable.ShapeRecyclerView_shape_topLeftRadius, radius); - mTopRightRadius = typedArray.getDimensionPixelSize(R.styleable.ShapeRecyclerView_shape_topRightRadius, radius); - mBottomLeftRadius = typedArray.getDimensionPixelSize(R.styleable.ShapeRecyclerView_shape_bottomLeftRadius, radius); - mBottomRightRadius = typedArray.getDimensionPixelSize(R.styleable.ShapeRecyclerView_shape_bottomRightRadius, radius); - - mStartColor = typedArray.getColor(R.styleable.ShapeRecyclerView_shape_startColor, mSolidColor); - mCenterColor = typedArray.getColor(R.styleable.ShapeRecyclerView_shape_centerColor, mSolidColor); - mEndColor = typedArray.getColor(R.styleable.ShapeRecyclerView_shape_endColor, mSolidColor); - mUseLevel = typedArray.getBoolean(R.styleable.ShapeRecyclerView_shape_useLevel, DEFAULT_SHAPE_USE_LEVEL); - mAngle = (int) typedArray.getFloat(R.styleable.ShapeRecyclerView_shape_angle, DEFAULT_SHAPE_ANGLE); - mGradientType = typedArray.getInt(R.styleable.ShapeRecyclerView_shape_gradientType, DEFAULT_SHAPE_GRADIENT_TYPE); - mCenterX = typedArray.getFloat(R.styleable.ShapeRecyclerView_shape_centerX, DEFAULT_SHAPE_CENTER_X); - mCenterY = typedArray.getFloat(R.styleable.ShapeRecyclerView_shape_centerY, DEFAULT_SHAPE_CENTER_Y); - mGradientRadius = typedArray.getDimensionPixelSize(R.styleable.ShapeRecyclerView_shape_gradientRadius, radius); - - mStrokeColor = typedArray.getColor(R.styleable.ShapeRecyclerView_shape_strokeColor, DEFAULT_SHAPE_STROKE_COLOR); - mStrokePressedColor = typedArray.getColor(R.styleable.ShapeRecyclerView_shape_strokePressedColor, mStrokeColor); - mStrokeDisabledColor = typedArray.getColor(R.styleable.ShapeRecyclerView_shape_strokeDisabledColor, mStrokeColor); - mStrokeFocusedColor = typedArray.getColor(R.styleable.ShapeRecyclerView_shape_strokeFocusedColor, mStrokeColor); - mStrokeSelectedColor = typedArray.getColor(R.styleable.ShapeRecyclerView_shape_strokeSelectedColor, mStrokeColor); - - mStrokeWidth = typedArray.getDimensionPixelSize(R.styleable.ShapeRecyclerView_shape_strokeWidth, DEFAULT_SHAPE_STROKE_WIDTH); - mDashWidth = typedArray.getDimensionPixelSize(R.styleable.ShapeRecyclerView_shape_dashWidth, DEFAULT_SHAPE_DASH_WIDTH); - mDashGap = typedArray.getDimensionPixelSize(R.styleable.ShapeRecyclerView_shape_dashGap, DEFAULT_SHAPE_DASH_GAP); - - mInnerRadius = typedArray.getDimensionPixelOffset(R.styleable.ShapeRecyclerView_shape_innerRadius, DEFAULT_SHAPE_INNER_RADIUS); - mInnerRadiusRatio = typedArray.getFloat(R.styleable.ShapeRecyclerView_shape_innerRadiusRatio, DEFAULT_SHAPE_INNER_RADIUS_RATIO); - mThickness = typedArray.getDimensionPixelOffset(R.styleable.ShapeRecyclerView_shape_thickness, DEFAULT_SHAPE_THICKNESS); - mThicknessRatio = typedArray.getFloat(R.styleable.ShapeRecyclerView_shape_thicknessRatio, DEFAULT_SHAPE_THICKNESS_RATIO); - - mShadowSize = typedArray.getDimensionPixelSize(R.styleable.ShapeRecyclerView_shape_shadowSize, DEFAULT_SHAPE_SHADOW_SIZE); - mShadowColor = typedArray.getColor(R.styleable.ShapeRecyclerView_shape_shadowColor, DEFAULT_SHAPE_SHADOW_COLOR); - mShadowOffsetX = typedArray.getDimensionPixelOffset(R.styleable.ShapeRecyclerView_shape_shadowOffsetX, DEFAULT_SHAPE_SHADOW_OFFSET_X); - mShadowOffsetY = typedArray.getDimensionPixelOffset(R.styleable.ShapeRecyclerView_shape_shadowOffsetY, DEFAULT_SHAPE_SHADOW_OFFSET_Y); - + mShapeDrawableBuilder = new ShapeDrawableBuilder(this, typedArray, STYLEABLE); typedArray.recycle(); - intoBackground(); - } - - /** - * {@link IShapeDrawable} - */ - - @Override - public ShapeRecyclerView setShapeType(int type) { - mShapeType = type; - return this; - } - - @Override - public int getShapeType() { - return mShapeType; - } - - @Override - public ShapeRecyclerView setShapeWidth(int width) { - mShapeWidth = width; - return this; - } - - @Override - public int getShapeWidth() { - return mShapeWidth; - } - - @Override - public ShapeRecyclerView setShapeHeight(int height) { - mShapeHeight = height; - return this; - } - - @Override - public int getShapeHeight() { - return mShapeHeight; - } - - @Override - public ShapeRecyclerView setSolidColor(int color) { - if (mSolidPressedColor == mSolidColor) { - mSolidPressedColor = color; - } - if (mSolidDisabledColor == mSolidColor) { - mSolidDisabledColor = color; - } - if (mSolidFocusedColor == mSolidColor) { - mSolidFocusedColor = color; - } - if (mSolidSelectedColor == mSolidColor) { - mSolidSelectedColor = color; - } - mSolidColor = color; - mStartColor = color; - mCenterColor = color; - mEndColor = color; - return this; - } - - @Override - public int getSolidColor() { - return mSolidColor; - } - - @Override - public ShapeRecyclerView setSolidPressedColor(int color) { - mSolidPressedColor = color; - return this; - } - - @Override - public int getSolidPressedColor() { - return mSolidPressedColor; - } - - @Override - public ShapeRecyclerView setSolidDisabledColor(int color) { - mSolidDisabledColor = color; - return this; - } - - @Override - public int getSolidDisabledColor() { - return mSolidDisabledColor; - } - - @Override - public ShapeRecyclerView setSolidFocusedColor(int color) { - mSolidFocusedColor = color; - return this; - } - - @Override - public int getSolidFocusedColor() { - return mSolidFocusedColor; - } - - @Override - public ShapeRecyclerView setSolidSelectedColor(int color) { - mSolidSelectedColor = color; - return this; - } - - @Override - public int getSolidSelectedColor() { - return mSolidSelectedColor; - } - - @Override - public ShapeRecyclerView setTopLeftRadius(int radius) { - mTopLeftRadius = radius; - return this; - } - - @Override - public int getTopLeftRadius() { - return mTopLeftRadius; - } - - @Override - public ShapeRecyclerView setTopRightRadius(int radius) { - mTopRightRadius = radius; - return this; - } - - @Override - public int getTopRightRadius() { - return mTopRightRadius; - } - - @Override - public ShapeRecyclerView setBottomLeftRadius(int radius) { - mBottomLeftRadius = radius; - return this; - } - - @Override - public int getBottomLeftRadius() { - return mBottomLeftRadius; - } - - @Override - public ShapeRecyclerView setBottomRightRadius(int radius) { - mBottomRightRadius = radius; - return this; - } - - @Override - public int getBottomRightRadius() { - return mBottomRightRadius; - } - - @Override - public ShapeRecyclerView setStartColor(int color) { - mStartColor = color; - return this; - } - - @Override - public int getStartColor() { - return mStartColor; - } - - @Override - public ShapeRecyclerView setCenterColor(int color) { - mCenterColor = color; - return this; - } - - @Override - public int getCenterColor() { - return mCenterColor; - } - - @Override - public ShapeRecyclerView setEndColor(int color) { - mEndColor = color; - return this; - } - - @Override - public int getEndColor() { - return mEndColor; - } - - @Override - public ShapeRecyclerView setUseLevel(boolean useLevel) { - mUseLevel = useLevel; - return this; - } - - @Override - public boolean isUseLevel() { - return mUseLevel; - } - - @Override - public ShapeRecyclerView setAngle(int angle) { - mAngle = angle; - return this; - } - - @Override - public int getAngle() { - return mAngle; - } - - @Override - public ShapeRecyclerView setGradientType(int type) { - mGradientType = type; - return this; - } - - @Override - public int getGradientType() { - return mGradientType; - } - - @Override - public ShapeRecyclerView setCenterX(float x) { - mCenterX = x; - return this; - } - - @Override - public float getCenterX() { - return mCenterX; - } - - @Override - public ShapeRecyclerView setCenterY(float y) { - mCenterY = y; - return this; - } - - @Override - public float getCenterY() { - return mCenterY; - } - - @Override - public ShapeRecyclerView setGradientRadius(int radius) { - mGradientRadius = radius; - return this; - } - - @Override - public int getGradientRadius() { - return mGradientRadius; - } - - @Override - public ShapeRecyclerView setStrokeColor(int color) { - if (mStrokePressedColor == mStrokeColor) { - mStrokePressedColor = color; - } - if (mStrokeDisabledColor == mStrokeColor) { - mStrokeDisabledColor = color; - } - if (mStrokeFocusedColor == mStrokeColor) { - mStrokeFocusedColor = color; - } - if (mStrokeSelectedColor == mStrokeColor) { - mStrokeSelectedColor = color; - } - mStrokeColor = color; - return this; - } - - @Override - public int getStrokeColor() { - return mStrokeColor; - } - - @Override - public ShapeRecyclerView setStrokePressedColor(int color) { - mStrokePressedColor = color; - return this; - } - - @Override - public int getStrokePressedColor() { - return mStrokePressedColor; - } - - @Override - public ShapeRecyclerView setStrokeDisabledColor(int color) { - mStrokeDisabledColor = color; - return this; - } - - @Override - public int getStrokeDisabledColor() { - return mStrokeDisabledColor; - } - - @Override - public ShapeRecyclerView setStrokeFocusedColor(int color) { - mStrokeFocusedColor = color; - return this; - } - - @Override - public int getStrokeFocusedColor() { - return mStrokeFocusedColor; - } - - @Override - public ShapeRecyclerView setStrokeSelectedColor(int color) { - mStrokeSelectedColor = color; - return this; - } - - @Override - public int getStrokeSelectedColor() { - return mStrokeSelectedColor; - } - - @Override - public ShapeRecyclerView setStrokeWidth(int width) { - mStrokeWidth = width; - return this; - } - - @Override - public int getStrokeWidth() { - return mStrokeWidth; - } - - @Override - public ShapeRecyclerView setDashWidth(int width) { - mDashWidth = width; - return this; - } - - @Override - public int getDashWidth() { - return mDashWidth; - } - - @Override - public ShapeRecyclerView setDashGap(int gap) { - mDashGap = gap; - return this; - } - - @Override - public int getDashGap() { - return mDashGap; - } - - @Override - public ShapeRecyclerView setInnerRadius(int radius) { - mInnerRadius = radius; - return this; - } - - @Override - public int getInnerRadius() { - return mInnerRadius; - } - - @Override - public ShapeRecyclerView setInnerRadiusRatio(float ratio) { - mInnerRadiusRatio = ratio; - return this; - } - - @Override - public float getInnerRadiusRatio() { - return mInnerRadiusRatio; - } - - @Override - public ShapeRecyclerView setThickness(int size) { - mThickness = size; - return this; - } - - @Override - public int getThickness() { - return mThickness; - } - - @Override - public ShapeRecyclerView setThicknessRatio(float ratio) { - mThicknessRatio = ratio; - return this; - } - - @Override - public float getThicknessRatio() { - return mThicknessRatio; - } - - @Override - public ShapeRecyclerView setShadowSize(int size) { - mShadowSize = size; - return this; - } - - @Override - public int getShadowSize() { - return mShadowSize; - } - - @Override - public ShapeRecyclerView setShadowColor(int color) { - mShadowColor = color; - return this; - } - - @Override - public int getShadowColor() { - return mShadowColor; - } - - @Override - public ShapeRecyclerView setShadowOffsetX(int offsetX) { - mShadowOffsetX = offsetX; - return this; - } - - @Override - public int getShadowOffsetX() { - return mShadowOffsetX; - } - - @Override - public ShapeRecyclerView setShadowOffsetY(int offsetY) { - mShadowOffsetY = offsetY; - return this; - } - - @Override - public int getShadowOffsetY() { - return mShadowOffsetY; + mShapeDrawableBuilder.intoBackground(); } - @Override - public void intoBackground() { - Drawable drawable = buildBackgroundDrawable(); - if (drawable == null) { - return; - } - if (isDashLineEnable() || isShadowEnable()) { - // 需要关闭硬件加速,否则虚线或者阴影在某些手机上面无法生效 - setLayerType(View.LAYER_TYPE_SOFTWARE, null); - } - setBackground(drawable); + public ShapeDrawableBuilder getShapeDrawableBuilder() { + return mShapeDrawableBuilder; } } \ No newline at end of file diff --git a/library/src/main/java/com/hjq/shape/layout/ShapeRelativeLayout.java b/library/src/main/java/com/hjq/shape/layout/ShapeRelativeLayout.java index 8b36a0b..dcf65bc 100644 --- a/library/src/main/java/com/hjq/shape/layout/ShapeRelativeLayout.java +++ b/library/src/main/java/com/hjq/shape/layout/ShapeRelativeLayout.java @@ -2,13 +2,12 @@ import android.content.Context; import android.content.res.TypedArray; -import android.graphics.drawable.Drawable; import android.util.AttributeSet; -import android.view.View; import android.widget.RelativeLayout; import com.hjq.shape.R; -import com.hjq.shape.core.IShapeDrawable; +import com.hjq.shape.builder.ShapeDrawableBuilder; +import com.hjq.shape.styleable.ShapeRelativeLayoutStyleable; /** * author : Android 轮子哥 @@ -16,52 +15,11 @@ * time : 2021/07/17 * desc : 支持直接定义 Shape 背景的 RelativeLayout */ -public class ShapeRelativeLayout extends RelativeLayout implements IShapeDrawable { +public class ShapeRelativeLayout extends RelativeLayout { - private int mShapeType; - private int mShapeWidth; - private int mShapeHeight; + private static final ShapeRelativeLayoutStyleable STYLEABLE = new ShapeRelativeLayoutStyleable(); - private int mSolidColor; - private int mSolidPressedColor; - private int mSolidDisabledColor; - private int mSolidFocusedColor; - private int mSolidSelectedColor; - - private int mTopLeftRadius; - private int mTopRightRadius; - private int mBottomLeftRadius; - private int mBottomRightRadius; - - private int mStartColor; - private int mCenterColor; - private int mEndColor; - private boolean mUseLevel; - private int mAngle; - private int mGradientType; - private float mCenterX; - private float mCenterY; - private int mGradientRadius; - - private int mStrokeColor; - private int mStrokePressedColor; - private int mStrokeDisabledColor; - private int mStrokeFocusedColor; - private int mStrokeSelectedColor; - - private int mStrokeWidth; - private int mDashWidth; - private int mDashGap; - - private int mInnerRadius; - private float mInnerRadiusRatio; - private int mThickness; - private float mThicknessRatio; - - private int mShadowSize; - private int mShadowColor; - private int mShadowOffsetX; - private int mShadowOffsetY; + private final ShapeDrawableBuilder mShapeDrawableBuilder; public ShapeRelativeLayout(Context context) { this(context, null); @@ -75,505 +33,13 @@ public ShapeRelativeLayout(Context context, AttributeSet attrs, int defStyleAttr super(context, attrs, defStyleAttr); TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.ShapeRelativeLayout); - mShapeType = typedArray.getInt(R.styleable.ShapeRelativeLayout_shape, DEFAULT_SHAPE_TYPE); - mShapeWidth = typedArray.getDimensionPixelSize(R.styleable.ShapeRelativeLayout_shape_width, DEFAULT_SHAPE_WIDTH); - mShapeHeight = typedArray.getDimensionPixelSize(R.styleable.ShapeRelativeLayout_shape_height, DEFAULT_SHAPE_HEIGHT); - - mSolidColor = typedArray.getColor(R.styleable.ShapeRelativeLayout_shape_solidColor, DEFAULT_SHAPE_SOLID_COLOR); - mSolidPressedColor = typedArray.getColor(R.styleable.ShapeRelativeLayout_shape_solidPressedColor, mSolidColor); - mSolidDisabledColor = typedArray.getColor(R.styleable.ShapeRelativeLayout_shape_solidDisabledColor, mSolidColor); - mSolidFocusedColor = typedArray.getColor(R.styleable.ShapeRelativeLayout_shape_solidFocusedColor, mSolidColor); - mSolidSelectedColor = typedArray.getColor(R.styleable.ShapeRelativeLayout_shape_solidSelectedColor, mSolidColor); - - int radius = typedArray.getDimensionPixelSize(R.styleable.ShapeRelativeLayout_shape_radius, DEFAULT_SHAPE_RADIUS); - mTopLeftRadius = typedArray.getDimensionPixelSize(R.styleable.ShapeRelativeLayout_shape_topLeftRadius, radius); - mTopRightRadius = typedArray.getDimensionPixelSize(R.styleable.ShapeRelativeLayout_shape_topRightRadius, radius); - mBottomLeftRadius = typedArray.getDimensionPixelSize(R.styleable.ShapeRelativeLayout_shape_bottomLeftRadius, radius); - mBottomRightRadius = typedArray.getDimensionPixelSize(R.styleable.ShapeRelativeLayout_shape_bottomRightRadius, radius); - - mStartColor = typedArray.getColor(R.styleable.ShapeRelativeLayout_shape_startColor, mSolidColor); - mCenterColor = typedArray.getColor(R.styleable.ShapeRelativeLayout_shape_centerColor, mSolidColor); - mEndColor = typedArray.getColor(R.styleable.ShapeRelativeLayout_shape_endColor, mSolidColor); - mUseLevel = typedArray.getBoolean(R.styleable.ShapeRelativeLayout_shape_useLevel, DEFAULT_SHAPE_USE_LEVEL); - mAngle = (int) typedArray.getFloat(R.styleable.ShapeRelativeLayout_shape_angle, DEFAULT_SHAPE_ANGLE); - mGradientType = typedArray.getInt(R.styleable.ShapeRelativeLayout_shape_gradientType, DEFAULT_SHAPE_GRADIENT_TYPE); - mCenterX = typedArray.getFloat(R.styleable.ShapeRelativeLayout_shape_centerX, DEFAULT_SHAPE_CENTER_X); - mCenterY = typedArray.getFloat(R.styleable.ShapeRelativeLayout_shape_centerY, DEFAULT_SHAPE_CENTER_Y); - mGradientRadius = typedArray.getDimensionPixelSize(R.styleable.ShapeRelativeLayout_shape_gradientRadius, radius); - - mStrokeColor = typedArray.getColor(R.styleable.ShapeRelativeLayout_shape_strokeColor, DEFAULT_SHAPE_STROKE_COLOR); - mStrokePressedColor = typedArray.getColor(R.styleable.ShapeRelativeLayout_shape_strokePressedColor, mStrokeColor); - mStrokeDisabledColor = typedArray.getColor(R.styleable.ShapeRelativeLayout_shape_strokeDisabledColor, mStrokeColor); - mStrokeFocusedColor = typedArray.getColor(R.styleable.ShapeRelativeLayout_shape_strokeFocusedColor, mStrokeColor); - mStrokeSelectedColor = typedArray.getColor(R.styleable.ShapeRelativeLayout_shape_strokeSelectedColor, mStrokeColor); - - mStrokeWidth = typedArray.getDimensionPixelSize(R.styleable.ShapeRelativeLayout_shape_strokeWidth, DEFAULT_SHAPE_STROKE_WIDTH); - mDashWidth = typedArray.getDimensionPixelSize(R.styleable.ShapeRelativeLayout_shape_dashWidth, DEFAULT_SHAPE_DASH_WIDTH); - mDashGap = typedArray.getDimensionPixelSize(R.styleable.ShapeRelativeLayout_shape_dashGap, DEFAULT_SHAPE_DASH_GAP); - - mInnerRadius = typedArray.getDimensionPixelOffset(R.styleable.ShapeRelativeLayout_shape_innerRadius, DEFAULT_SHAPE_INNER_RADIUS); - mInnerRadiusRatio = typedArray.getFloat(R.styleable.ShapeRelativeLayout_shape_innerRadiusRatio, DEFAULT_SHAPE_INNER_RADIUS_RATIO); - mThickness = typedArray.getDimensionPixelOffset(R.styleable.ShapeRelativeLayout_shape_thickness, DEFAULT_SHAPE_THICKNESS); - mThicknessRatio = typedArray.getFloat(R.styleable.ShapeRelativeLayout_shape_thicknessRatio, DEFAULT_SHAPE_THICKNESS_RATIO); - - mShadowSize = typedArray.getDimensionPixelSize(R.styleable.ShapeRelativeLayout_shape_shadowSize, DEFAULT_SHAPE_SHADOW_SIZE); - mShadowColor = typedArray.getColor(R.styleable.ShapeRelativeLayout_shape_shadowColor, DEFAULT_SHAPE_SHADOW_COLOR); - mShadowOffsetX = typedArray.getDimensionPixelOffset(R.styleable.ShapeRelativeLayout_shape_shadowOffsetX, DEFAULT_SHAPE_SHADOW_OFFSET_X); - mShadowOffsetY = typedArray.getDimensionPixelOffset(R.styleable.ShapeRelativeLayout_shape_shadowOffsetY, DEFAULT_SHAPE_SHADOW_OFFSET_Y); - + mShapeDrawableBuilder = new ShapeDrawableBuilder(this, typedArray, STYLEABLE); typedArray.recycle(); - intoBackground(); - } - - /** - * {@link IShapeDrawable} - */ - - @Override - public ShapeRelativeLayout setShapeType(int type) { - mShapeType = type; - return this; - } - - @Override - public int getShapeType() { - return mShapeType; - } - - @Override - public ShapeRelativeLayout setShapeWidth(int width) { - mShapeWidth = width; - return this; - } - - @Override - public int getShapeWidth() { - return mShapeWidth; - } - - @Override - public ShapeRelativeLayout setShapeHeight(int height) { - mShapeHeight = height; - return this; - } - - @Override - public int getShapeHeight() { - return mShapeHeight; - } - - @Override - public ShapeRelativeLayout setSolidColor(int color) { - if (mSolidPressedColor == mSolidColor) { - mSolidPressedColor = color; - } - if (mSolidDisabledColor == mSolidColor) { - mSolidDisabledColor = color; - } - if (mSolidFocusedColor == mSolidColor) { - mSolidFocusedColor = color; - } - if (mSolidSelectedColor == mSolidColor) { - mSolidSelectedColor = color; - } - mSolidColor = color; - mStartColor = color; - mCenterColor = color; - mEndColor = color; - return this; - } - - @Override - public int getSolidColor() { - return mSolidColor; - } - - @Override - public ShapeRelativeLayout setSolidPressedColor(int color) { - mSolidPressedColor = color; - return this; - } - - @Override - public int getSolidPressedColor() { - return mSolidPressedColor; - } - - @Override - public ShapeRelativeLayout setSolidDisabledColor(int color) { - mSolidDisabledColor = color; - return this; - } - - @Override - public int getSolidDisabledColor() { - return mSolidDisabledColor; - } - - @Override - public ShapeRelativeLayout setSolidFocusedColor(int color) { - mSolidFocusedColor = color; - return this; - } - - @Override - public int getSolidFocusedColor() { - return mSolidFocusedColor; - } - - @Override - public ShapeRelativeLayout setSolidSelectedColor(int color) { - mSolidSelectedColor = color; - return this; - } - - @Override - public int getSolidSelectedColor() { - return mSolidSelectedColor; - } - - @Override - public ShapeRelativeLayout setTopLeftRadius(int radius) { - mTopLeftRadius = radius; - return this; - } - - @Override - public int getTopLeftRadius() { - return mTopLeftRadius; - } - - @Override - public ShapeRelativeLayout setTopRightRadius(int radius) { - mTopRightRadius = radius; - return this; - } - - @Override - public int getTopRightRadius() { - return mTopRightRadius; - } - - @Override - public ShapeRelativeLayout setBottomLeftRadius(int radius) { - mBottomLeftRadius = radius; - return this; - } - - @Override - public int getBottomLeftRadius() { - return mBottomLeftRadius; - } - - @Override - public ShapeRelativeLayout setBottomRightRadius(int radius) { - mBottomRightRadius = radius; - return this; - } - - @Override - public int getBottomRightRadius() { - return mBottomRightRadius; - } - - @Override - public ShapeRelativeLayout setStartColor(int color) { - mStartColor = color; - return this; - } - - @Override - public int getStartColor() { - return mStartColor; - } - - @Override - public ShapeRelativeLayout setCenterColor(int color) { - mCenterColor = color; - return this; - } - - @Override - public int getCenterColor() { - return mCenterColor; - } - - @Override - public ShapeRelativeLayout setEndColor(int color) { - mEndColor = color; - return this; - } - - @Override - public int getEndColor() { - return mEndColor; - } - - @Override - public ShapeRelativeLayout setUseLevel(boolean useLevel) { - mUseLevel = useLevel; - return this; - } - - @Override - public boolean isUseLevel() { - return mUseLevel; - } - - @Override - public ShapeRelativeLayout setAngle(int angle) { - mAngle = angle; - return this; - } - - @Override - public int getAngle() { - return mAngle; - } - - @Override - public ShapeRelativeLayout setGradientType(int type) { - mGradientType = type; - return this; - } - - @Override - public int getGradientType() { - return mGradientType; - } - - @Override - public ShapeRelativeLayout setCenterX(float x) { - mCenterX = x; - return this; - } - - @Override - public float getCenterX() { - return mCenterX; - } - - @Override - public ShapeRelativeLayout setCenterY(float y) { - mCenterY = y; - return this; - } - - @Override - public float getCenterY() { - return mCenterY; - } - - @Override - public ShapeRelativeLayout setGradientRadius(int radius) { - mGradientRadius = radius; - return this; - } - - @Override - public int getGradientRadius() { - return mGradientRadius; - } - - @Override - public ShapeRelativeLayout setStrokeColor(int color) { - if (mStrokePressedColor == mStrokeColor) { - mStrokePressedColor = color; - } - if (mStrokeDisabledColor == mStrokeColor) { - mStrokeDisabledColor = color; - } - if (mStrokeFocusedColor == mStrokeColor) { - mStrokeFocusedColor = color; - } - if (mStrokeSelectedColor == mStrokeColor) { - mStrokeSelectedColor = color; - } - mStrokeColor = color; - return this; - } - - @Override - public int getStrokeColor() { - return mStrokeColor; - } - - @Override - public ShapeRelativeLayout setStrokePressedColor(int color) { - mStrokePressedColor = color; - return this; - } - - @Override - public int getStrokePressedColor() { - return mStrokePressedColor; - } - - @Override - public ShapeRelativeLayout setStrokeDisabledColor(int color) { - mStrokeDisabledColor = color; - return this; - } - - @Override - public int getStrokeDisabledColor() { - return mStrokeDisabledColor; - } - - @Override - public ShapeRelativeLayout setStrokeFocusedColor(int color) { - mStrokeFocusedColor = color; - return this; - } - - @Override - public int getStrokeFocusedColor() { - return mStrokeFocusedColor; - } - - @Override - public ShapeRelativeLayout setStrokeSelectedColor(int color) { - mStrokeSelectedColor = color; - return this; - } - - @Override - public int getStrokeSelectedColor() { - return mStrokeSelectedColor; - } - - @Override - public ShapeRelativeLayout setStrokeWidth(int width) { - mStrokeWidth = width; - return this; - } - - @Override - public int getStrokeWidth() { - return mStrokeWidth; - } - - @Override - public ShapeRelativeLayout setDashWidth(int width) { - mDashWidth = width; - return this; - } - - @Override - public int getDashWidth() { - return mDashWidth; - } - - @Override - public ShapeRelativeLayout setDashGap(int gap) { - mDashGap = gap; - return this; - } - - @Override - public int getDashGap() { - return mDashGap; - } - - @Override - public ShapeRelativeLayout setInnerRadius(int radius) { - mInnerRadius = radius; - return this; - } - - @Override - public int getInnerRadius() { - return mInnerRadius; - } - - @Override - public ShapeRelativeLayout setInnerRadiusRatio(float ratio) { - mInnerRadiusRatio = ratio; - return this; - } - - @Override - public float getInnerRadiusRatio() { - return mInnerRadiusRatio; - } - - @Override - public ShapeRelativeLayout setThickness(int size) { - mThickness = size; - return this; - } - - @Override - public int getThickness() { - return mThickness; - } - - @Override - public ShapeRelativeLayout setThicknessRatio(float ratio) { - mThicknessRatio = ratio; - return this; - } - - @Override - public float getThicknessRatio() { - return mThicknessRatio; - } - - @Override - public ShapeRelativeLayout setShadowSize(int size) { - mShadowSize = size; - return this; - } - - @Override - public int getShadowSize() { - return mShadowSize; - } - - @Override - public ShapeRelativeLayout setShadowColor(int color) { - mShadowColor = color; - return this; - } - - @Override - public int getShadowColor() { - return mShadowColor; - } - - @Override - public ShapeRelativeLayout setShadowOffsetX(int offsetX) { - mShadowOffsetX = offsetX; - return this; - } - - @Override - public int getShadowOffsetX() { - return mShadowOffsetX; - } - - @Override - public ShapeRelativeLayout setShadowOffsetY(int offsetY) { - mShadowOffsetY = offsetY; - return this; - } - - @Override - public int getShadowOffsetY() { - return mShadowOffsetY; + mShapeDrawableBuilder.intoBackground(); } - @Override - public void intoBackground() { - Drawable drawable = buildBackgroundDrawable(); - if (drawable == null) { - return; - } - if (isDashLineEnable() || isShadowEnable()) { - // 需要关闭硬件加速,否则虚线或者阴影在某些手机上面无法生效 - setLayerType(View.LAYER_TYPE_SOFTWARE, null); - } - setBackground(drawable); + public ShapeDrawableBuilder getShapeDrawableBuilder() { + return mShapeDrawableBuilder; } } \ No newline at end of file diff --git a/library/src/main/java/com/hjq/shape/core/LinearGradientFontSpan.java b/library/src/main/java/com/hjq/shape/other/LinearGradientFontSpan.java similarity index 69% rename from library/src/main/java/com/hjq/shape/core/LinearGradientFontSpan.java rename to library/src/main/java/com/hjq/shape/other/LinearGradientFontSpan.java index 54512fb..b8d4522 100644 --- a/library/src/main/java/com/hjq/shape/core/LinearGradientFontSpan.java +++ b/library/src/main/java/com/hjq/shape/other/LinearGradientFontSpan.java @@ -1,10 +1,13 @@ -package com.hjq.shape.core; +package com.hjq.shape.other; import android.graphics.Canvas; import android.graphics.LinearGradient; import android.graphics.Paint; import android.graphics.Shader; +import android.text.SpannableStringBuilder; +import android.text.Spanned; import android.text.style.ReplacementSpan; +import android.widget.LinearLayout; /** * author : Android 轮子哥 @@ -14,21 +17,37 @@ */ public class LinearGradientFontSpan extends ReplacementSpan { + /** 水平渐变方向 */ + public static final int GRADIENT_ORIENTATION_HORIZONTAL = LinearLayout.HORIZONTAL; + /** 垂直渐变方向 */ + public static final int GRADIENT_ORIENTATION_VERTICAL = LinearLayout.VERTICAL; + + /** + * 构建一个文字渐变色的 Spannable 对象 + */ + public static SpannableStringBuilder buildLinearGradientSpannable(CharSequence text, int[] colors, float[] positions, int orientation) { + SpannableStringBuilder builder = new SpannableStringBuilder(text); + LinearGradientFontSpan span = new LinearGradientFontSpan(); + span.setTextGradientColor(colors); + span.setTextGradientOrientation(orientation); + span.setTextGradientPositions(positions); + builder.setSpan(span, 0, builder.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); + return builder; + } + /** 测量的文本宽度 */ - private int mMeasureTextWidth; + private float mMeasureTextWidth; /** 文字渐变方向 */ private int mTextGradientOrientation; - /** 文字渐变颜色组 */ private int[] mTextGradientColors; - /** 文字渐变位置组 */ private float[] mTextGradientPositions; @Override public int getSize(Paint paint, CharSequence text, int start, int end, Paint.FontMetricsInt fontMetricsInt) { - mMeasureTextWidth = (int) (paint.measureText(text, start, end)); + mMeasureTextWidth = paint.measureText(text, start, end); // 这段不可以去掉,字体高度没设置,会出现 draw 方法没有被调用的问题 // 详情请见:https://stackoverflow.com/questions/20069537/replacementspans-draw-method-isnt-called @@ -39,13 +58,13 @@ public int getSize(Paint paint, CharSequence text, int start, int end, Paint.Fon fontMetricsInt.descent = metrics.descent; fontMetricsInt.bottom = metrics.bottom; } - return mMeasureTextWidth; + return (int) mMeasureTextWidth; } @Override public void draw(Canvas canvas, CharSequence text, int start, int end, float x, int top, int y, int bottom, Paint paint) { LinearGradient linearGradient; - if (mTextGradientOrientation == IShapeTextColor.SHAPE_TEXT_GRADIENT_TYPE_VERTICAL) { + if (mTextGradientOrientation == GRADIENT_ORIENTATION_VERTICAL) { linearGradient = new LinearGradient(0, 0, 0, paint.descent() - paint.ascent(), mTextGradientColors, mTextGradientPositions, Shader.TileMode.REPEAT); } else { diff --git a/library/src/main/java/com/hjq/shape/styleable/ICompoundButtonStyleable.java b/library/src/main/java/com/hjq/shape/styleable/ICompoundButtonStyleable.java new file mode 100644 index 0000000..dc6f2e3 --- /dev/null +++ b/library/src/main/java/com/hjq/shape/styleable/ICompoundButtonStyleable.java @@ -0,0 +1,22 @@ +package com.hjq.shape.styleable; + +/** + * author : Android 轮子哥 + * github : https://github.com/getActivity/ShapeView + * time : 2021/08/28 + * desc : CompoundButton View 属性收集接口 + */ +public interface ICompoundButtonStyleable { + + int getButtonDrawableStyleable(); + + int getButtonPressedDrawableStyleable(); + + int getButtonCheckedDrawableStyleable(); + + int getButtonDisabledDrawableStyleable(); + + int getButtonFocusedDrawableStyleable(); + + int getButtonSelectedDrawableStyleable(); +} \ No newline at end of file diff --git a/library/src/main/java/com/hjq/shape/styleable/IShapeDrawableStyleable.java b/library/src/main/java/com/hjq/shape/styleable/IShapeDrawableStyleable.java new file mode 100644 index 0000000..34b54a3 --- /dev/null +++ b/library/src/main/java/com/hjq/shape/styleable/IShapeDrawableStyleable.java @@ -0,0 +1,94 @@ +package com.hjq.shape.styleable; + +/** + * author : Android 轮子哥 + * github : https://github.com/getActivity/ShapeView + * time : 2021/08/28 + * desc : ShapeDrawable View 属性收集接口 + */ +public interface IShapeDrawableStyleable { + + int getShapeTypeStyleable(); + + int getShapeWidthStyleable(); + + int getShapeHeightStyleable(); + + int getSolidColorStyleable(); + + int getSolidPressedColorStyleable(); + + default int getSolidCheckedColorStyleable() { + return 0; + } + + int getSolidDisabledColorStyleable(); + + int getSolidFocusedColorStyleable(); + + int getSolidSelectedColorStyleable(); + + int getRadiusStyleable(); + + int getTopLeftRadiusStyleable(); + + int getTopRightRadiusStyleable(); + + int getBottomLeftRadiusStyleable(); + + int getBottomRightRadiusStyleable(); + + int getStartColorStyleable(); + + int getCenterColorStyleable(); + + int getEndColorStyleable(); + + int getUseLevelStyleable(); + + int getAngleStyleable(); + + int getGradientTypeStyleable(); + + int getCenterXStyleable(); + + int getCenterYStyleable(); + + int getGradientRadiusStyleable(); + + int getStrokeColorStyleable(); + + int getStrokePressedColorStyleable(); + + default int getStrokeCheckedColorStyleable() { + return 0; + } + + int getStrokeDisabledColorStyleable(); + + int getStrokeFocusedColorStyleable(); + + int getStrokeSelectedColorStyleable(); + + int getStrokeWidthStyleable(); + + int getDashWidthStyleable(); + + int getDashGapStyleable(); + + int getInnerRadiusStyleable(); + + int getInnerRadiusRatioStyleable(); + + int getThicknessStyleable(); + + int getThicknessRatioStyleable(); + + int getShadowSizeStyleable(); + + int getShadowColorStyleable(); + + int getShadowOffsetXStyleable(); + + int getShadowOffsetYStyleable(); +} \ No newline at end of file diff --git a/library/src/main/java/com/hjq/shape/styleable/ITextColorStyleable.java b/library/src/main/java/com/hjq/shape/styleable/ITextColorStyleable.java new file mode 100644 index 0000000..ace0b0e --- /dev/null +++ b/library/src/main/java/com/hjq/shape/styleable/ITextColorStyleable.java @@ -0,0 +1,32 @@ +package com.hjq.shape.styleable; + +/** + * author : Android 轮子哥 + * github : https://github.com/getActivity/ShapeView + * time : 2021/08/28 + * desc : 文本颜色 View 属性收集接口 + */ +public interface ITextColorStyleable { + + int getTextColorStyleable(); + + int getTextPressedColorStyleable(); + + default int getTextCheckedColorStyleable() { + return 0; + } + + int getTextDisabledColorStyleable(); + + int getTextFocusedColorStyleable(); + + int getTextSelectedColorStyleable(); + + int getTextStartColorStyleable(); + + int getTextCenterColorStyleable(); + + int getTextEndColorStyleable(); + + int getTextGradientOrientationStyleable(); +} \ No newline at end of file diff --git a/library/src/main/java/com/hjq/shape/styleable/ShapeButtonStyleable.java b/library/src/main/java/com/hjq/shape/styleable/ShapeButtonStyleable.java new file mode 100644 index 0000000..5a72255 --- /dev/null +++ b/library/src/main/java/com/hjq/shape/styleable/ShapeButtonStyleable.java @@ -0,0 +1,255 @@ +package com.hjq.shape.styleable; + +import com.hjq.shape.R; + +/** + * author : Android 轮子哥 + * github : https://github.com/getActivity/ShapeView + * time : 2021/08/28 + * desc : Button 的 Shape 属性值 + */ +public final class ShapeButtonStyleable implements IShapeDrawableStyleable, ITextColorStyleable { + + /** + * {@link IShapeDrawableStyleable} + */ + + @Override + public int getShapeTypeStyleable() { + return R.styleable.ShapeButton_shape; + } + + @Override + public int getShapeWidthStyleable() { + return R.styleable.ShapeButton_shape_width; + } + + @Override + public int getShapeHeightStyleable() { + return R.styleable.ShapeButton_shape_height; + } + + @Override + public int getSolidColorStyleable() { + return R.styleable.ShapeButton_shape_solidColor; + } + + @Override + public int getSolidPressedColorStyleable() { + return R.styleable.ShapeButton_shape_solidPressedColor; + } + + @Override + public int getSolidDisabledColorStyleable() { + return R.styleable.ShapeButton_shape_solidDisabledColor; + } + + @Override + public int getSolidFocusedColorStyleable() { + return R.styleable.ShapeButton_shape_solidFocusedColor; + } + + @Override + public int getSolidSelectedColorStyleable() { + return R.styleable.ShapeButton_shape_solidSelectedColor; + } + + @Override + public int getRadiusStyleable() { + return R.styleable.ShapeButton_shape_radius; + } + + @Override + public int getTopLeftRadiusStyleable() { + return R.styleable.ShapeButton_shape_topLeftRadius; + } + + @Override + public int getTopRightRadiusStyleable() { + return R.styleable.ShapeButton_shape_topRightRadius; + } + + @Override + public int getBottomLeftRadiusStyleable() { + return R.styleable.ShapeButton_shape_bottomLeftRadius; + } + + @Override + public int getBottomRightRadiusStyleable() { + return R.styleable.ShapeButton_shape_bottomRightRadius; + } + + @Override + public int getStartColorStyleable() { + return R.styleable.ShapeButton_shape_startColor; + } + + @Override + public int getCenterColorStyleable() { + return R.styleable.ShapeButton_shape_centerColor; + } + + @Override + public int getEndColorStyleable() { + return R.styleable.ShapeButton_shape_endColor; + } + + @Override + public int getUseLevelStyleable() { + return R.styleable.ShapeButton_shape_useLevel; + } + + @Override + public int getAngleStyleable() { + return R.styleable.ShapeButton_shape_angle; + } + + @Override + public int getGradientTypeStyleable() { + return R.styleable.ShapeButton_shape_gradientType; + } + + @Override + public int getCenterXStyleable() { + return R.styleable.ShapeButton_shape_centerX; + } + + @Override + public int getCenterYStyleable() { + return R.styleable.ShapeButton_shape_centerY; + } + + @Override + public int getGradientRadiusStyleable() { + return R.styleable.ShapeButton_shape_gradientRadius; + } + + @Override + public int getStrokeColorStyleable() { + return R.styleable.ShapeButton_shape_strokeColor; + } + + @Override + public int getStrokePressedColorStyleable() { + return R.styleable.ShapeButton_shape_strokePressedColor; + } + + @Override + public int getStrokeDisabledColorStyleable() { + return R.styleable.ShapeButton_shape_strokeDisabledColor; + } + + @Override + public int getStrokeFocusedColorStyleable() { + return R.styleable.ShapeButton_shape_strokeFocusedColor; + } + + @Override + public int getStrokeSelectedColorStyleable() { + return R.styleable.ShapeButton_shape_strokeSelectedColor; + } + + @Override + public int getStrokeWidthStyleable() { + return R.styleable.ShapeButton_shape_strokeWidth; + } + + @Override + public int getDashWidthStyleable() { + return R.styleable.ShapeButton_shape_dashWidth; + } + + @Override + public int getDashGapStyleable() { + return R.styleable.ShapeButton_shape_dashGap; + } + + @Override + public int getInnerRadiusStyleable() { + return R.styleable.ShapeButton_shape_innerRadius; + } + + @Override + public int getInnerRadiusRatioStyleable() { + return R.styleable.ShapeButton_shape_innerRadiusRatio; + } + + @Override + public int getThicknessStyleable() { + return R.styleable.ShapeButton_shape_thickness; + } + + @Override + public int getThicknessRatioStyleable() { + return R.styleable.ShapeButton_shape_thicknessRatio; + } + + @Override + public int getShadowSizeStyleable() { + return R.styleable.ShapeButton_shape_shadowSize; + } + + @Override + public int getShadowColorStyleable() { + return R.styleable.ShapeButton_shape_shadowColor; + } + + @Override + public int getShadowOffsetXStyleable() { + return R.styleable.ShapeButton_shape_shadowOffsetX; + } + + @Override + public int getShadowOffsetYStyleable() { + return R.styleable.ShapeButton_shape_shadowOffsetY; + } + + /** + * {@link ITextColorStyleable} + */ + + @Override + public int getTextColorStyleable() { + return R.styleable.ShapeButton_shape_textColor; + } + + @Override + public int getTextPressedColorStyleable() { + return R.styleable.ShapeButton_shape_textPressedColor; + } + + @Override + public int getTextDisabledColorStyleable() { + return R.styleable.ShapeButton_shape_textDisabledColor; + } + + @Override + public int getTextFocusedColorStyleable() { + return R.styleable.ShapeButton_shape_textFocusedColor; + } + + @Override + public int getTextSelectedColorStyleable() { + return R.styleable.ShapeButton_shape_textSelectedColor; + } + + @Override + public int getTextStartColorStyleable() { + return R.styleable.ShapeButton_shape_textStartColor; + } + + @Override + public int getTextCenterColorStyleable() { + return R.styleable.ShapeButton_shape_textCenterColor; + } + + @Override + public int getTextEndColorStyleable() { + return R.styleable.ShapeButton_shape_textEndColor; + } + + @Override + public int getTextGradientOrientationStyleable() { + return R.styleable.ShapeButton_shape_textGradientOrientation; + } +} \ No newline at end of file diff --git a/library/src/main/java/com/hjq/shape/styleable/ShapeCheckBoxStyleable.java b/library/src/main/java/com/hjq/shape/styleable/ShapeCheckBoxStyleable.java new file mode 100644 index 0000000..e47855d --- /dev/null +++ b/library/src/main/java/com/hjq/shape/styleable/ShapeCheckBoxStyleable.java @@ -0,0 +1,305 @@ +package com.hjq.shape.styleable; + +import com.hjq.shape.R; + +/** + * author : Android 轮子哥 + * github : https://github.com/getActivity/ShapeView + * time : 2021/08/28 + * desc : CheckBox 的 Shape 属性值 + */ +public final class ShapeCheckBoxStyleable implements IShapeDrawableStyleable, + ITextColorStyleable, ICompoundButtonStyleable { + + /** + * {@link IShapeDrawableStyleable} + */ + + @Override + public int getShapeTypeStyleable() { + return R.styleable.ShapeCheckBox_shape; + } + + @Override + public int getShapeWidthStyleable() { + return R.styleable.ShapeCheckBox_shape_width; + } + + @Override + public int getShapeHeightStyleable() { + return R.styleable.ShapeCheckBox_shape_height; + } + + @Override + public int getSolidColorStyleable() { + return R.styleable.ShapeCheckBox_shape_solidColor; + } + + @Override + public int getSolidPressedColorStyleable() { + return R.styleable.ShapeCheckBox_shape_solidPressedColor; + } + + @Override + public int getSolidCheckedColorStyleable() { + return R.styleable.ShapeCheckBox_shape_solidCheckedColor; + } + + @Override + public int getSolidDisabledColorStyleable() { + return R.styleable.ShapeCheckBox_shape_solidDisabledColor; + } + + @Override + public int getSolidFocusedColorStyleable() { + return R.styleable.ShapeCheckBox_shape_solidFocusedColor; + } + + @Override + public int getSolidSelectedColorStyleable() { + return R.styleable.ShapeCheckBox_shape_solidSelectedColor; + } + + @Override + public int getRadiusStyleable() { + return R.styleable.ShapeCheckBox_shape_radius; + } + + @Override + public int getTopLeftRadiusStyleable() { + return R.styleable.ShapeCheckBox_shape_topLeftRadius; + } + + @Override + public int getTopRightRadiusStyleable() { + return R.styleable.ShapeCheckBox_shape_topRightRadius; + } + + @Override + public int getBottomLeftRadiusStyleable() { + return R.styleable.ShapeCheckBox_shape_bottomLeftRadius; + } + + @Override + public int getBottomRightRadiusStyleable() { + return R.styleable.ShapeCheckBox_shape_bottomRightRadius; + } + + @Override + public int getStartColorStyleable() { + return R.styleable.ShapeCheckBox_shape_startColor; + } + + @Override + public int getCenterColorStyleable() { + return R.styleable.ShapeCheckBox_shape_centerColor; + } + + @Override + public int getEndColorStyleable() { + return R.styleable.ShapeCheckBox_shape_endColor; + } + + @Override + public int getUseLevelStyleable() { + return R.styleable.ShapeCheckBox_shape_useLevel; + } + + @Override + public int getAngleStyleable() { + return R.styleable.ShapeCheckBox_shape_angle; + } + + @Override + public int getGradientTypeStyleable() { + return R.styleable.ShapeCheckBox_shape_gradientType; + } + + @Override + public int getCenterXStyleable() { + return R.styleable.ShapeCheckBox_shape_centerX; + } + + @Override + public int getCenterYStyleable() { + return R.styleable.ShapeCheckBox_shape_centerY; + } + + @Override + public int getGradientRadiusStyleable() { + return R.styleable.ShapeCheckBox_shape_gradientRadius; + } + + @Override + public int getStrokeColorStyleable() { + return R.styleable.ShapeCheckBox_shape_strokeColor; + } + + @Override + public int getStrokePressedColorStyleable() { + return R.styleable.ShapeCheckBox_shape_strokePressedColor; + } + + @Override + public int getStrokeCheckedColorStyleable() { + return R.styleable.ShapeCheckBox_shape_strokeCheckedColor; + } + + @Override + public int getStrokeDisabledColorStyleable() { + return R.styleable.ShapeCheckBox_shape_strokeDisabledColor; + } + + @Override + public int getStrokeFocusedColorStyleable() { + return R.styleable.ShapeCheckBox_shape_strokeFocusedColor; + } + + @Override + public int getStrokeSelectedColorStyleable() { + return R.styleable.ShapeCheckBox_shape_strokeSelectedColor; + } + + @Override + public int getStrokeWidthStyleable() { + return R.styleable.ShapeCheckBox_shape_strokeWidth; + } + + @Override + public int getDashWidthStyleable() { + return R.styleable.ShapeCheckBox_shape_dashWidth; + } + + @Override + public int getDashGapStyleable() { + return R.styleable.ShapeCheckBox_shape_dashGap; + } + + @Override + public int getInnerRadiusStyleable() { + return R.styleable.ShapeCheckBox_shape_innerRadius; + } + + @Override + public int getInnerRadiusRatioStyleable() { + return R.styleable.ShapeCheckBox_shape_innerRadiusRatio; + } + + @Override + public int getThicknessStyleable() { + return R.styleable.ShapeCheckBox_shape_thickness; + } + + @Override + public int getThicknessRatioStyleable() { + return R.styleable.ShapeCheckBox_shape_thicknessRatio; + } + + @Override + public int getShadowSizeStyleable() { + return R.styleable.ShapeCheckBox_shape_shadowSize; + } + + @Override + public int getShadowColorStyleable() { + return R.styleable.ShapeCheckBox_shape_shadowColor; + } + + @Override + public int getShadowOffsetXStyleable() { + return R.styleable.ShapeCheckBox_shape_shadowOffsetX; + } + + @Override + public int getShadowOffsetYStyleable() { + return R.styleable.ShapeCheckBox_shape_shadowOffsetY; + } + + /** + * {@link ITextColorStyleable} + */ + + @Override + public int getTextColorStyleable() { + return R.styleable.ShapeCheckBox_shape_textColor; + } + + @Override + public int getTextPressedColorStyleable() { + return R.styleable.ShapeCheckBox_shape_textPressedColor; + } + + @Override + public int getTextCheckedColorStyleable() { + return R.styleable.ShapeCheckBox_shape_textCheckedColor; + } + + @Override + public int getTextDisabledColorStyleable() { + return R.styleable.ShapeCheckBox_shape_textDisabledColor; + } + + @Override + public int getTextFocusedColorStyleable() { + return R.styleable.ShapeCheckBox_shape_textFocusedColor; + } + + @Override + public int getTextSelectedColorStyleable() { + return R.styleable.ShapeCheckBox_shape_textSelectedColor; + } + + @Override + public int getTextStartColorStyleable() { + return R.styleable.ShapeCheckBox_shape_textStartColor; + } + + @Override + public int getTextCenterColorStyleable() { + return R.styleable.ShapeCheckBox_shape_textCenterColor; + } + + @Override + public int getTextEndColorStyleable() { + return R.styleable.ShapeCheckBox_shape_textEndColor; + } + + @Override + public int getTextGradientOrientationStyleable() { + return R.styleable.ShapeCheckBox_shape_textGradientOrientation; + } + + /** + * {@link ICompoundButtonStyleable} + */ + + @Override + public int getButtonDrawableStyleable() { + return R.styleable.ShapeCheckBox_shape_buttonDrawable; + } + + @Override + public int getButtonPressedDrawableStyleable() { + return R.styleable.ShapeCheckBox_shape_buttonPressedDrawable; + } + + @Override + public int getButtonCheckedDrawableStyleable() { + return R.styleable.ShapeCheckBox_shape_buttonCheckedDrawable; + } + + @Override + public int getButtonDisabledDrawableStyleable() { + return R.styleable.ShapeCheckBox_shape_buttonDisabledDrawable; + } + + @Override + public int getButtonFocusedDrawableStyleable() { + return R.styleable.ShapeCheckBox_shape_buttonFocusedDrawable; + } + + @Override + public int getButtonSelectedDrawableStyleable() { + return R.styleable.ShapeCheckBox_shape_buttonSelectedDrawable; + } +} \ No newline at end of file diff --git a/library/src/main/java/com/hjq/shape/styleable/ShapeConstraintLayoutStyleable.java b/library/src/main/java/com/hjq/shape/styleable/ShapeConstraintLayoutStyleable.java new file mode 100644 index 0000000..a3c5d47 --- /dev/null +++ b/library/src/main/java/com/hjq/shape/styleable/ShapeConstraintLayoutStyleable.java @@ -0,0 +1,206 @@ +package com.hjq.shape.styleable; + +import com.hjq.shape.R; + +/** + * author : Android 轮子哥 + * github : https://github.com/getActivity/ShapeView + * time : 2021/08/28 + * desc : ConstraintLayout 的 Shape 属性值 + */ +public final class ShapeConstraintLayoutStyleable implements IShapeDrawableStyleable { + + /** + * {@link IShapeDrawableStyleable} + */ + + @Override + public int getShapeTypeStyleable() { + return R.styleable.ShapeConstraintLayout_shape; + } + + @Override + public int getShapeWidthStyleable() { + return R.styleable.ShapeConstraintLayout_shape_width; + } + + @Override + public int getShapeHeightStyleable() { + return R.styleable.ShapeConstraintLayout_shape_height; + } + + @Override + public int getSolidColorStyleable() { + return R.styleable.ShapeConstraintLayout_shape_solidColor; + } + + @Override + public int getSolidPressedColorStyleable() { + return R.styleable.ShapeConstraintLayout_shape_solidPressedColor; + } + + @Override + public int getSolidDisabledColorStyleable() { + return R.styleable.ShapeConstraintLayout_shape_solidDisabledColor; + } + + @Override + public int getSolidFocusedColorStyleable() { + return R.styleable.ShapeConstraintLayout_shape_solidFocusedColor; + } + + @Override + public int getSolidSelectedColorStyleable() { + return R.styleable.ShapeConstraintLayout_shape_solidSelectedColor; + } + + @Override + public int getRadiusStyleable() { + return R.styleable.ShapeConstraintLayout_shape_radius; + } + + @Override + public int getTopLeftRadiusStyleable() { + return R.styleable.ShapeConstraintLayout_shape_topLeftRadius; + } + + @Override + public int getTopRightRadiusStyleable() { + return R.styleable.ShapeConstraintLayout_shape_topRightRadius; + } + + @Override + public int getBottomLeftRadiusStyleable() { + return R.styleable.ShapeConstraintLayout_shape_bottomLeftRadius; + } + + @Override + public int getBottomRightRadiusStyleable() { + return R.styleable.ShapeConstraintLayout_shape_bottomRightRadius; + } + + @Override + public int getStartColorStyleable() { + return R.styleable.ShapeConstraintLayout_shape_startColor; + } + + @Override + public int getCenterColorStyleable() { + return R.styleable.ShapeConstraintLayout_shape_centerColor; + } + + @Override + public int getEndColorStyleable() { + return R.styleable.ShapeConstraintLayout_shape_endColor; + } + + @Override + public int getUseLevelStyleable() { + return R.styleable.ShapeConstraintLayout_shape_useLevel; + } + + @Override + public int getAngleStyleable() { + return R.styleable.ShapeConstraintLayout_shape_angle; + } + + @Override + public int getGradientTypeStyleable() { + return R.styleable.ShapeConstraintLayout_shape_gradientType; + } + + @Override + public int getCenterXStyleable() { + return R.styleable.ShapeConstraintLayout_shape_centerX; + } + + @Override + public int getCenterYStyleable() { + return R.styleable.ShapeConstraintLayout_shape_centerY; + } + + @Override + public int getGradientRadiusStyleable() { + return R.styleable.ShapeConstraintLayout_shape_gradientRadius; + } + + @Override + public int getStrokeColorStyleable() { + return R.styleable.ShapeConstraintLayout_shape_strokeColor; + } + + @Override + public int getStrokePressedColorStyleable() { + return R.styleable.ShapeConstraintLayout_shape_strokePressedColor; + } + + @Override + public int getStrokeDisabledColorStyleable() { + return R.styleable.ShapeConstraintLayout_shape_strokeDisabledColor; + } + + @Override + public int getStrokeFocusedColorStyleable() { + return R.styleable.ShapeConstraintLayout_shape_strokeFocusedColor; + } + + @Override + public int getStrokeSelectedColorStyleable() { + return R.styleable.ShapeConstraintLayout_shape_strokeSelectedColor; + } + + @Override + public int getStrokeWidthStyleable() { + return R.styleable.ShapeConstraintLayout_shape_strokeWidth; + } + + @Override + public int getDashWidthStyleable() { + return R.styleable.ShapeConstraintLayout_shape_dashWidth; + } + + @Override + public int getDashGapStyleable() { + return R.styleable.ShapeConstraintLayout_shape_dashGap; + } + + @Override + public int getInnerRadiusStyleable() { + return R.styleable.ShapeConstraintLayout_shape_innerRadius; + } + + @Override + public int getInnerRadiusRatioStyleable() { + return R.styleable.ShapeConstraintLayout_shape_innerRadiusRatio; + } + + @Override + public int getThicknessStyleable() { + return R.styleable.ShapeConstraintLayout_shape_thickness; + } + + @Override + public int getThicknessRatioStyleable() { + return R.styleable.ShapeConstraintLayout_shape_thicknessRatio; + } + + @Override + public int getShadowSizeStyleable() { + return R.styleable.ShapeConstraintLayout_shape_shadowSize; + } + + @Override + public int getShadowColorStyleable() { + return R.styleable.ShapeConstraintLayout_shape_shadowColor; + } + + @Override + public int getShadowOffsetXStyleable() { + return R.styleable.ShapeConstraintLayout_shape_shadowOffsetX; + } + + @Override + public int getShadowOffsetYStyleable() { + return R.styleable.ShapeConstraintLayout_shape_shadowOffsetY; + } +} \ No newline at end of file diff --git a/library/src/main/java/com/hjq/shape/styleable/ShapeEditTextStyleable.java b/library/src/main/java/com/hjq/shape/styleable/ShapeEditTextStyleable.java new file mode 100644 index 0000000..9795b74 --- /dev/null +++ b/library/src/main/java/com/hjq/shape/styleable/ShapeEditTextStyleable.java @@ -0,0 +1,255 @@ +package com.hjq.shape.styleable; + +import com.hjq.shape.R; + +/** + * author : Android 轮子哥 + * github : https://github.com/getActivity/ShapeView + * time : 2021/08/28 + * desc : EditText 的 Shape 属性值 + */ +public final class ShapeEditTextStyleable implements IShapeDrawableStyleable, ITextColorStyleable { + + /** + * {@link IShapeDrawableStyleable} + */ + + @Override + public int getShapeTypeStyleable() { + return R.styleable.ShapeEditText_shape; + } + + @Override + public int getShapeWidthStyleable() { + return R.styleable.ShapeEditText_shape_width; + } + + @Override + public int getShapeHeightStyleable() { + return R.styleable.ShapeEditText_shape_height; + } + + @Override + public int getSolidColorStyleable() { + return R.styleable.ShapeEditText_shape_solidColor; + } + + @Override + public int getSolidPressedColorStyleable() { + return R.styleable.ShapeEditText_shape_solidPressedColor; + } + + @Override + public int getSolidDisabledColorStyleable() { + return R.styleable.ShapeEditText_shape_solidDisabledColor; + } + + @Override + public int getSolidFocusedColorStyleable() { + return R.styleable.ShapeEditText_shape_solidFocusedColor; + } + + @Override + public int getSolidSelectedColorStyleable() { + return R.styleable.ShapeEditText_shape_solidSelectedColor; + } + + @Override + public int getRadiusStyleable() { + return R.styleable.ShapeEditText_shape_radius; + } + + @Override + public int getTopLeftRadiusStyleable() { + return R.styleable.ShapeEditText_shape_topLeftRadius; + } + + @Override + public int getTopRightRadiusStyleable() { + return R.styleable.ShapeEditText_shape_topRightRadius; + } + + @Override + public int getBottomLeftRadiusStyleable() { + return R.styleable.ShapeEditText_shape_bottomLeftRadius; + } + + @Override + public int getBottomRightRadiusStyleable() { + return R.styleable.ShapeEditText_shape_bottomRightRadius; + } + + @Override + public int getStartColorStyleable() { + return R.styleable.ShapeEditText_shape_startColor; + } + + @Override + public int getCenterColorStyleable() { + return R.styleable.ShapeEditText_shape_centerColor; + } + + @Override + public int getEndColorStyleable() { + return R.styleable.ShapeEditText_shape_endColor; + } + + @Override + public int getUseLevelStyleable() { + return R.styleable.ShapeEditText_shape_useLevel; + } + + @Override + public int getAngleStyleable() { + return R.styleable.ShapeEditText_shape_angle; + } + + @Override + public int getGradientTypeStyleable() { + return R.styleable.ShapeEditText_shape_gradientType; + } + + @Override + public int getCenterXStyleable() { + return R.styleable.ShapeEditText_shape_centerX; + } + + @Override + public int getCenterYStyleable() { + return R.styleable.ShapeEditText_shape_centerY; + } + + @Override + public int getGradientRadiusStyleable() { + return R.styleable.ShapeEditText_shape_gradientRadius; + } + + @Override + public int getStrokeColorStyleable() { + return R.styleable.ShapeEditText_shape_strokeColor; + } + + @Override + public int getStrokePressedColorStyleable() { + return R.styleable.ShapeEditText_shape_strokePressedColor; + } + + @Override + public int getStrokeDisabledColorStyleable() { + return R.styleable.ShapeEditText_shape_strokeDisabledColor; + } + + @Override + public int getStrokeFocusedColorStyleable() { + return R.styleable.ShapeEditText_shape_strokeFocusedColor; + } + + @Override + public int getStrokeSelectedColorStyleable() { + return R.styleable.ShapeEditText_shape_strokeSelectedColor; + } + + @Override + public int getStrokeWidthStyleable() { + return R.styleable.ShapeEditText_shape_strokeWidth; + } + + @Override + public int getDashWidthStyleable() { + return R.styleable.ShapeEditText_shape_dashWidth; + } + + @Override + public int getDashGapStyleable() { + return R.styleable.ShapeEditText_shape_dashGap; + } + + @Override + public int getInnerRadiusStyleable() { + return R.styleable.ShapeEditText_shape_innerRadius; + } + + @Override + public int getInnerRadiusRatioStyleable() { + return R.styleable.ShapeEditText_shape_innerRadiusRatio; + } + + @Override + public int getThicknessStyleable() { + return R.styleable.ShapeEditText_shape_thickness; + } + + @Override + public int getThicknessRatioStyleable() { + return R.styleable.ShapeEditText_shape_thicknessRatio; + } + + @Override + public int getShadowSizeStyleable() { + return R.styleable.ShapeEditText_shape_shadowSize; + } + + @Override + public int getShadowColorStyleable() { + return R.styleable.ShapeEditText_shape_shadowColor; + } + + @Override + public int getShadowOffsetXStyleable() { + return R.styleable.ShapeEditText_shape_shadowOffsetX; + } + + @Override + public int getShadowOffsetYStyleable() { + return R.styleable.ShapeEditText_shape_shadowOffsetY; + } + + /** + * {@link ITextColorStyleable} + */ + + @Override + public int getTextColorStyleable() { + return R.styleable.ShapeEditText_shape_textColor; + } + + @Override + public int getTextPressedColorStyleable() { + return R.styleable.ShapeEditText_shape_textPressedColor; + } + + @Override + public int getTextDisabledColorStyleable() { + return R.styleable.ShapeEditText_shape_textDisabledColor; + } + + @Override + public int getTextFocusedColorStyleable() { + return R.styleable.ShapeEditText_shape_textFocusedColor; + } + + @Override + public int getTextSelectedColorStyleable() { + return R.styleable.ShapeEditText_shape_textSelectedColor; + } + + @Override + public int getTextStartColorStyleable() { + return R.styleable.ShapeEditText_shape_textStartColor; + } + + @Override + public int getTextCenterColorStyleable() { + return R.styleable.ShapeEditText_shape_textCenterColor; + } + + @Override + public int getTextEndColorStyleable() { + return R.styleable.ShapeEditText_shape_textEndColor; + } + + @Override + public int getTextGradientOrientationStyleable() { + return R.styleable.ShapeEditText_shape_textGradientOrientation; + } +} \ No newline at end of file diff --git a/library/src/main/java/com/hjq/shape/styleable/ShapeFrameLayoutStyleable.java b/library/src/main/java/com/hjq/shape/styleable/ShapeFrameLayoutStyleable.java new file mode 100644 index 0000000..0702d39 --- /dev/null +++ b/library/src/main/java/com/hjq/shape/styleable/ShapeFrameLayoutStyleable.java @@ -0,0 +1,206 @@ +package com.hjq.shape.styleable; + +import com.hjq.shape.R; + +/** + * author : Android 轮子哥 + * github : https://github.com/getActivity/ShapeView + * time : 2021/08/28 + * desc : FrameLayout 的 Shape 属性值 + */ +public final class ShapeFrameLayoutStyleable implements IShapeDrawableStyleable { + + /** + * {@link IShapeDrawableStyleable} + */ + + @Override + public int getShapeTypeStyleable() { + return R.styleable.ShapeFrameLayout_shape; + } + + @Override + public int getShapeWidthStyleable() { + return R.styleable.ShapeFrameLayout_shape_width; + } + + @Override + public int getShapeHeightStyleable() { + return R.styleable.ShapeFrameLayout_shape_height; + } + + @Override + public int getSolidColorStyleable() { + return R.styleable.ShapeFrameLayout_shape_solidColor; + } + + @Override + public int getSolidPressedColorStyleable() { + return R.styleable.ShapeFrameLayout_shape_solidPressedColor; + } + + @Override + public int getSolidDisabledColorStyleable() { + return R.styleable.ShapeFrameLayout_shape_solidDisabledColor; + } + + @Override + public int getSolidFocusedColorStyleable() { + return R.styleable.ShapeFrameLayout_shape_solidFocusedColor; + } + + @Override + public int getSolidSelectedColorStyleable() { + return R.styleable.ShapeFrameLayout_shape_solidSelectedColor; + } + + @Override + public int getRadiusStyleable() { + return R.styleable.ShapeFrameLayout_shape_radius; + } + + @Override + public int getTopLeftRadiusStyleable() { + return R.styleable.ShapeFrameLayout_shape_topLeftRadius; + } + + @Override + public int getTopRightRadiusStyleable() { + return R.styleable.ShapeFrameLayout_shape_topRightRadius; + } + + @Override + public int getBottomLeftRadiusStyleable() { + return R.styleable.ShapeFrameLayout_shape_bottomLeftRadius; + } + + @Override + public int getBottomRightRadiusStyleable() { + return R.styleable.ShapeFrameLayout_shape_bottomRightRadius; + } + + @Override + public int getStartColorStyleable() { + return R.styleable.ShapeFrameLayout_shape_startColor; + } + + @Override + public int getCenterColorStyleable() { + return R.styleable.ShapeFrameLayout_shape_centerColor; + } + + @Override + public int getEndColorStyleable() { + return R.styleable.ShapeFrameLayout_shape_endColor; + } + + @Override + public int getUseLevelStyleable() { + return R.styleable.ShapeFrameLayout_shape_useLevel; + } + + @Override + public int getAngleStyleable() { + return R.styleable.ShapeFrameLayout_shape_angle; + } + + @Override + public int getGradientTypeStyleable() { + return R.styleable.ShapeFrameLayout_shape_gradientType; + } + + @Override + public int getCenterXStyleable() { + return R.styleable.ShapeFrameLayout_shape_centerX; + } + + @Override + public int getCenterYStyleable() { + return R.styleable.ShapeFrameLayout_shape_centerY; + } + + @Override + public int getGradientRadiusStyleable() { + return R.styleable.ShapeFrameLayout_shape_gradientRadius; + } + + @Override + public int getStrokeColorStyleable() { + return R.styleable.ShapeFrameLayout_shape_strokeColor; + } + + @Override + public int getStrokePressedColorStyleable() { + return R.styleable.ShapeFrameLayout_shape_strokePressedColor; + } + + @Override + public int getStrokeDisabledColorStyleable() { + return R.styleable.ShapeFrameLayout_shape_strokeDisabledColor; + } + + @Override + public int getStrokeFocusedColorStyleable() { + return R.styleable.ShapeFrameLayout_shape_strokeFocusedColor; + } + + @Override + public int getStrokeSelectedColorStyleable() { + return R.styleable.ShapeFrameLayout_shape_strokeSelectedColor; + } + + @Override + public int getStrokeWidthStyleable() { + return R.styleable.ShapeFrameLayout_shape_strokeWidth; + } + + @Override + public int getDashWidthStyleable() { + return R.styleable.ShapeFrameLayout_shape_dashWidth; + } + + @Override + public int getDashGapStyleable() { + return R.styleable.ShapeFrameLayout_shape_dashGap; + } + + @Override + public int getInnerRadiusStyleable() { + return R.styleable.ShapeFrameLayout_shape_innerRadius; + } + + @Override + public int getInnerRadiusRatioStyleable() { + return R.styleable.ShapeFrameLayout_shape_innerRadiusRatio; + } + + @Override + public int getThicknessStyleable() { + return R.styleable.ShapeFrameLayout_shape_thickness; + } + + @Override + public int getThicknessRatioStyleable() { + return R.styleable.ShapeFrameLayout_shape_thicknessRatio; + } + + @Override + public int getShadowSizeStyleable() { + return R.styleable.ShapeFrameLayout_shape_shadowSize; + } + + @Override + public int getShadowColorStyleable() { + return R.styleable.ShapeFrameLayout_shape_shadowColor; + } + + @Override + public int getShadowOffsetXStyleable() { + return R.styleable.ShapeFrameLayout_shape_shadowOffsetX; + } + + @Override + public int getShadowOffsetYStyleable() { + return R.styleable.ShapeFrameLayout_shape_shadowOffsetY; + } +} \ No newline at end of file diff --git a/library/src/main/java/com/hjq/shape/styleable/ShapeImageViewStyleable.java b/library/src/main/java/com/hjq/shape/styleable/ShapeImageViewStyleable.java new file mode 100644 index 0000000..01d6502 --- /dev/null +++ b/library/src/main/java/com/hjq/shape/styleable/ShapeImageViewStyleable.java @@ -0,0 +1,206 @@ +package com.hjq.shape.styleable; + +import com.hjq.shape.R; + +/** + * author : Android 轮子哥 + * github : https://github.com/getActivity/ShapeView + * time : 2021/08/28 + * desc : ImageView 的 Shape 属性值 + */ +public final class ShapeImageViewStyleable implements IShapeDrawableStyleable { + + /** + * {@link IShapeDrawableStyleable} + */ + + @Override + public int getShapeTypeStyleable() { + return R.styleable.ShapeImageView_shape; + } + + @Override + public int getShapeWidthStyleable() { + return R.styleable.ShapeImageView_shape_width; + } + + @Override + public int getShapeHeightStyleable() { + return R.styleable.ShapeImageView_shape_height; + } + + @Override + public int getSolidColorStyleable() { + return R.styleable.ShapeImageView_shape_solidColor; + } + + @Override + public int getSolidPressedColorStyleable() { + return R.styleable.ShapeImageView_shape_solidPressedColor; + } + + @Override + public int getSolidDisabledColorStyleable() { + return R.styleable.ShapeImageView_shape_solidDisabledColor; + } + + @Override + public int getSolidFocusedColorStyleable() { + return R.styleable.ShapeImageView_shape_solidFocusedColor; + } + + @Override + public int getSolidSelectedColorStyleable() { + return R.styleable.ShapeImageView_shape_solidSelectedColor; + } + + @Override + public int getRadiusStyleable() { + return R.styleable.ShapeImageView_shape_radius; + } + + @Override + public int getTopLeftRadiusStyleable() { + return R.styleable.ShapeImageView_shape_topLeftRadius; + } + + @Override + public int getTopRightRadiusStyleable() { + return R.styleable.ShapeImageView_shape_topRightRadius; + } + + @Override + public int getBottomLeftRadiusStyleable() { + return R.styleable.ShapeImageView_shape_bottomLeftRadius; + } + + @Override + public int getBottomRightRadiusStyleable() { + return R.styleable.ShapeImageView_shape_bottomRightRadius; + } + + @Override + public int getStartColorStyleable() { + return R.styleable.ShapeImageView_shape_startColor; + } + + @Override + public int getCenterColorStyleable() { + return R.styleable.ShapeImageView_shape_centerColor; + } + + @Override + public int getEndColorStyleable() { + return R.styleable.ShapeImageView_shape_endColor; + } + + @Override + public int getUseLevelStyleable() { + return R.styleable.ShapeImageView_shape_useLevel; + } + + @Override + public int getAngleStyleable() { + return R.styleable.ShapeImageView_shape_angle; + } + + @Override + public int getGradientTypeStyleable() { + return R.styleable.ShapeImageView_shape_gradientType; + } + + @Override + public int getCenterXStyleable() { + return R.styleable.ShapeImageView_shape_centerX; + } + + @Override + public int getCenterYStyleable() { + return R.styleable.ShapeImageView_shape_centerY; + } + + @Override + public int getGradientRadiusStyleable() { + return R.styleable.ShapeImageView_shape_gradientRadius; + } + + @Override + public int getStrokeColorStyleable() { + return R.styleable.ShapeImageView_shape_strokeColor; + } + + @Override + public int getStrokePressedColorStyleable() { + return R.styleable.ShapeImageView_shape_strokePressedColor; + } + + @Override + public int getStrokeDisabledColorStyleable() { + return R.styleable.ShapeImageView_shape_strokeDisabledColor; + } + + @Override + public int getStrokeFocusedColorStyleable() { + return R.styleable.ShapeImageView_shape_strokeFocusedColor; + } + + @Override + public int getStrokeSelectedColorStyleable() { + return R.styleable.ShapeImageView_shape_strokeSelectedColor; + } + + @Override + public int getStrokeWidthStyleable() { + return R.styleable.ShapeImageView_shape_strokeWidth; + } + + @Override + public int getDashWidthStyleable() { + return R.styleable.ShapeImageView_shape_dashWidth; + } + + @Override + public int getDashGapStyleable() { + return R.styleable.ShapeImageView_shape_dashGap; + } + + @Override + public int getInnerRadiusStyleable() { + return R.styleable.ShapeImageView_shape_innerRadius; + } + + @Override + public int getInnerRadiusRatioStyleable() { + return R.styleable.ShapeImageView_shape_innerRadiusRatio; + } + + @Override + public int getThicknessStyleable() { + return R.styleable.ShapeImageView_shape_thickness; + } + + @Override + public int getThicknessRatioStyleable() { + return R.styleable.ShapeImageView_shape_thicknessRatio; + } + + @Override + public int getShadowSizeStyleable() { + return R.styleable.ShapeImageView_shape_shadowSize; + } + + @Override + public int getShadowColorStyleable() { + return R.styleable.ShapeImageView_shape_shadowColor; + } + + @Override + public int getShadowOffsetXStyleable() { + return R.styleable.ShapeImageView_shape_shadowOffsetX; + } + + @Override + public int getShadowOffsetYStyleable() { + return R.styleable.ShapeImageView_shape_shadowOffsetY; + } +} \ No newline at end of file diff --git a/library/src/main/java/com/hjq/shape/styleable/ShapeLinearLayoutStyleable.java b/library/src/main/java/com/hjq/shape/styleable/ShapeLinearLayoutStyleable.java new file mode 100644 index 0000000..d78af91 --- /dev/null +++ b/library/src/main/java/com/hjq/shape/styleable/ShapeLinearLayoutStyleable.java @@ -0,0 +1,206 @@ +package com.hjq.shape.styleable; + +import com.hjq.shape.R; + +/** + * author : Android 轮子哥 + * github : https://github.com/getActivity/ShapeView + * time : 2021/08/28 + * desc : LinearLayout 的 Shape 属性值 + */ +public final class ShapeLinearLayoutStyleable implements IShapeDrawableStyleable { + + /** + * {@link IShapeDrawableStyleable} + */ + + @Override + public int getShapeTypeStyleable() { + return R.styleable.ShapeLinearLayout_shape; + } + + @Override + public int getShapeWidthStyleable() { + return R.styleable.ShapeLinearLayout_shape_width; + } + + @Override + public int getShapeHeightStyleable() { + return R.styleable.ShapeLinearLayout_shape_height; + } + + @Override + public int getSolidColorStyleable() { + return R.styleable.ShapeLinearLayout_shape_solidColor; + } + + @Override + public int getSolidPressedColorStyleable() { + return R.styleable.ShapeLinearLayout_shape_solidPressedColor; + } + + @Override + public int getSolidDisabledColorStyleable() { + return R.styleable.ShapeLinearLayout_shape_solidDisabledColor; + } + + @Override + public int getSolidFocusedColorStyleable() { + return R.styleable.ShapeLinearLayout_shape_solidFocusedColor; + } + + @Override + public int getSolidSelectedColorStyleable() { + return R.styleable.ShapeLinearLayout_shape_solidSelectedColor; + } + + @Override + public int getRadiusStyleable() { + return R.styleable.ShapeLinearLayout_shape_radius; + } + + @Override + public int getTopLeftRadiusStyleable() { + return R.styleable.ShapeLinearLayout_shape_topLeftRadius; + } + + @Override + public int getTopRightRadiusStyleable() { + return R.styleable.ShapeLinearLayout_shape_topRightRadius; + } + + @Override + public int getBottomLeftRadiusStyleable() { + return R.styleable.ShapeLinearLayout_shape_bottomLeftRadius; + } + + @Override + public int getBottomRightRadiusStyleable() { + return R.styleable.ShapeLinearLayout_shape_bottomRightRadius; + } + + @Override + public int getStartColorStyleable() { + return R.styleable.ShapeLinearLayout_shape_startColor; + } + + @Override + public int getCenterColorStyleable() { + return R.styleable.ShapeLinearLayout_shape_centerColor; + } + + @Override + public int getEndColorStyleable() { + return R.styleable.ShapeLinearLayout_shape_endColor; + } + + @Override + public int getUseLevelStyleable() { + return R.styleable.ShapeLinearLayout_shape_useLevel; + } + + @Override + public int getAngleStyleable() { + return R.styleable.ShapeLinearLayout_shape_angle; + } + + @Override + public int getGradientTypeStyleable() { + return R.styleable.ShapeLinearLayout_shape_gradientType; + } + + @Override + public int getCenterXStyleable() { + return R.styleable.ShapeLinearLayout_shape_centerX; + } + + @Override + public int getCenterYStyleable() { + return R.styleable.ShapeLinearLayout_shape_centerY; + } + + @Override + public int getGradientRadiusStyleable() { + return R.styleable.ShapeLinearLayout_shape_gradientRadius; + } + + @Override + public int getStrokeColorStyleable() { + return R.styleable.ShapeLinearLayout_shape_strokeColor; + } + + @Override + public int getStrokePressedColorStyleable() { + return R.styleable.ShapeLinearLayout_shape_strokePressedColor; + } + + @Override + public int getStrokeDisabledColorStyleable() { + return R.styleable.ShapeLinearLayout_shape_strokeDisabledColor; + } + + @Override + public int getStrokeFocusedColorStyleable() { + return R.styleable.ShapeLinearLayout_shape_strokeFocusedColor; + } + + @Override + public int getStrokeSelectedColorStyleable() { + return R.styleable.ShapeLinearLayout_shape_strokeSelectedColor; + } + + @Override + public int getStrokeWidthStyleable() { + return R.styleable.ShapeLinearLayout_shape_strokeWidth; + } + + @Override + public int getDashWidthStyleable() { + return R.styleable.ShapeLinearLayout_shape_dashWidth; + } + + @Override + public int getDashGapStyleable() { + return R.styleable.ShapeLinearLayout_shape_dashGap; + } + + @Override + public int getInnerRadiusStyleable() { + return R.styleable.ShapeLinearLayout_shape_innerRadius; + } + + @Override + public int getInnerRadiusRatioStyleable() { + return R.styleable.ShapeLinearLayout_shape_innerRadiusRatio; + } + + @Override + public int getThicknessStyleable() { + return R.styleable.ShapeLinearLayout_shape_thickness; + } + + @Override + public int getThicknessRatioStyleable() { + return R.styleable.ShapeLinearLayout_shape_thicknessRatio; + } + + @Override + public int getShadowSizeStyleable() { + return R.styleable.ShapeLinearLayout_shape_shadowSize; + } + + @Override + public int getShadowColorStyleable() { + return R.styleable.ShapeLinearLayout_shape_shadowColor; + } + + @Override + public int getShadowOffsetXStyleable() { + return R.styleable.ShapeLinearLayout_shape_shadowOffsetX; + } + + @Override + public int getShadowOffsetYStyleable() { + return R.styleable.ShapeLinearLayout_shape_shadowOffsetY; + } +} \ No newline at end of file diff --git a/library/src/main/java/com/hjq/shape/styleable/ShapeRadioButtonStyleable.java b/library/src/main/java/com/hjq/shape/styleable/ShapeRadioButtonStyleable.java new file mode 100644 index 0000000..ec5a1c7 --- /dev/null +++ b/library/src/main/java/com/hjq/shape/styleable/ShapeRadioButtonStyleable.java @@ -0,0 +1,304 @@ +package com.hjq.shape.styleable; + +import com.hjq.shape.R; + +/** + * author : Android 轮子哥 + * github : https://github.com/getActivity/ShapeView + * time : 2021/08/28 + * desc : RadioButton 的 Shape 属性值 + */ +public final class ShapeRadioButtonStyleable implements IShapeDrawableStyleable, + ITextColorStyleable, ICompoundButtonStyleable { + + /** + * {@link IShapeDrawableStyleable} + */ + + @Override + public int getShapeTypeStyleable() { + return R.styleable.ShapeRadioButton_shape; + } + + @Override + public int getShapeWidthStyleable() { + return R.styleable.ShapeRadioButton_shape_width; + } + + @Override + public int getShapeHeightStyleable() { + return R.styleable.ShapeRadioButton_shape_height; + } + + @Override + public int getSolidColorStyleable() { + return R.styleable.ShapeRadioButton_shape_solidColor; + } + + @Override + public int getSolidPressedColorStyleable() { + return R.styleable.ShapeRadioButton_shape_solidPressedColor; + } + + @Override + public int getSolidCheckedColorStyleable() { + return R.styleable.ShapeRadioButton_shape_solidCheckedColor; + } + + @Override + public int getSolidDisabledColorStyleable() { + return R.styleable.ShapeRadioButton_shape_solidDisabledColor; + } + + @Override + public int getSolidFocusedColorStyleable() { + return R.styleable.ShapeRadioButton_shape_solidFocusedColor; + } + + @Override + public int getSolidSelectedColorStyleable() { + return R.styleable.ShapeRadioButton_shape_solidSelectedColor; + } + + @Override + public int getRadiusStyleable() { + return R.styleable.ShapeRadioButton_shape_radius; + } + + @Override + public int getTopLeftRadiusStyleable() { + return R.styleable.ShapeRadioButton_shape_topLeftRadius; + } + + @Override + public int getTopRightRadiusStyleable() { + return R.styleable.ShapeRadioButton_shape_topRightRadius; + } + + @Override + public int getBottomLeftRadiusStyleable() { + return R.styleable.ShapeRadioButton_shape_bottomLeftRadius; + } + + @Override + public int getBottomRightRadiusStyleable() { + return R.styleable.ShapeRadioButton_shape_bottomRightRadius; + } + + @Override + public int getStartColorStyleable() { + return R.styleable.ShapeRadioButton_shape_startColor; + } + + @Override + public int getCenterColorStyleable() { + return R.styleable.ShapeRadioButton_shape_centerColor; + } + + @Override + public int getEndColorStyleable() { + return R.styleable.ShapeRadioButton_shape_endColor; + } + + @Override + public int getUseLevelStyleable() { + return R.styleable.ShapeRadioButton_shape_useLevel; + } + + @Override + public int getAngleStyleable() { + return R.styleable.ShapeRadioButton_shape_angle; + } + + @Override + public int getGradientTypeStyleable() { + return R.styleable.ShapeRadioButton_shape_gradientType; + } + + @Override + public int getCenterXStyleable() { + return R.styleable.ShapeRadioButton_shape_centerX; + } + + @Override + public int getCenterYStyleable() { + return R.styleable.ShapeRadioButton_shape_centerY; + } + + @Override + public int getGradientRadiusStyleable() { + return R.styleable.ShapeRadioButton_shape_gradientRadius; + } + + @Override + public int getStrokeColorStyleable() { + return R.styleable.ShapeRadioButton_shape_strokeColor; + } + + @Override + public int getStrokePressedColorStyleable() { + return R.styleable.ShapeRadioButton_shape_strokePressedColor; + } + + @Override + public int getStrokeCheckedColorStyleable() { + return R.styleable.ShapeRadioButton_shape_strokeCheckedColor; + } + + @Override + public int getStrokeDisabledColorStyleable() { + return R.styleable.ShapeRadioButton_shape_strokeDisabledColor; + } + + @Override + public int getStrokeFocusedColorStyleable() { + return R.styleable.ShapeRadioButton_shape_strokeFocusedColor; + } + + @Override + public int getStrokeSelectedColorStyleable() { + return R.styleable.ShapeRadioButton_shape_strokeSelectedColor; + } + + @Override + public int getStrokeWidthStyleable() { + return R.styleable.ShapeRadioButton_shape_strokeWidth; + } + + @Override + public int getDashWidthStyleable() { + return R.styleable.ShapeRadioButton_shape_dashWidth; + } + + @Override + public int getDashGapStyleable() { + return R.styleable.ShapeRadioButton_shape_dashGap; + } + + @Override + public int getInnerRadiusStyleable() { + return R.styleable.ShapeRadioButton_shape_innerRadius; + } + + @Override + public int getInnerRadiusRatioStyleable() { + return R.styleable.ShapeRadioButton_shape_innerRadiusRatio; + } + + @Override + public int getThicknessStyleable() { + return R.styleable.ShapeRadioButton_shape_thickness; + } + + @Override + public int getThicknessRatioStyleable() { + return R.styleable.ShapeRadioButton_shape_thicknessRatio; + } + + @Override + public int getShadowSizeStyleable() { + return R.styleable.ShapeRadioButton_shape_shadowSize; + } + + @Override + public int getShadowColorStyleable() { + return R.styleable.ShapeRadioButton_shape_shadowColor; + } + + @Override + public int getShadowOffsetXStyleable() { + return R.styleable.ShapeRadioButton_shape_shadowOffsetX; + } + + @Override + public int getShadowOffsetYStyleable() { + return R.styleable.ShapeRadioButton_shape_shadowOffsetY; + } + + /** + * {@link ITextColorStyleable} + */ + + @Override + public int getTextColorStyleable() { + return R.styleable.ShapeRadioButton_shape_textColor; + } + + @Override + public int getTextPressedColorStyleable() { + return R.styleable.ShapeRadioButton_shape_textPressedColor; + } + + @Override + public int getTextCheckedColorStyleable() { + return R.styleable.ShapeRadioButton_shape_textCheckedColor; + } + + @Override + public int getTextDisabledColorStyleable() { + return R.styleable.ShapeRadioButton_shape_textDisabledColor; + } + + @Override + public int getTextFocusedColorStyleable() { + return R.styleable.ShapeRadioButton_shape_textFocusedColor; + } + + @Override + public int getTextSelectedColorStyleable() { + return R.styleable.ShapeRadioButton_shape_textSelectedColor; + } + + @Override + public int getTextStartColorStyleable() { + return R.styleable.ShapeRadioButton_shape_textStartColor; + } + + @Override + public int getTextCenterColorStyleable() { + return R.styleable.ShapeRadioButton_shape_textCenterColor; + } + + @Override + public int getTextEndColorStyleable() { + return R.styleable.ShapeRadioButton_shape_textEndColor; + } + + @Override + public int getTextGradientOrientationStyleable() { + return R.styleable.ShapeRadioButton_shape_textGradientOrientation; + } + /** + * {@link ICompoundButtonStyleable} + */ + + @Override + public int getButtonDrawableStyleable() { + return R.styleable.ShapeRadioButton_shape_buttonDrawable; + } + + @Override + public int getButtonPressedDrawableStyleable() { + return R.styleable.ShapeRadioButton_shape_buttonPressedDrawable; + } + + @Override + public int getButtonCheckedDrawableStyleable() { + return R.styleable.ShapeRadioButton_shape_buttonCheckedDrawable; + } + + @Override + public int getButtonDisabledDrawableStyleable() { + return R.styleable.ShapeRadioButton_shape_buttonDisabledDrawable; + } + + @Override + public int getButtonFocusedDrawableStyleable() { + return R.styleable.ShapeRadioButton_shape_buttonFocusedDrawable; + } + + @Override + public int getButtonSelectedDrawableStyleable() { + return R.styleable.ShapeRadioButton_shape_buttonSelectedDrawable; + } +} \ No newline at end of file diff --git a/library/src/main/java/com/hjq/shape/styleable/ShapeRecyclerViewStyleable.java b/library/src/main/java/com/hjq/shape/styleable/ShapeRecyclerViewStyleable.java new file mode 100644 index 0000000..90ba795 --- /dev/null +++ b/library/src/main/java/com/hjq/shape/styleable/ShapeRecyclerViewStyleable.java @@ -0,0 +1,206 @@ +package com.hjq.shape.styleable; + +import com.hjq.shape.R; + +/** + * author : Android 轮子哥 + * github : https://github.com/getActivity/ShapeView + * time : 2021/08/28 + * desc : RecyclerView 的 Shape 属性值 + */ +public final class ShapeRecyclerViewStyleable implements IShapeDrawableStyleable { + + /** + * {@link IShapeDrawableStyleable} + */ + + @Override + public int getShapeTypeStyleable() { + return R.styleable.ShapeRecyclerView_shape; + } + + @Override + public int getShapeWidthStyleable() { + return R.styleable.ShapeRecyclerView_shape_width; + } + + @Override + public int getShapeHeightStyleable() { + return R.styleable.ShapeRecyclerView_shape_height; + } + + @Override + public int getSolidColorStyleable() { + return R.styleable.ShapeRecyclerView_shape_solidColor; + } + + @Override + public int getSolidPressedColorStyleable() { + return R.styleable.ShapeRecyclerView_shape_solidPressedColor; + } + + @Override + public int getSolidDisabledColorStyleable() { + return R.styleable.ShapeRecyclerView_shape_solidDisabledColor; + } + + @Override + public int getSolidFocusedColorStyleable() { + return R.styleable.ShapeRecyclerView_shape_solidFocusedColor; + } + + @Override + public int getSolidSelectedColorStyleable() { + return R.styleable.ShapeRecyclerView_shape_solidSelectedColor; + } + + @Override + public int getRadiusStyleable() { + return R.styleable.ShapeRecyclerView_shape_radius; + } + + @Override + public int getTopLeftRadiusStyleable() { + return R.styleable.ShapeRecyclerView_shape_topLeftRadius; + } + + @Override + public int getTopRightRadiusStyleable() { + return R.styleable.ShapeRecyclerView_shape_topRightRadius; + } + + @Override + public int getBottomLeftRadiusStyleable() { + return R.styleable.ShapeRecyclerView_shape_bottomLeftRadius; + } + + @Override + public int getBottomRightRadiusStyleable() { + return R.styleable.ShapeRecyclerView_shape_bottomRightRadius; + } + + @Override + public int getStartColorStyleable() { + return R.styleable.ShapeRecyclerView_shape_startColor; + } + + @Override + public int getCenterColorStyleable() { + return R.styleable.ShapeRecyclerView_shape_centerColor; + } + + @Override + public int getEndColorStyleable() { + return R.styleable.ShapeRecyclerView_shape_endColor; + } + + @Override + public int getUseLevelStyleable() { + return R.styleable.ShapeRecyclerView_shape_useLevel; + } + + @Override + public int getAngleStyleable() { + return R.styleable.ShapeRecyclerView_shape_angle; + } + + @Override + public int getGradientTypeStyleable() { + return R.styleable.ShapeRecyclerView_shape_gradientType; + } + + @Override + public int getCenterXStyleable() { + return R.styleable.ShapeRecyclerView_shape_centerX; + } + + @Override + public int getCenterYStyleable() { + return R.styleable.ShapeRecyclerView_shape_centerY; + } + + @Override + public int getGradientRadiusStyleable() { + return R.styleable.ShapeRecyclerView_shape_gradientRadius; + } + + @Override + public int getStrokeColorStyleable() { + return R.styleable.ShapeRecyclerView_shape_strokeColor; + } + + @Override + public int getStrokePressedColorStyleable() { + return R.styleable.ShapeRecyclerView_shape_strokePressedColor; + } + + @Override + public int getStrokeDisabledColorStyleable() { + return R.styleable.ShapeRecyclerView_shape_strokeDisabledColor; + } + + @Override + public int getStrokeFocusedColorStyleable() { + return R.styleable.ShapeRecyclerView_shape_strokeFocusedColor; + } + + @Override + public int getStrokeSelectedColorStyleable() { + return R.styleable.ShapeRecyclerView_shape_strokeSelectedColor; + } + + @Override + public int getStrokeWidthStyleable() { + return R.styleable.ShapeRecyclerView_shape_strokeWidth; + } + + @Override + public int getDashWidthStyleable() { + return R.styleable.ShapeRecyclerView_shape_dashWidth; + } + + @Override + public int getDashGapStyleable() { + return R.styleable.ShapeRecyclerView_shape_dashGap; + } + + @Override + public int getInnerRadiusStyleable() { + return R.styleable.ShapeRecyclerView_shape_innerRadius; + } + + @Override + public int getInnerRadiusRatioStyleable() { + return R.styleable.ShapeRecyclerView_shape_innerRadiusRatio; + } + + @Override + public int getThicknessStyleable() { + return R.styleable.ShapeRecyclerView_shape_thickness; + } + + @Override + public int getThicknessRatioStyleable() { + return R.styleable.ShapeRecyclerView_shape_thicknessRatio; + } + + @Override + public int getShadowSizeStyleable() { + return R.styleable.ShapeRecyclerView_shape_shadowSize; + } + + @Override + public int getShadowColorStyleable() { + return R.styleable.ShapeRecyclerView_shape_shadowColor; + } + + @Override + public int getShadowOffsetXStyleable() { + return R.styleable.ShapeRecyclerView_shape_shadowOffsetX; + } + + @Override + public int getShadowOffsetYStyleable() { + return R.styleable.ShapeRecyclerView_shape_shadowOffsetY; + } +} \ No newline at end of file diff --git a/library/src/main/java/com/hjq/shape/styleable/ShapeRelativeLayoutStyleable.java b/library/src/main/java/com/hjq/shape/styleable/ShapeRelativeLayoutStyleable.java new file mode 100644 index 0000000..8b8dea4 --- /dev/null +++ b/library/src/main/java/com/hjq/shape/styleable/ShapeRelativeLayoutStyleable.java @@ -0,0 +1,206 @@ +package com.hjq.shape.styleable; + +import com.hjq.shape.R; + +/** + * author : Android 轮子哥 + * github : https://github.com/getActivity/ShapeView + * time : 2021/08/28 + * desc : RelativeLayout 的 Shape 属性值 + */ +public final class ShapeRelativeLayoutStyleable implements IShapeDrawableStyleable { + + /** + * {@link IShapeDrawableStyleable} + */ + + @Override + public int getShapeTypeStyleable() { + return R.styleable.ShapeRelativeLayout_shape; + } + + @Override + public int getShapeWidthStyleable() { + return R.styleable.ShapeRelativeLayout_shape_width; + } + + @Override + public int getShapeHeightStyleable() { + return R.styleable.ShapeRelativeLayout_shape_height; + } + + @Override + public int getSolidColorStyleable() { + return R.styleable.ShapeRelativeLayout_shape_solidColor; + } + + @Override + public int getSolidPressedColorStyleable() { + return R.styleable.ShapeRelativeLayout_shape_solidPressedColor; + } + + @Override + public int getSolidDisabledColorStyleable() { + return R.styleable.ShapeRelativeLayout_shape_solidDisabledColor; + } + + @Override + public int getSolidFocusedColorStyleable() { + return R.styleable.ShapeRelativeLayout_shape_solidFocusedColor; + } + + @Override + public int getSolidSelectedColorStyleable() { + return R.styleable.ShapeRelativeLayout_shape_solidSelectedColor; + } + + @Override + public int getRadiusStyleable() { + return R.styleable.ShapeRelativeLayout_shape_radius; + } + + @Override + public int getTopLeftRadiusStyleable() { + return R.styleable.ShapeRelativeLayout_shape_topLeftRadius; + } + + @Override + public int getTopRightRadiusStyleable() { + return R.styleable.ShapeRelativeLayout_shape_topRightRadius; + } + + @Override + public int getBottomLeftRadiusStyleable() { + return R.styleable.ShapeRelativeLayout_shape_bottomLeftRadius; + } + + @Override + public int getBottomRightRadiusStyleable() { + return R.styleable.ShapeRelativeLayout_shape_bottomRightRadius; + } + + @Override + public int getStartColorStyleable() { + return R.styleable.ShapeRelativeLayout_shape_startColor; + } + + @Override + public int getCenterColorStyleable() { + return R.styleable.ShapeRelativeLayout_shape_centerColor; + } + + @Override + public int getEndColorStyleable() { + return R.styleable.ShapeRelativeLayout_shape_endColor; + } + + @Override + public int getUseLevelStyleable() { + return R.styleable.ShapeRelativeLayout_shape_useLevel; + } + + @Override + public int getAngleStyleable() { + return R.styleable.ShapeRelativeLayout_shape_angle; + } + + @Override + public int getGradientTypeStyleable() { + return R.styleable.ShapeRelativeLayout_shape_gradientType; + } + + @Override + public int getCenterXStyleable() { + return R.styleable.ShapeRelativeLayout_shape_centerX; + } + + @Override + public int getCenterYStyleable() { + return R.styleable.ShapeRelativeLayout_shape_centerY; + } + + @Override + public int getGradientRadiusStyleable() { + return R.styleable.ShapeRelativeLayout_shape_gradientRadius; + } + + @Override + public int getStrokeColorStyleable() { + return R.styleable.ShapeRelativeLayout_shape_strokeColor; + } + + @Override + public int getStrokePressedColorStyleable() { + return R.styleable.ShapeRelativeLayout_shape_strokePressedColor; + } + + @Override + public int getStrokeDisabledColorStyleable() { + return R.styleable.ShapeRelativeLayout_shape_strokeDisabledColor; + } + + @Override + public int getStrokeFocusedColorStyleable() { + return R.styleable.ShapeRelativeLayout_shape_strokeFocusedColor; + } + + @Override + public int getStrokeSelectedColorStyleable() { + return R.styleable.ShapeRelativeLayout_shape_strokeSelectedColor; + } + + @Override + public int getStrokeWidthStyleable() { + return R.styleable.ShapeRelativeLayout_shape_strokeWidth; + } + + @Override + public int getDashWidthStyleable() { + return R.styleable.ShapeRelativeLayout_shape_dashWidth; + } + + @Override + public int getDashGapStyleable() { + return R.styleable.ShapeRelativeLayout_shape_dashGap; + } + + @Override + public int getInnerRadiusStyleable() { + return R.styleable.ShapeRelativeLayout_shape_innerRadius; + } + + @Override + public int getInnerRadiusRatioStyleable() { + return R.styleable.ShapeRelativeLayout_shape_innerRadiusRatio; + } + + @Override + public int getThicknessStyleable() { + return R.styleable.ShapeRelativeLayout_shape_thickness; + } + + @Override + public int getThicknessRatioStyleable() { + return R.styleable.ShapeRelativeLayout_shape_thicknessRatio; + } + + @Override + public int getShadowSizeStyleable() { + return R.styleable.ShapeRelativeLayout_shape_shadowSize; + } + + @Override + public int getShadowColorStyleable() { + return R.styleable.ShapeRelativeLayout_shape_shadowColor; + } + + @Override + public int getShadowOffsetXStyleable() { + return R.styleable.ShapeRelativeLayout_shape_shadowOffsetX; + } + + @Override + public int getShadowOffsetYStyleable() { + return R.styleable.ShapeRelativeLayout_shape_shadowOffsetY; + } +} \ No newline at end of file diff --git a/library/src/main/java/com/hjq/shape/styleable/ShapeTextViewStyleable.java b/library/src/main/java/com/hjq/shape/styleable/ShapeTextViewStyleable.java new file mode 100644 index 0000000..933353d --- /dev/null +++ b/library/src/main/java/com/hjq/shape/styleable/ShapeTextViewStyleable.java @@ -0,0 +1,255 @@ +package com.hjq.shape.styleable; + +import com.hjq.shape.R; + +/** + * author : Android 轮子哥 + * github : https://github.com/getActivity/ShapeView + * time : 2021/08/28 + * desc : TextView 的 Shape 属性值 + */ +public final class ShapeTextViewStyleable implements IShapeDrawableStyleable, ITextColorStyleable { + + /** + * {@link IShapeDrawableStyleable} + */ + + @Override + public int getShapeTypeStyleable() { + return R.styleable.ShapeTextView_shape; + } + + @Override + public int getShapeWidthStyleable() { + return R.styleable.ShapeTextView_shape_width; + } + + @Override + public int getShapeHeightStyleable() { + return R.styleable.ShapeTextView_shape_height; + } + + @Override + public int getSolidColorStyleable() { + return R.styleable.ShapeTextView_shape_solidColor; + } + + @Override + public int getSolidPressedColorStyleable() { + return R.styleable.ShapeTextView_shape_solidPressedColor; + } + + @Override + public int getSolidDisabledColorStyleable() { + return R.styleable.ShapeTextView_shape_solidDisabledColor; + } + + @Override + public int getSolidFocusedColorStyleable() { + return R.styleable.ShapeTextView_shape_solidFocusedColor; + } + + @Override + public int getSolidSelectedColorStyleable() { + return R.styleable.ShapeTextView_shape_solidSelectedColor; + } + + @Override + public int getRadiusStyleable() { + return R.styleable.ShapeTextView_shape_radius; + } + + @Override + public int getTopLeftRadiusStyleable() { + return R.styleable.ShapeTextView_shape_topLeftRadius; + } + + @Override + public int getTopRightRadiusStyleable() { + return R.styleable.ShapeTextView_shape_topRightRadius; + } + + @Override + public int getBottomLeftRadiusStyleable() { + return R.styleable.ShapeTextView_shape_bottomLeftRadius; + } + + @Override + public int getBottomRightRadiusStyleable() { + return R.styleable.ShapeTextView_shape_bottomRightRadius; + } + + @Override + public int getStartColorStyleable() { + return R.styleable.ShapeTextView_shape_startColor; + } + + @Override + public int getCenterColorStyleable() { + return R.styleable.ShapeTextView_shape_centerColor; + } + + @Override + public int getEndColorStyleable() { + return R.styleable.ShapeTextView_shape_endColor; + } + + @Override + public int getUseLevelStyleable() { + return R.styleable.ShapeTextView_shape_useLevel; + } + + @Override + public int getAngleStyleable() { + return R.styleable.ShapeTextView_shape_angle; + } + + @Override + public int getGradientTypeStyleable() { + return R.styleable.ShapeTextView_shape_gradientType; + } + + @Override + public int getCenterXStyleable() { + return R.styleable.ShapeTextView_shape_centerX; + } + + @Override + public int getCenterYStyleable() { + return R.styleable.ShapeTextView_shape_centerY; + } + + @Override + public int getGradientRadiusStyleable() { + return R.styleable.ShapeTextView_shape_gradientRadius; + } + + @Override + public int getStrokeColorStyleable() { + return R.styleable.ShapeTextView_shape_strokeColor; + } + + @Override + public int getStrokePressedColorStyleable() { + return R.styleable.ShapeTextView_shape_strokePressedColor; + } + + @Override + public int getStrokeDisabledColorStyleable() { + return R.styleable.ShapeTextView_shape_strokeDisabledColor; + } + + @Override + public int getStrokeFocusedColorStyleable() { + return R.styleable.ShapeTextView_shape_strokeFocusedColor; + } + + @Override + public int getStrokeSelectedColorStyleable() { + return R.styleable.ShapeTextView_shape_strokeSelectedColor; + } + + @Override + public int getStrokeWidthStyleable() { + return R.styleable.ShapeTextView_shape_strokeWidth; + } + + @Override + public int getDashWidthStyleable() { + return R.styleable.ShapeTextView_shape_dashWidth; + } + + @Override + public int getDashGapStyleable() { + return R.styleable.ShapeTextView_shape_dashGap; + } + + @Override + public int getInnerRadiusStyleable() { + return R.styleable.ShapeTextView_shape_innerRadius; + } + + @Override + public int getInnerRadiusRatioStyleable() { + return R.styleable.ShapeTextView_shape_innerRadiusRatio; + } + + @Override + public int getThicknessStyleable() { + return R.styleable.ShapeTextView_shape_thickness; + } + + @Override + public int getThicknessRatioStyleable() { + return R.styleable.ShapeTextView_shape_thicknessRatio; + } + + @Override + public int getShadowSizeStyleable() { + return R.styleable.ShapeTextView_shape_shadowSize; + } + + @Override + public int getShadowColorStyleable() { + return R.styleable.ShapeTextView_shape_shadowColor; + } + + @Override + public int getShadowOffsetXStyleable() { + return R.styleable.ShapeTextView_shape_shadowOffsetX; + } + + @Override + public int getShadowOffsetYStyleable() { + return R.styleable.ShapeTextView_shape_shadowOffsetY; + } + + /** + * {@link ITextColorStyleable} + */ + + @Override + public int getTextColorStyleable() { + return R.styleable.ShapeTextView_shape_textColor; + } + + @Override + public int getTextPressedColorStyleable() { + return R.styleable.ShapeTextView_shape_textPressedColor; + } + + @Override + public int getTextDisabledColorStyleable() { + return R.styleable.ShapeTextView_shape_textDisabledColor; + } + + @Override + public int getTextFocusedColorStyleable() { + return R.styleable.ShapeTextView_shape_textFocusedColor; + } + + @Override + public int getTextSelectedColorStyleable() { + return R.styleable.ShapeTextView_shape_textSelectedColor; + } + + @Override + public int getTextStartColorStyleable() { + return R.styleable.ShapeTextView_shape_textStartColor; + } + + @Override + public int getTextCenterColorStyleable() { + return R.styleable.ShapeTextView_shape_textCenterColor; + } + + @Override + public int getTextEndColorStyleable() { + return R.styleable.ShapeTextView_shape_textEndColor; + } + + @Override + public int getTextGradientOrientationStyleable() { + return R.styleable.ShapeTextView_shape_textGradientOrientation; + } +} \ No newline at end of file diff --git a/library/src/main/java/com/hjq/shape/styleable/ShapeViewStyleable.java b/library/src/main/java/com/hjq/shape/styleable/ShapeViewStyleable.java new file mode 100644 index 0000000..db097c0 --- /dev/null +++ b/library/src/main/java/com/hjq/shape/styleable/ShapeViewStyleable.java @@ -0,0 +1,206 @@ +package com.hjq.shape.styleable; + +import com.hjq.shape.R; + +/** + * author : Android 轮子哥 + * github : https://github.com/getActivity/ShapeView + * time : 2021/08/28 + * desc : View 的 Shape 属性值 + */ +public final class ShapeViewStyleable implements IShapeDrawableStyleable { + + /** + * {@link IShapeDrawableStyleable} + */ + + @Override + public int getShapeTypeStyleable() { + return R.styleable.ShapeView_shape; + } + + @Override + public int getShapeWidthStyleable() { + return R.styleable.ShapeView_shape_width; + } + + @Override + public int getShapeHeightStyleable() { + return R.styleable.ShapeView_shape_height; + } + + @Override + public int getSolidColorStyleable() { + return R.styleable.ShapeView_shape_solidColor; + } + + @Override + public int getSolidPressedColorStyleable() { + return R.styleable.ShapeView_shape_solidPressedColor; + } + + @Override + public int getSolidDisabledColorStyleable() { + return R.styleable.ShapeView_shape_solidDisabledColor; + } + + @Override + public int getSolidFocusedColorStyleable() { + return R.styleable.ShapeView_shape_solidFocusedColor; + } + + @Override + public int getSolidSelectedColorStyleable() { + return R.styleable.ShapeView_shape_solidSelectedColor; + } + + @Override + public int getRadiusStyleable() { + return R.styleable.ShapeView_shape_radius; + } + + @Override + public int getTopLeftRadiusStyleable() { + return R.styleable.ShapeView_shape_topLeftRadius; + } + + @Override + public int getTopRightRadiusStyleable() { + return R.styleable.ShapeView_shape_topRightRadius; + } + + @Override + public int getBottomLeftRadiusStyleable() { + return R.styleable.ShapeView_shape_bottomLeftRadius; + } + + @Override + public int getBottomRightRadiusStyleable() { + return R.styleable.ShapeView_shape_bottomRightRadius; + } + + @Override + public int getStartColorStyleable() { + return R.styleable.ShapeView_shape_startColor; + } + + @Override + public int getCenterColorStyleable() { + return R.styleable.ShapeView_shape_centerColor; + } + + @Override + public int getEndColorStyleable() { + return R.styleable.ShapeView_shape_endColor; + } + + @Override + public int getUseLevelStyleable() { + return R.styleable.ShapeView_shape_useLevel; + } + + @Override + public int getAngleStyleable() { + return R.styleable.ShapeView_shape_angle; + } + + @Override + public int getGradientTypeStyleable() { + return R.styleable.ShapeView_shape_gradientType; + } + + @Override + public int getCenterXStyleable() { + return R.styleable.ShapeView_shape_centerX; + } + + @Override + public int getCenterYStyleable() { + return R.styleable.ShapeView_shape_centerY; + } + + @Override + public int getGradientRadiusStyleable() { + return R.styleable.ShapeView_shape_gradientRadius; + } + + @Override + public int getStrokeColorStyleable() { + return R.styleable.ShapeView_shape_strokeColor; + } + + @Override + public int getStrokePressedColorStyleable() { + return R.styleable.ShapeView_shape_strokePressedColor; + } + + @Override + public int getStrokeDisabledColorStyleable() { + return R.styleable.ShapeView_shape_strokeDisabledColor; + } + + @Override + public int getStrokeFocusedColorStyleable() { + return R.styleable.ShapeView_shape_strokeFocusedColor; + } + + @Override + public int getStrokeSelectedColorStyleable() { + return R.styleable.ShapeView_shape_strokeSelectedColor; + } + + @Override + public int getStrokeWidthStyleable() { + return R.styleable.ShapeView_shape_strokeWidth; + } + + @Override + public int getDashWidthStyleable() { + return R.styleable.ShapeView_shape_dashWidth; + } + + @Override + public int getDashGapStyleable() { + return R.styleable.ShapeView_shape_dashGap; + } + + @Override + public int getInnerRadiusStyleable() { + return R.styleable.ShapeView_shape_innerRadius; + } + + @Override + public int getInnerRadiusRatioStyleable() { + return R.styleable.ShapeView_shape_innerRadiusRatio; + } + + @Override + public int getThicknessStyleable() { + return R.styleable.ShapeView_shape_thickness; + } + + @Override + public int getThicknessRatioStyleable() { + return R.styleable.ShapeView_shape_thicknessRatio; + } + + @Override + public int getShadowSizeStyleable() { + return R.styleable.ShapeView_shape_shadowSize; + } + + @Override + public int getShadowColorStyleable() { + return R.styleable.ShapeView_shape_shadowColor; + } + + @Override + public int getShadowOffsetXStyleable() { + return R.styleable.ShapeView_shape_shadowOffsetX; + } + + @Override + public int getShadowOffsetYStyleable() { + return R.styleable.ShapeView_shape_shadowOffsetY; + } +} \ No newline at end of file diff --git a/library/src/main/java/com/hjq/shape/view/ShapeButton.java b/library/src/main/java/com/hjq/shape/view/ShapeButton.java index 0aaa45c..9192356 100644 --- a/library/src/main/java/com/hjq/shape/view/ShapeButton.java +++ b/library/src/main/java/com/hjq/shape/view/ShapeButton.java @@ -2,14 +2,13 @@ import android.content.Context; import android.content.res.TypedArray; -import android.graphics.drawable.Drawable; import android.support.v7.widget.AppCompatButton; import android.util.AttributeSet; -import android.view.View; import com.hjq.shape.R; -import com.hjq.shape.core.IShapeDrawable; -import com.hjq.shape.core.IShapeTextColor; +import com.hjq.shape.builder.ShapeDrawableBuilder; +import com.hjq.shape.builder.TextColorBuilder; +import com.hjq.shape.styleable.ShapeButtonStyleable; /** * author : Android 轮子哥 @@ -17,65 +16,12 @@ * time : 2021/07/17 * desc : 支持直接定义 Shape 背景的 Button */ -public class ShapeButton extends AppCompatButton implements - IShapeDrawable, IShapeTextColor { +public class ShapeButton extends AppCompatButton { - private int mShapeType; - private int mShapeWidth; - private int mShapeHeight; + private static final ShapeButtonStyleable STYLEABLE = new ShapeButtonStyleable(); - private int mSolidColor; - private int mSolidPressedColor; - private int mSolidDisabledColor; - private int mSolidFocusedColor; - private int mSolidSelectedColor; - - private int mTopLeftRadius; - private int mTopRightRadius; - private int mBottomLeftRadius; - private int mBottomRightRadius; - - private int mStartColor; - private int mCenterColor; - private int mEndColor; - private boolean mUseLevel; - private int mAngle; - private int mGradientType; - private float mCenterX; - private float mCenterY; - private int mGradientRadius; - - private int mStrokeColor; - private int mStrokePressedColor; - private int mStrokeDisabledColor; - private int mStrokeFocusedColor; - private int mStrokeSelectedColor; - - private int mStrokeWidth; - private int mDashWidth; - private int mDashGap; - - private int mInnerRadius; - private float mInnerRadiusRatio; - private int mThickness; - private float mThicknessRatio; - - private int mShadowSize; - private int mShadowColor; - private int mShadowOffsetX; - private int mShadowOffsetY; - - private int mTextColor; - private int mTextPressedColor; - private int mTextCheckedColor; - private int mTextDisabledColor; - private int mTextFocusedColor; - private int mTextSelectedColor; - - private int mTextStartColor; - private int mTextCenterColor; - private int mTextEndColor; - private int mTextGradientOrientation; + private final ShapeDrawableBuilder mShapeDrawableBuilder; + private final TextColorBuilder mTextColorBuilder; public ShapeButton(Context context) { this(context, null); @@ -89,665 +35,43 @@ public ShapeButton(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.ShapeButton); - mShapeType = typedArray.getInt(R.styleable.ShapeButton_shape, DEFAULT_SHAPE_TYPE); - mShapeWidth = typedArray.getDimensionPixelSize(R.styleable.ShapeButton_shape_width, DEFAULT_SHAPE_WIDTH); - mShapeHeight = typedArray.getDimensionPixelSize(R.styleable.ShapeButton_shape_height, DEFAULT_SHAPE_HEIGHT); - - mSolidColor = typedArray.getColor(R.styleable.ShapeButton_shape_solidColor, DEFAULT_SHAPE_SOLID_COLOR); - mSolidPressedColor = typedArray.getColor(R.styleable.ShapeButton_shape_solidPressedColor, mSolidColor); - mSolidDisabledColor = typedArray.getColor(R.styleable.ShapeButton_shape_solidDisabledColor, mSolidColor); - mSolidFocusedColor = typedArray.getColor(R.styleable.ShapeButton_shape_solidFocusedColor, mSolidColor); - mSolidSelectedColor = typedArray.getColor(R.styleable.ShapeButton_shape_solidSelectedColor, mSolidColor); - - int radius = typedArray.getDimensionPixelSize(R.styleable.ShapeButton_shape_radius, DEFAULT_SHAPE_RADIUS); - mTopLeftRadius = typedArray.getDimensionPixelSize(R.styleable.ShapeButton_shape_topLeftRadius, radius); - mTopRightRadius = typedArray.getDimensionPixelSize(R.styleable.ShapeButton_shape_topRightRadius, radius); - mBottomLeftRadius = typedArray.getDimensionPixelSize(R.styleable.ShapeButton_shape_bottomLeftRadius, radius); - mBottomRightRadius = typedArray.getDimensionPixelSize(R.styleable.ShapeButton_shape_bottomRightRadius, radius); - - mStartColor = typedArray.getColor(R.styleable.ShapeButton_shape_startColor, mSolidColor); - mCenterColor = typedArray.getColor(R.styleable.ShapeButton_shape_centerColor, mSolidColor); - mEndColor = typedArray.getColor(R.styleable.ShapeButton_shape_endColor, mSolidColor); - mUseLevel = typedArray.getBoolean(R.styleable.ShapeButton_shape_useLevel, DEFAULT_SHAPE_USE_LEVEL); - mAngle = (int) typedArray.getFloat(R.styleable.ShapeButton_shape_angle, DEFAULT_SHAPE_ANGLE); - mGradientType = typedArray.getInt(R.styleable.ShapeButton_shape_gradientType, DEFAULT_SHAPE_GRADIENT_TYPE); - mCenterX = typedArray.getFloat(R.styleable.ShapeButton_shape_centerX, DEFAULT_SHAPE_CENTER_X); - mCenterY = typedArray.getFloat(R.styleable.ShapeButton_shape_centerY, DEFAULT_SHAPE_CENTER_Y); - mGradientRadius = typedArray.getDimensionPixelSize(R.styleable.ShapeButton_shape_gradientRadius, radius); - - mStrokeColor = typedArray.getColor(R.styleable.ShapeButton_shape_strokeColor, DEFAULT_SHAPE_STROKE_COLOR); - mStrokePressedColor = typedArray.getColor(R.styleable.ShapeButton_shape_strokePressedColor, mStrokeColor); - mStrokeDisabledColor = typedArray.getColor(R.styleable.ShapeButton_shape_strokeDisabledColor, mStrokeColor); - mStrokeFocusedColor = typedArray.getColor(R.styleable.ShapeButton_shape_strokeFocusedColor, mStrokeColor); - mStrokeSelectedColor = typedArray.getColor(R.styleable.ShapeButton_shape_strokeSelectedColor, mStrokeColor); - - mStrokeWidth = typedArray.getDimensionPixelSize(R.styleable.ShapeButton_shape_strokeWidth, DEFAULT_SHAPE_STROKE_WIDTH); - mDashWidth = typedArray.getDimensionPixelSize(R.styleable.ShapeButton_shape_dashWidth, DEFAULT_SHAPE_DASH_WIDTH); - mDashGap = typedArray.getDimensionPixelSize(R.styleable.ShapeButton_shape_dashGap, DEFAULT_SHAPE_DASH_GAP); - - mInnerRadius = typedArray.getDimensionPixelOffset(R.styleable.ShapeButton_shape_innerRadius, DEFAULT_SHAPE_INNER_RADIUS); - mInnerRadiusRatio = typedArray.getFloat(R.styleable.ShapeButton_shape_innerRadiusRatio, DEFAULT_SHAPE_INNER_RADIUS_RATIO); - mThickness = typedArray.getDimensionPixelOffset(R.styleable.ShapeButton_shape_thickness, DEFAULT_SHAPE_THICKNESS); - mThicknessRatio = typedArray.getFloat(R.styleable.ShapeButton_shape_thicknessRatio, DEFAULT_SHAPE_THICKNESS_RATIO); - - mShadowSize = typedArray.getDimensionPixelSize(R.styleable.ShapeButton_shape_shadowSize, DEFAULT_SHAPE_SHADOW_SIZE); - mShadowColor = typedArray.getColor(R.styleable.ShapeButton_shape_shadowColor, DEFAULT_SHAPE_SHADOW_COLOR); - mShadowOffsetX = typedArray.getDimensionPixelOffset(R.styleable.ShapeButton_shape_shadowOffsetX, DEFAULT_SHAPE_SHADOW_OFFSET_X); - mShadowOffsetY = typedArray.getDimensionPixelOffset(R.styleable.ShapeButton_shape_shadowOffsetY, DEFAULT_SHAPE_SHADOW_OFFSET_Y); - - mTextColor = typedArray.getColor(R.styleable.ShapeButton_shape_textColor, getTextColors().getDefaultColor()); - mTextPressedColor = typedArray.getColor(R.styleable.ShapeButton_shape_textPressedColor, getTextColors().getColorForState(new int[]{android.R.attr.state_pressed}, mTextColor)); - mTextDisabledColor = typedArray.getColor(R.styleable.ShapeButton_shape_textDisabledColor, getTextColors().getColorForState(new int[]{-android.R.attr.state_enabled}, mTextColor)); - mTextFocusedColor = typedArray.getColor(R.styleable.ShapeButton_shape_textFocusedColor, getTextColors().getColorForState(new int[]{android.R.attr.state_focused}, mTextColor)); - mTextSelectedColor = typedArray.getColor(R.styleable.ShapeButton_shape_textSelectedColor, getTextColors().getColorForState(new int[]{android.R.attr.state_selected}, mTextColor)); - - mTextStartColor = typedArray.getColor(R.styleable.ShapeButton_shape_textStartColor, mTextColor); - mTextCenterColor = typedArray.getColor(R.styleable.ShapeButton_shape_textCenterColor, mTextColor); - mTextEndColor = typedArray.getColor(R.styleable.ShapeButton_shape_textEndColor, mTextColor); - mTextGradientOrientation = typedArray.getColor(R.styleable.ShapeButton_shape_textGradientOrientation, SHAPE_TEXT_GRADIENT_TYPE_HORIZONTAL); - + mShapeDrawableBuilder = new ShapeDrawableBuilder(this, typedArray, STYLEABLE); + mTextColorBuilder = new TextColorBuilder(this, typedArray, STYLEABLE); typedArray.recycle(); - intoBackground(); + mShapeDrawableBuilder.intoBackground(); - if (isTextGradientColor()) { + if (mTextColorBuilder.isTextGradientColor()) { setText(getText()); } else { - intoTextColor(); + mTextColorBuilder.intoTextColor(); } } @Override public void setTextColor(int color) { super.setTextColor(color); - mTextColor = color; - - clearTextGradientColor(); - } - - /** - * {@link IShapeDrawable} - */ - - @Override - public ShapeButton setShapeType(int type) { - mShapeType = type; - return this; - } - - @Override - public int getShapeType() { - return mShapeType; - } - - @Override - public ShapeButton setShapeWidth(int width) { - mShapeWidth = width; - return this; - } - - @Override - public int getShapeWidth() { - return mShapeWidth; - } - - @Override - public ShapeButton setShapeHeight(int height) { - mShapeHeight = height; - return this; - } - - @Override - public int getShapeHeight() { - return mShapeHeight; - } - - @Override - public ShapeButton setSolidColor(int color) { - if (mSolidPressedColor == mSolidColor) { - mSolidPressedColor = color; - } - if (mSolidDisabledColor == mSolidColor) { - mSolidDisabledColor = color; - } - if (mSolidFocusedColor == mSolidColor) { - mSolidFocusedColor = color; - } - if (mSolidSelectedColor == mSolidColor) { - mSolidSelectedColor = color; - } - mSolidColor = color; - mStartColor = color; - mCenterColor = color; - mEndColor = color; - return this; - } - - @Override - public int getSolidColor() { - return mSolidColor; - } - - @Override - public ShapeButton setSolidPressedColor(int color) { - mSolidPressedColor = color; - return this; - } - - @Override - public int getSolidPressedColor() { - return mSolidPressedColor; - } - - @Override - public ShapeButton setSolidDisabledColor(int color) { - mSolidDisabledColor = color; - return this; - } - - @Override - public int getSolidDisabledColor() { - return mSolidDisabledColor; - } - - @Override - public ShapeButton setSolidFocusedColor(int color) { - mSolidFocusedColor = color; - return this; - } - - @Override - public int getSolidFocusedColor() { - return mSolidFocusedColor; - } - - @Override - public ShapeButton setSolidSelectedColor(int color) { - mSolidSelectedColor = color; - return this; - } - - @Override - public int getSolidSelectedColor() { - return mSolidSelectedColor; - } - - @Override - public ShapeButton setTopLeftRadius(int radius) { - mTopLeftRadius = radius; - return this; - } - - @Override - public int getTopLeftRadius() { - return mTopLeftRadius; - } - - @Override - public ShapeButton setTopRightRadius(int radius) { - mTopRightRadius = radius; - return this; - } - - @Override - public int getTopRightRadius() { - return mTopRightRadius; - } - - @Override - public ShapeButton setBottomLeftRadius(int radius) { - mBottomLeftRadius = radius; - return this; - } - - @Override - public int getBottomLeftRadius() { - return mBottomLeftRadius; - } - - @Override - public ShapeButton setBottomRightRadius(int radius) { - mBottomRightRadius = radius; - return this; - } - - @Override - public int getBottomRightRadius() { - return mBottomRightRadius; - } - - @Override - public ShapeButton setStartColor(int color) { - mStartColor = color; - return this; - } - - @Override - public int getStartColor() { - return mStartColor; - } - - @Override - public ShapeButton setCenterColor(int color) { - mCenterColor = color; - return this; - } - - @Override - public int getCenterColor() { - return mCenterColor; - } - - @Override - public ShapeButton setEndColor(int color) { - mEndColor = color; - return this; - } - - @Override - public int getEndColor() { - return mEndColor; - } - - @Override - public ShapeButton setUseLevel(boolean useLevel) { - mUseLevel = useLevel; - return this; - } - - @Override - public boolean isUseLevel() { - return mUseLevel; - } - - @Override - public ShapeButton setAngle(int angle) { - mAngle = angle; - return this; - } - - @Override - public int getAngle() { - return mAngle; - } - - @Override - public ShapeButton setGradientType(int type) { - mGradientType = type; - return this; - } - - @Override - public int getGradientType() { - return mGradientType; - } - - @Override - public ShapeButton setCenterX(float x) { - mCenterX = x; - return this; - } - - @Override - public float getCenterX() { - return mCenterX; - } - - @Override - public ShapeButton setCenterY(float y) { - mCenterY = y; - return this; - } - - @Override - public float getCenterY() { - return mCenterY; - } - - @Override - public ShapeButton setGradientRadius(int radius) { - mGradientRadius = radius; - return this; - } - - @Override - public int getGradientRadius() { - return mGradientRadius; - } - - @Override - public ShapeButton setStrokeColor(int color) { - if (mStrokePressedColor == mStrokeColor) { - mStrokePressedColor = color; - } - if (mStrokeDisabledColor == mStrokeColor) { - mStrokeDisabledColor = color; - } - if (mStrokeFocusedColor == mStrokeColor) { - mStrokeFocusedColor = color; - } - if (mStrokeSelectedColor == mStrokeColor) { - mStrokeSelectedColor = color; - } - mStrokeColor = color; - return this; - } - - @Override - public int getStrokeColor() { - return mStrokeColor; - } - - @Override - public ShapeButton setStrokePressedColor(int color) { - mStrokePressedColor = color; - return this; - } - - @Override - public int getStrokePressedColor() { - return mStrokePressedColor; - } - - @Override - public ShapeButton setStrokeDisabledColor(int color) { - mStrokeDisabledColor = color; - return this; - } - - @Override - public int getStrokeDisabledColor() { - return mStrokeDisabledColor; - } - - @Override - public ShapeButton setStrokeFocusedColor(int color) { - mStrokeFocusedColor = color; - return this; - } - - @Override - public int getStrokeFocusedColor() { - return mStrokeFocusedColor; - } - - @Override - public ShapeButton setStrokeSelectedColor(int color) { - mStrokeSelectedColor = color; - return this; - } - - @Override - public int getStrokeSelectedColor() { - return mStrokeSelectedColor; - } - - @Override - public ShapeButton setStrokeWidth(int width) { - mStrokeWidth = width; - return this; - } - - @Override - public int getStrokeWidth() { - return mStrokeWidth; - } - - @Override - public ShapeButton setDashWidth(int width) { - mDashWidth = width; - return this; - } - - @Override - public int getDashWidth() { - return mDashWidth; - } - - @Override - public ShapeButton setDashGap(int gap) { - mDashGap = gap; - return this; - } - - @Override - public int getDashGap() { - return mDashGap; - } - - @Override - public ShapeButton setInnerRadius(int radius) { - mInnerRadius = radius; - return this; - } - - @Override - public int getInnerRadius() { - return mInnerRadius; - } - - @Override - public ShapeButton setInnerRadiusRatio(float ratio) { - mInnerRadiusRatio = ratio; - return this; - } - - @Override - public float getInnerRadiusRatio() { - return mInnerRadiusRatio; - } - - @Override - public ShapeButton setThickness(int size) { - mThickness = size; - return this; - } - - @Override - public int getThickness() { - return mThickness; - } - - @Override - public ShapeButton setThicknessRatio(float ratio) { - mThicknessRatio = ratio; - return this; - } - - @Override - public float getThicknessRatio() { - return mThicknessRatio; - } - - @Override - public ShapeButton setShadowSize(int size) { - mShadowSize = size; - return this; - } - - @Override - public int getShadowSize() { - return mShadowSize; - } - - @Override - public ShapeButton setShadowColor(int color) { - mShadowColor = color; - return this; - } - - @Override - public int getShadowColor() { - return mShadowColor; - } - - /** - * 获取文本阴影颜色(避免和 TextView 原有的方法冲突) - */ - public int getTextShadowColor() { - return super.getShadowColor(); - } - - @Override - public ShapeButton setShadowOffsetX(int offsetX) { - mShadowOffsetX = offsetX; - return this; - } - - @Override - public int getShadowOffsetX() { - return mShadowOffsetX; - } - - @Override - public ShapeButton setShadowOffsetY(int offsetY) { - mShadowOffsetY = offsetY; - return this; - } - - @Override - public int getShadowOffsetY() { - return mShadowOffsetY; - } - - @Override - public void intoBackground() { - Drawable drawable = buildBackgroundDrawable(); - if (drawable == null) { + if (mTextColorBuilder == null) { return; } - if (isDashLineEnable() || isShadowEnable()) { - // 需要关闭硬件加速,否则虚线或者阴影在某些手机上面无法生效 - setLayerType(View.LAYER_TYPE_SOFTWARE, null); - } - setBackground(drawable); - } - - /** - * {@link IShapeTextColor} - */ - - @Override - public ShapeButton setNormalTextColor(int color) { - mTextColor = color; - return this; - } - - @Override - public int getNormalTextColor() { - return mTextColor; - } - - @Override - public ShapeButton setTextPressedColor(int color) { - mTextPressedColor = color; - return this; - } - - @Override - public int getTextPressedColor() { - return mTextPressedColor; - } - - @Override - public ShapeButton setTextCheckedColor(int color) { - mTextCheckedColor = color; - return this; - } - - @Override - public int getTextCheckedColor() { - return mTextCheckedColor; - } - - @Override - public ShapeButton setTextDisabledColor(int color) { - mTextDisabledColor = color; - return this; - } - - @Override - public int getTextDisabledColor() { - return mTextDisabledColor; - } - - @Override - public ShapeButton setTextFocusedColor(int color) { - mTextFocusedColor = color; - return this; - } - - @Override - public int getTextFocusedColor() { - return mTextFocusedColor; - } - - @Override - public ShapeButton setTextSelectedColor(int color) { - mTextSelectedColor = color; - return this; - } - - @Override - public int getTextSelectedColor() { - return mTextSelectedColor; - } - - @Override - public void intoTextColor() { - setTextColor(buildColorState()); - } - - @Override - public ShapeButton setTextStartColor(int color) { - mTextStartColor = color; - return this; - } - - @Override - public int getTextStartColor() { - return mTextStartColor; - } - - @Override - public ShapeButton setTextCenterColor(int color) { - mTextCenterColor = color; - return this; - } - - @Override - public int getTextCenterColor() { - return mTextCenterColor; - } - - @Override - public ShapeButton setTextEndColor(int color) { - mTextEndColor = color; - return this; - } - - @Override - public int getTextEndColor() { - return mTextEndColor; - } - - @Override - public ShapeButton setTextGradientOrientation(int orientation) { - mTextGradientOrientation = orientation; - return this; - } - - @Override - public int getTextGradientOrientation() { - return mTextGradientOrientation; + mTextColorBuilder.setTextColor(color); + mTextColorBuilder.clearTextGradientColor(); } @Override public void setText(CharSequence text, BufferType type) { - if (isTextGradientColor()) { - super.setText(buildLinearGradientSpannable(text), type); + if (mTextColorBuilder != null && mTextColorBuilder.isTextGradientColor()) { + super.setText(mTextColorBuilder.buildLinearGradientSpannable(text), type); } else { super.setText(text, type); } } + + public ShapeDrawableBuilder getShapeDrawableBuilder() { + return mShapeDrawableBuilder; + } + + public TextColorBuilder getTextColorBuilder() { + return mTextColorBuilder; + } } \ No newline at end of file diff --git a/library/src/main/java/com/hjq/shape/view/ShapeCheckBox.java b/library/src/main/java/com/hjq/shape/view/ShapeCheckBox.java index adc56be..bd42de7 100644 --- a/library/src/main/java/com/hjq/shape/view/ShapeCheckBox.java +++ b/library/src/main/java/com/hjq/shape/view/ShapeCheckBox.java @@ -5,11 +5,12 @@ import android.graphics.drawable.Drawable; import android.support.v7.widget.AppCompatCheckBox; import android.util.AttributeSet; -import android.view.View; import com.hjq.shape.R; -import com.hjq.shape.core.IShapeDrawable; -import com.hjq.shape.core.IShapeTextColor; +import com.hjq.shape.builder.ButtonDrawableBuilder; +import com.hjq.shape.builder.ShapeDrawableBuilder; +import com.hjq.shape.builder.TextColorBuilder; +import com.hjq.shape.styleable.ShapeCheckBoxStyleable; /** * author : Android 轮子哥 @@ -17,67 +18,13 @@ * time : 2021/07/17 * desc : 支持直接定义 Shape 背景的 CheckBox */ -public class ShapeCheckBox extends AppCompatCheckBox implements - IShapeDrawable, IShapeTextColor { +public class ShapeCheckBox extends AppCompatCheckBox { - private int mShapeType; - private int mShapeWidth; - private int mShapeHeight; + private static final ShapeCheckBoxStyleable STYLEABLE = new ShapeCheckBoxStyleable(); - private int mSolidColor; - private int mSolidPressedColor; - private int mSolidCheckedColor; - private int mSolidDisabledColor; - private int mSolidFocusedColor; - private int mSolidSelectedColor; - - private int mTopLeftRadius; - private int mTopRightRadius; - private int mBottomLeftRadius; - private int mBottomRightRadius; - - private int mStartColor; - private int mCenterColor; - private int mEndColor; - private boolean mUseLevel; - private int mAngle; - private int mGradientType; - private float mCenterX; - private float mCenterY; - private int mGradientRadius; - - private int mStrokeColor; - private int mStrokePressedColor; - private int mStrokeCheckedColor; - private int mStrokeDisabledColor; - private int mStrokeFocusedColor; - private int mStrokeSelectedColor; - - private int mStrokeWidth; - private int mDashWidth; - private int mDashGap; - - private int mInnerRadius; - private float mInnerRadiusRatio; - private int mThickness; - private float mThicknessRatio; - - private int mShadowSize; - private int mShadowColor; - private int mShadowOffsetX; - private int mShadowOffsetY; - - private int mTextColor; - private int mTextPressedColor; - private int mTextCheckedColor; - private int mTextDisabledColor; - private int mTextFocusedColor; - private int mTextSelectedColor; - - private int mTextStartColor; - private int mTextCenterColor; - private int mTextEndColor; - private int mTextGradientOrientation; + private final ShapeDrawableBuilder mShapeDrawableBuilder; + private final TextColorBuilder mTextColorBuilder; + private final ButtonDrawableBuilder mButtonDrawableBuilder; public ShapeCheckBox(Context context) { this(context, null); @@ -91,696 +38,59 @@ public ShapeCheckBox(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.ShapeCheckBox); - mShapeType = typedArray.getInt(R.styleable.ShapeCheckBox_shape, DEFAULT_SHAPE_TYPE); - mShapeWidth = typedArray.getDimensionPixelSize(R.styleable.ShapeCheckBox_shape_width, DEFAULT_SHAPE_WIDTH); - mShapeHeight = typedArray.getDimensionPixelSize(R.styleable.ShapeCheckBox_shape_height, DEFAULT_SHAPE_HEIGHT); - - mSolidColor = typedArray.getColor(R.styleable.ShapeCheckBox_shape_solidColor, DEFAULT_SHAPE_SOLID_COLOR); - mSolidPressedColor = typedArray.getColor(R.styleable.ShapeCheckBox_shape_solidPressedColor, mSolidColor); - mSolidCheckedColor = typedArray.getColor(R.styleable.ShapeCheckBox_shape_solidCheckedColor, mSolidColor); - mSolidDisabledColor = typedArray.getColor(R.styleable.ShapeCheckBox_shape_solidDisabledColor, mSolidColor); - mSolidFocusedColor = typedArray.getColor(R.styleable.ShapeCheckBox_shape_solidFocusedColor, mSolidColor); - mSolidSelectedColor = typedArray.getColor(R.styleable.ShapeCheckBox_shape_solidSelectedColor, mSolidColor); - - int radius = typedArray.getDimensionPixelSize(R.styleable.ShapeCheckBox_shape_radius, DEFAULT_SHAPE_RADIUS); - mTopLeftRadius = typedArray.getDimensionPixelSize(R.styleable.ShapeCheckBox_shape_topLeftRadius, radius); - mTopRightRadius = typedArray.getDimensionPixelSize(R.styleable.ShapeCheckBox_shape_topRightRadius, radius); - mBottomLeftRadius = typedArray.getDimensionPixelSize(R.styleable.ShapeCheckBox_shape_bottomLeftRadius, radius); - mBottomRightRadius = typedArray.getDimensionPixelSize(R.styleable.ShapeCheckBox_shape_bottomRightRadius, radius); - - mStartColor = typedArray.getColor(R.styleable.ShapeCheckBox_shape_startColor, mSolidColor); - mCenterColor = typedArray.getColor(R.styleable.ShapeCheckBox_shape_centerColor, mSolidColor); - mEndColor = typedArray.getColor(R.styleable.ShapeCheckBox_shape_endColor, mSolidColor); - mUseLevel = typedArray.getBoolean(R.styleable.ShapeCheckBox_shape_useLevel, DEFAULT_SHAPE_USE_LEVEL); - mAngle = (int) typedArray.getFloat(R.styleable.ShapeCheckBox_shape_angle, DEFAULT_SHAPE_ANGLE); - mGradientType = typedArray.getInt(R.styleable.ShapeCheckBox_shape_gradientType, DEFAULT_SHAPE_GRADIENT_TYPE); - mCenterX = typedArray.getFloat(R.styleable.ShapeCheckBox_shape_centerX, DEFAULT_SHAPE_CENTER_X); - mCenterY = typedArray.getFloat(R.styleable.ShapeCheckBox_shape_centerY, DEFAULT_SHAPE_CENTER_Y); - mGradientRadius = typedArray.getDimensionPixelSize(R.styleable.ShapeCheckBox_shape_gradientRadius, radius); - - mStrokeColor = typedArray.getColor(R.styleable.ShapeCheckBox_shape_strokeColor, DEFAULT_SHAPE_STROKE_COLOR); - mStrokePressedColor = typedArray.getColor(R.styleable.ShapeCheckBox_shape_strokePressedColor, mStrokeColor); - mStrokeCheckedColor = typedArray.getColor(R.styleable.ShapeCheckBox_shape_strokeCheckedColor, mStrokeColor); - mStrokeDisabledColor = typedArray.getColor(R.styleable.ShapeCheckBox_shape_strokeDisabledColor, mStrokeColor); - mStrokeFocusedColor = typedArray.getColor(R.styleable.ShapeCheckBox_shape_strokeFocusedColor, mStrokeColor); - mStrokeSelectedColor = typedArray.getColor(R.styleable.ShapeCheckBox_shape_strokeSelectedColor, mStrokeColor); - - mStrokeWidth = typedArray.getDimensionPixelSize(R.styleable.ShapeCheckBox_shape_strokeWidth, DEFAULT_SHAPE_STROKE_WIDTH); - mDashWidth = typedArray.getDimensionPixelSize(R.styleable.ShapeCheckBox_shape_dashWidth, DEFAULT_SHAPE_DASH_WIDTH); - mDashGap = typedArray.getDimensionPixelSize(R.styleable.ShapeCheckBox_shape_dashGap, DEFAULT_SHAPE_DASH_GAP); - - mInnerRadius = typedArray.getDimensionPixelOffset(R.styleable.ShapeCheckBox_shape_innerRadius, DEFAULT_SHAPE_INNER_RADIUS); - mInnerRadiusRatio = typedArray.getFloat(R.styleable.ShapeCheckBox_shape_innerRadiusRatio, DEFAULT_SHAPE_INNER_RADIUS_RATIO); - mThickness = typedArray.getDimensionPixelOffset(R.styleable.ShapeCheckBox_shape_thickness, DEFAULT_SHAPE_THICKNESS); - mThicknessRatio = typedArray.getFloat(R.styleable.ShapeCheckBox_shape_thicknessRatio, DEFAULT_SHAPE_THICKNESS_RATIO); - - mShadowSize = typedArray.getDimensionPixelSize(R.styleable.ShapeCheckBox_shape_shadowSize, DEFAULT_SHAPE_SHADOW_SIZE); - mShadowColor = typedArray.getColor(R.styleable.ShapeCheckBox_shape_shadowColor, DEFAULT_SHAPE_SHADOW_COLOR); - mShadowOffsetX = typedArray.getDimensionPixelOffset(R.styleable.ShapeCheckBox_shape_shadowOffsetX, DEFAULT_SHAPE_SHADOW_OFFSET_X); - mShadowOffsetY = typedArray.getDimensionPixelOffset(R.styleable.ShapeCheckBox_shape_shadowOffsetY, DEFAULT_SHAPE_SHADOW_OFFSET_Y); - - mTextColor = typedArray.getColor(R.styleable.ShapeCheckBox_shape_textColor, getTextColors().getDefaultColor()); - mTextPressedColor = typedArray.getColor(R.styleable.ShapeCheckBox_shape_textPressedColor, getTextColors().getColorForState(new int[]{android.R.attr.state_pressed}, mTextColor)); - mTextCheckedColor = typedArray.getColor(R.styleable.ShapeCheckBox_shape_textCheckedColor, getTextColors().getColorForState(new int[]{android.R.attr.state_checked}, mTextColor)); - mTextDisabledColor = typedArray.getColor(R.styleable.ShapeCheckBox_shape_textDisabledColor, getTextColors().getColorForState(new int[]{-android.R.attr.state_enabled}, mTextColor)); - mTextFocusedColor = typedArray.getColor(R.styleable.ShapeCheckBox_shape_textFocusedColor, getTextColors().getColorForState(new int[]{android.R.attr.state_focused}, mTextColor)); - mTextSelectedColor = typedArray.getColor(R.styleable.ShapeCheckBox_shape_textSelectedColor, getTextColors().getColorForState(new int[]{android.R.attr.state_selected}, mTextColor)); - - mTextStartColor = typedArray.getColor(R.styleable.ShapeCheckBox_shape_textStartColor, mTextColor); - mTextCenterColor = typedArray.getColor(R.styleable.ShapeCheckBox_shape_textCenterColor, mTextColor); - mTextEndColor = typedArray.getColor(R.styleable.ShapeCheckBox_shape_textEndColor, mTextColor); - mTextGradientOrientation = typedArray.getColor(R.styleable.ShapeCheckBox_shape_textGradientOrientation, SHAPE_TEXT_GRADIENT_TYPE_HORIZONTAL); - + mShapeDrawableBuilder = new ShapeDrawableBuilder(this, typedArray, STYLEABLE); + mTextColorBuilder = new TextColorBuilder(this, typedArray, STYLEABLE); + mButtonDrawableBuilder = new ButtonDrawableBuilder(this, typedArray, STYLEABLE); typedArray.recycle(); - intoBackground(); + mShapeDrawableBuilder.intoBackground(); - if (isTextGradientColor()) { + if (mTextColorBuilder.isTextGradientColor()) { setText(getText()); } else { - intoTextColor(); + mTextColorBuilder.intoTextColor(); } + + mButtonDrawableBuilder.intoButtonDrawable(); } @Override public void setTextColor(int color) { super.setTextColor(color); - mTextColor = color; - - clearTextGradientColor(); - } - - /** - * {@link IShapeDrawable} - */ - - @Override - public ShapeCheckBox setShapeType(int type) { - mShapeType = type; - return this; - } - - @Override - public int getShapeType() { - return mShapeType; - } - - @Override - public ShapeCheckBox setShapeWidth(int width) { - mShapeWidth = width; - return this; - } - - @Override - public int getShapeWidth() { - return mShapeWidth; - } - - @Override - public ShapeCheckBox setShapeHeight(int height) { - mShapeHeight = height; - return this; - } - - @Override - public int getShapeHeight() { - return mShapeHeight; - } - - @Override - public ShapeCheckBox setSolidColor(int color) { - if (mSolidPressedColor == mSolidColor) { - mSolidPressedColor = color; - } - if (mSolidCheckedColor == mSolidColor) { - mSolidCheckedColor = color; - } - if (mSolidDisabledColor == mSolidColor) { - mSolidDisabledColor = color; - } - if (mSolidFocusedColor == mSolidColor) { - mSolidFocusedColor = color; - } - if (mSolidSelectedColor == mSolidColor) { - mSolidSelectedColor = color; + if (mTextColorBuilder == null) { + return; } - mSolidColor = color; - mStartColor = color; - mCenterColor = color; - mEndColor = color; - return this; - } - - @Override - public int getSolidColor() { - return mSolidColor; - } - - @Override - public ShapeCheckBox setSolidPressedColor(int color) { - mSolidPressedColor = color; - return this; - } - - @Override - public int getSolidPressedColor() { - return mSolidPressedColor; - } - - @Override - public ShapeCheckBox setSolidCheckedColor(int color) { - mSolidCheckedColor = color; - return this; - } - - @Override - public int getSolidCheckedColor() { - return mSolidCheckedColor; - } - - @Override - public ShapeCheckBox setSolidDisabledColor(int color) { - mSolidDisabledColor = color; - return this; - } - - @Override - public int getSolidDisabledColor() { - return mSolidDisabledColor; - } - - @Override - public ShapeCheckBox setSolidFocusedColor(int color) { - mSolidFocusedColor = color; - return this; - } - - @Override - public int getSolidFocusedColor() { - return mSolidFocusedColor; - } - - @Override - public ShapeCheckBox setSolidSelectedColor(int color) { - mSolidSelectedColor = color; - return this; - } - - @Override - public int getSolidSelectedColor() { - return mSolidSelectedColor; - } - - @Override - public ShapeCheckBox setTopLeftRadius(int radius) { - mTopLeftRadius = radius; - return this; + mTextColorBuilder.setTextColor(color); + mTextColorBuilder.clearTextGradientColor(); } @Override - public int getTopLeftRadius() { - return mTopLeftRadius; - } - - @Override - public ShapeCheckBox setTopRightRadius(int radius) { - mTopRightRadius = radius; - return this; - } - - @Override - public int getTopRightRadius() { - return mTopRightRadius; - } - - @Override - public ShapeCheckBox setBottomLeftRadius(int radius) { - mBottomLeftRadius = radius; - return this; - } - - @Override - public int getBottomLeftRadius() { - return mBottomLeftRadius; - } - - @Override - public ShapeCheckBox setBottomRightRadius(int radius) { - mBottomRightRadius = radius; - return this; - } - - @Override - public int getBottomRightRadius() { - return mBottomRightRadius; - } - - @Override - public ShapeCheckBox setStartColor(int color) { - mStartColor = color; - return this; - } - - @Override - public int getStartColor() { - return mStartColor; - } - - @Override - public ShapeCheckBox setCenterColor(int color) { - mCenterColor = color; - return this; - } - - @Override - public int getCenterColor() { - return mCenterColor; - } - - @Override - public ShapeCheckBox setEndColor(int color) { - mEndColor = color; - return this; - } - - @Override - public int getEndColor() { - return mEndColor; - } - - @Override - public ShapeCheckBox setUseLevel(boolean useLevel) { - mUseLevel = useLevel; - return this; - } - - @Override - public boolean isUseLevel() { - return mUseLevel; - } - - @Override - public ShapeCheckBox setAngle(int angle) { - mAngle = angle; - return this; - } - - @Override - public int getAngle() { - return mAngle; - } - - @Override - public ShapeCheckBox setGradientType(int type) { - mGradientType = type; - return this; - } - - @Override - public int getGradientType() { - return mGradientType; - } - - @Override - public ShapeCheckBox setCenterX(float x) { - mCenterX = x; - return this; - } - - @Override - public float getCenterX() { - return mCenterX; - } - - @Override - public ShapeCheckBox setCenterY(float y) { - mCenterY = y; - return this; - } - - @Override - public float getCenterY() { - return mCenterY; - } - - @Override - public ShapeCheckBox setGradientRadius(int radius) { - mGradientRadius = radius; - return this; - } - - @Override - public int getGradientRadius() { - return mGradientRadius; - } - - @Override - public ShapeCheckBox setStrokeColor(int color) { - if (mStrokePressedColor == mStrokeColor) { - mStrokePressedColor = color; - } - if (mStrokeCheckedColor == mStrokeColor) { - mStrokeCheckedColor = color; - } - if (mStrokeDisabledColor == mStrokeColor) { - mStrokeDisabledColor = color; - } - if (mStrokeFocusedColor == mStrokeColor) { - mStrokeFocusedColor = color; - } - if (mStrokeSelectedColor == mStrokeColor) { - mStrokeSelectedColor = color; + public void setText(CharSequence text, BufferType type) { + if (mTextColorBuilder != null && mTextColorBuilder.isTextGradientColor()) { + super.setText(mTextColorBuilder.buildLinearGradientSpannable(text), type); + } else { + super.setText(text, type); } - mStrokeColor = color; - return this; - } - - @Override - public int getStrokeColor() { - return mStrokeColor; - } - - @Override - public ShapeCheckBox setStrokePressedColor(int color) { - mStrokePressedColor = color; - return this; - } - - @Override - public int getStrokePressedColor() { - return mStrokePressedColor; - } - - @Override - public ShapeCheckBox setStrokeCheckedColor(int color) { - mStrokeCheckedColor = color; - return this; - } - - @Override - public int getStrokeCheckedColor() { - return mStrokeCheckedColor; - } - - @Override - public ShapeCheckBox setStrokeDisabledColor(int color) { - mStrokeDisabledColor = color; - return this; - } - - @Override - public int getStrokeDisabledColor() { - return mStrokeDisabledColor; - } - - @Override - public ShapeCheckBox setStrokeFocusedColor(int color) { - mStrokeFocusedColor = color; - return this; - } - - @Override - public int getStrokeFocusedColor() { - return mStrokeFocusedColor; - } - - @Override - public ShapeCheckBox setStrokeSelectedColor(int color) { - mStrokeSelectedColor = color; - return this; - } - - @Override - public int getStrokeSelectedColor() { - return mStrokeSelectedColor; - } - - @Override - public ShapeCheckBox setStrokeWidth(int width) { - mStrokeWidth = width; - return this; - } - - @Override - public int getStrokeWidth() { - return mStrokeWidth; - } - - @Override - public ShapeCheckBox setDashWidth(int width) { - mDashWidth = width; - return this; - } - - @Override - public int getDashWidth() { - return mDashWidth; - } - - @Override - public ShapeCheckBox setDashGap(int gap) { - mDashGap = gap; - return this; - } - - @Override - public int getDashGap() { - return mDashGap; - } - - @Override - public ShapeCheckBox setInnerRadius(int radius) { - mInnerRadius = radius; - return this; - } - - @Override - public int getInnerRadius() { - return mInnerRadius; - } - - @Override - public ShapeCheckBox setInnerRadiusRatio(float ratio) { - mInnerRadiusRatio = ratio; - return this; - } - - @Override - public float getInnerRadiusRatio() { - return mInnerRadiusRatio; - } - - @Override - public ShapeCheckBox setThickness(int size) { - mThickness = size; - return this; - } - - @Override - public int getThickness() { - return mThickness; - } - - @Override - public ShapeCheckBox setThicknessRatio(float ratio) { - mThicknessRatio = ratio; - return this; - } - - @Override - public float getThicknessRatio() { - return mThicknessRatio; - } - - @Override - public ShapeCheckBox setShadowSize(int size) { - mShadowSize = size; - return this; - } - - @Override - public int getShadowSize() { - return mShadowSize; - } - - @Override - public ShapeCheckBox setShadowColor(int color) { - mShadowColor = color; - return this; } @Override - public int getShadowColor() { - return mShadowColor; - } - - /** - * 获取文本阴影颜色(避免和 TextView 原有的方法冲突) - */ - public int getTextShadowColor() { - return super.getShadowColor(); - } - - @Override - public ShapeCheckBox setShadowOffsetX(int offsetX) { - mShadowOffsetX = offsetX; - return this; - } - - @Override - public int getShadowOffsetX() { - return mShadowOffsetX; - } - - @Override - public ShapeCheckBox setShadowOffsetY(int offsetY) { - mShadowOffsetY = offsetY; - return this; - } - - @Override - public int getShadowOffsetY() { - return mShadowOffsetY; - } - - @Override - public void intoBackground() { - Drawable drawable = buildBackgroundDrawable(); - if (drawable == null) { + public void setButtonDrawable(Drawable drawable) { + super.setButtonDrawable(drawable); + if (mButtonDrawableBuilder == null) { return; } - if (isDashLineEnable() || isShadowEnable()) { - // 需要关闭硬件加速,否则虚线或者阴影在某些手机上面无法生效 - setLayerType(View.LAYER_TYPE_SOFTWARE, null); - } - setBackground(drawable); - } - - /** - * {@link IShapeTextColor} - */ - - @Override - public ShapeCheckBox setNormalTextColor(int color) { - mTextColor = color; - return this; - } - - @Override - public int getNormalTextColor() { - return mTextColor; + mButtonDrawableBuilder.setButtonDrawable(drawable); } - @Override - public ShapeCheckBox setTextPressedColor(int color) { - mTextPressedColor = color; - return this; - } - - @Override - public int getTextPressedColor() { - return mTextPressedColor; - } - - @Override - public ShapeCheckBox setTextCheckedColor(int color) { - mTextCheckedColor = color; - return this; + public ShapeDrawableBuilder getShapeDrawableBuilder() { + return mShapeDrawableBuilder; } - @Override - public int getTextCheckedColor() { - return mTextCheckedColor; - } - - @Override - public ShapeCheckBox setTextDisabledColor(int color) { - mTextDisabledColor = color; - return this; - } - - @Override - public int getTextDisabledColor() { - return mTextDisabledColor; - } - - @Override - public ShapeCheckBox setTextFocusedColor(int color) { - mTextFocusedColor = color; - return this; + public TextColorBuilder getTextColorBuilder() { + return mTextColorBuilder; } - @Override - public int getTextFocusedColor() { - return mTextFocusedColor; - } - - @Override - public ShapeCheckBox setTextSelectedColor(int color) { - mTextSelectedColor = color; - return this; - } - - @Override - public int getTextSelectedColor() { - return mTextSelectedColor; - } - - @Override - public void intoTextColor() { - setTextColor(buildColorState()); - } - - @Override - public ShapeCheckBox setTextStartColor(int color) { - mTextStartColor = color; - return this; - } - - @Override - public int getTextStartColor() { - return mTextStartColor; - } - - @Override - public ShapeCheckBox setTextCenterColor(int color) { - mTextCenterColor = color; - return this; - } - - @Override - public int getTextCenterColor() { - return mTextCenterColor; - } - - @Override - public ShapeCheckBox setTextEndColor(int color) { - mTextEndColor = color; - return this; - } - - @Override - public int getTextEndColor() { - return mTextEndColor; - } - - @Override - public ShapeCheckBox setTextGradientOrientation(int orientation) { - mTextGradientOrientation = orientation; - return this; - } - - @Override - public int getTextGradientOrientation() { - return mTextGradientOrientation; - } - - @Override - public void setText(CharSequence text, BufferType type) { - if (isTextGradientColor()) { - super.setText(buildLinearGradientSpannable(text), type); - } else { - super.setText(text, type); - } + public ButtonDrawableBuilder getButtonDrawableBuilder() { + return mButtonDrawableBuilder; } } \ No newline at end of file diff --git a/library/src/main/java/com/hjq/shape/view/ShapeEditText.java b/library/src/main/java/com/hjq/shape/view/ShapeEditText.java index d4e003e..d6fc839 100644 --- a/library/src/main/java/com/hjq/shape/view/ShapeEditText.java +++ b/library/src/main/java/com/hjq/shape/view/ShapeEditText.java @@ -2,14 +2,13 @@ import android.content.Context; import android.content.res.TypedArray; -import android.graphics.drawable.Drawable; import android.support.v7.widget.AppCompatEditText; import android.util.AttributeSet; -import android.view.View; import com.hjq.shape.R; -import com.hjq.shape.core.IShapeDrawable; -import com.hjq.shape.core.IShapeTextColor; +import com.hjq.shape.builder.ShapeDrawableBuilder; +import com.hjq.shape.builder.TextColorBuilder; +import com.hjq.shape.styleable.ShapeEditTextStyleable; /** * author : Android 轮子哥 @@ -17,64 +16,12 @@ * time : 2021/07/17 * desc : 支持直接定义 Shape 背景的 EditText */ -public class ShapeEditText extends AppCompatEditText implements - IShapeDrawable, IShapeTextColor { +public class ShapeEditText extends AppCompatEditText { - private int mShapeType; - private int mShapeWidth; - private int mShapeHeight; + private static final ShapeEditTextStyleable STYLEABLE = new ShapeEditTextStyleable(); - private int mSolidColor; - private int mSolidPressedColor; - private int mSolidDisabledColor; - private int mSolidFocusedColor; - private int mSolidSelectedColor; - - private int mTopLeftRadius; - private int mTopRightRadius; - private int mBottomLeftRadius; - private int mBottomRightRadius; - - private int mStartColor; - private int mCenterColor; - private int mEndColor; - private boolean mUseLevel; - private int mAngle; - private int mGradientType; - private float mCenterX; - private float mCenterY; - private int mGradientRadius; - - private int mStrokeColor; - private int mStrokePressedColor; - private int mStrokeDisabledColor; - private int mStrokeFocusedColor; - private int mStrokeSelectedColor; - - private int mStrokeWidth; - private int mDashWidth; - private int mDashGap; - - private int mInnerRadius; - private float mInnerRadiusRatio; - private int mThickness; - private float mThicknessRatio; - - private int mShadowSize; - private int mShadowColor; - private int mShadowOffsetX; - private int mShadowOffsetY; - - private int mTextColor; - private int mTextPressedColor; - private int mTextDisabledColor; - private int mTextFocusedColor; - private int mTextSelectedColor; - - private int mTextStartColor; - private int mTextCenterColor; - private int mTextEndColor; - private int mTextGradientOrientation; + private final ShapeDrawableBuilder mShapeDrawableBuilder; + private final TextColorBuilder mTextColorBuilder; public ShapeEditText(Context context) { this(context, null); @@ -88,654 +35,43 @@ public ShapeEditText(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.ShapeEditText); - mShapeType = typedArray.getInt(R.styleable.ShapeEditText_shape, DEFAULT_SHAPE_TYPE); - mShapeWidth = typedArray.getDimensionPixelSize(R.styleable.ShapeEditText_shape_width, DEFAULT_SHAPE_WIDTH); - mShapeHeight = typedArray.getDimensionPixelSize(R.styleable.ShapeEditText_shape_height, DEFAULT_SHAPE_HEIGHT); - - mSolidColor = typedArray.getColor(R.styleable.ShapeEditText_shape_solidColor, DEFAULT_SHAPE_SOLID_COLOR); - mSolidPressedColor = typedArray.getColor(R.styleable.ShapeEditText_shape_solidPressedColor, mSolidColor); - mSolidDisabledColor = typedArray.getColor(R.styleable.ShapeEditText_shape_solidDisabledColor, mSolidColor); - mSolidFocusedColor = typedArray.getColor(R.styleable.ShapeEditText_shape_solidFocusedColor, mSolidColor); - mSolidSelectedColor = typedArray.getColor(R.styleable.ShapeEditText_shape_solidSelectedColor, mSolidColor); - - int radius = typedArray.getDimensionPixelSize(R.styleable.ShapeEditText_shape_radius, DEFAULT_SHAPE_RADIUS); - mTopLeftRadius = typedArray.getDimensionPixelSize(R.styleable.ShapeEditText_shape_topLeftRadius, radius); - mTopRightRadius = typedArray.getDimensionPixelSize(R.styleable.ShapeEditText_shape_topRightRadius, radius); - mBottomLeftRadius = typedArray.getDimensionPixelSize(R.styleable.ShapeEditText_shape_bottomLeftRadius, radius); - mBottomRightRadius = typedArray.getDimensionPixelSize(R.styleable.ShapeEditText_shape_bottomRightRadius, radius); - - mStartColor = typedArray.getColor(R.styleable.ShapeEditText_shape_startColor, mSolidColor); - mCenterColor = typedArray.getColor(R.styleable.ShapeEditText_shape_centerColor, mSolidColor); - mEndColor = typedArray.getColor(R.styleable.ShapeEditText_shape_endColor, mSolidColor); - mUseLevel = typedArray.getBoolean(R.styleable.ShapeEditText_shape_useLevel, DEFAULT_SHAPE_USE_LEVEL); - mAngle = (int) typedArray.getFloat(R.styleable.ShapeEditText_shape_angle, DEFAULT_SHAPE_ANGLE); - mGradientType = typedArray.getInt(R.styleable.ShapeEditText_shape_gradientType, DEFAULT_SHAPE_GRADIENT_TYPE); - mCenterX = typedArray.getFloat(R.styleable.ShapeEditText_shape_centerX, DEFAULT_SHAPE_CENTER_X); - mCenterY = typedArray.getFloat(R.styleable.ShapeEditText_shape_centerY, DEFAULT_SHAPE_CENTER_Y); - mGradientRadius = typedArray.getDimensionPixelSize(R.styleable.ShapeEditText_shape_gradientRadius, radius); - - mStrokeColor = typedArray.getColor(R.styleable.ShapeEditText_shape_strokeColor, DEFAULT_SHAPE_STROKE_COLOR); - mStrokePressedColor = typedArray.getColor(R.styleable.ShapeEditText_shape_strokePressedColor, mStrokeColor); - mStrokeDisabledColor = typedArray.getColor(R.styleable.ShapeEditText_shape_strokeDisabledColor, mStrokeColor); - mStrokeFocusedColor = typedArray.getColor(R.styleable.ShapeEditText_shape_strokeFocusedColor, mStrokeColor); - mStrokeSelectedColor = typedArray.getColor(R.styleable.ShapeEditText_shape_strokeSelectedColor, mStrokeColor); - - mStrokeWidth = typedArray.getDimensionPixelSize(R.styleable.ShapeEditText_shape_strokeWidth, DEFAULT_SHAPE_STROKE_WIDTH); - mDashWidth = typedArray.getDimensionPixelSize(R.styleable.ShapeEditText_shape_dashWidth, DEFAULT_SHAPE_DASH_WIDTH); - mDashGap = typedArray.getDimensionPixelSize(R.styleable.ShapeEditText_shape_dashGap, DEFAULT_SHAPE_DASH_GAP); - - mInnerRadius = typedArray.getDimensionPixelOffset(R.styleable.ShapeEditText_shape_innerRadius, DEFAULT_SHAPE_INNER_RADIUS); - mInnerRadiusRatio = typedArray.getFloat(R.styleable.ShapeEditText_shape_innerRadiusRatio, DEFAULT_SHAPE_INNER_RADIUS_RATIO); - mThickness = typedArray.getDimensionPixelOffset(R.styleable.ShapeEditText_shape_thickness, DEFAULT_SHAPE_THICKNESS); - mThicknessRatio = typedArray.getFloat(R.styleable.ShapeEditText_shape_thicknessRatio, DEFAULT_SHAPE_THICKNESS_RATIO); - - mShadowSize = typedArray.getDimensionPixelSize(R.styleable.ShapeEditText_shape_shadowSize, DEFAULT_SHAPE_SHADOW_SIZE); - mShadowColor = typedArray.getColor(R.styleable.ShapeEditText_shape_shadowColor, DEFAULT_SHAPE_SHADOW_COLOR); - mShadowOffsetX = typedArray.getDimensionPixelOffset(R.styleable.ShapeEditText_shape_shadowOffsetX, DEFAULT_SHAPE_SHADOW_OFFSET_X); - mShadowOffsetY = typedArray.getDimensionPixelOffset(R.styleable.ShapeEditText_shape_shadowOffsetY, DEFAULT_SHAPE_SHADOW_OFFSET_Y); - - mTextColor = typedArray.getColor(R.styleable.ShapeEditText_shape_textColor, getTextColors().getDefaultColor()); - mTextPressedColor = typedArray.getColor(R.styleable.ShapeEditText_shape_textPressedColor, getTextColors().getColorForState(new int[]{android.R.attr.state_pressed}, mTextColor)); - mTextDisabledColor = typedArray.getColor(R.styleable.ShapeEditText_shape_textDisabledColor, getTextColors().getColorForState(new int[]{-android.R.attr.state_enabled}, mTextColor)); - mTextFocusedColor = typedArray.getColor(R.styleable.ShapeEditText_shape_textFocusedColor, getTextColors().getColorForState(new int[]{android.R.attr.state_focused}, mTextColor)); - mTextSelectedColor = typedArray.getColor(R.styleable.ShapeEditText_shape_textSelectedColor, getTextColors().getColorForState(new int[]{android.R.attr.state_selected}, mTextColor)); - - mTextStartColor = typedArray.getColor(R.styleable.ShapeEditText_shape_textStartColor, mTextColor); - mTextCenterColor = typedArray.getColor(R.styleable.ShapeEditText_shape_textCenterColor, mTextColor); - mTextEndColor = typedArray.getColor(R.styleable.ShapeEditText_shape_textEndColor, mTextColor); - mTextGradientOrientation = typedArray.getColor(R.styleable.ShapeEditText_shape_textGradientOrientation, SHAPE_TEXT_GRADIENT_TYPE_HORIZONTAL); - + mShapeDrawableBuilder = new ShapeDrawableBuilder(this, typedArray, STYLEABLE); + mTextColorBuilder = new TextColorBuilder(this, typedArray, STYLEABLE); typedArray.recycle(); - intoBackground(); + mShapeDrawableBuilder.intoBackground(); - if (isTextGradientColor()) { + if (mTextColorBuilder.isTextGradientColor()) { setText(getText()); } else { - intoTextColor(); + mTextColorBuilder.intoTextColor(); } } @Override public void setTextColor(int color) { super.setTextColor(color); - mTextColor = color; - - clearTextGradientColor(); - } - - /** - * {@link IShapeDrawable} - */ - - @Override - public ShapeEditText setShapeType(int type) { - mShapeType = type; - return this; - } - - @Override - public int getShapeType() { - return mShapeType; - } - - @Override - public ShapeEditText setShapeWidth(int width) { - mShapeWidth = width; - return this; - } - - @Override - public int getShapeWidth() { - return mShapeWidth; - } - - @Override - public ShapeEditText setShapeHeight(int height) { - mShapeHeight = height; - return this; - } - - @Override - public int getShapeHeight() { - return mShapeHeight; - } - - @Override - public ShapeEditText setSolidColor(int color) { - if (mSolidPressedColor == mSolidColor) { - mSolidPressedColor = color; - } - if (mSolidDisabledColor == mSolidColor) { - mSolidDisabledColor = color; - } - if (mSolidFocusedColor == mSolidColor) { - mSolidFocusedColor = color; - } - if (mSolidSelectedColor == mSolidColor) { - mSolidSelectedColor = color; - } - mSolidColor = color; - mStartColor = color; - mCenterColor = color; - mEndColor = color; - return this; - } - - @Override - public int getSolidColor() { - return mSolidColor; - } - - @Override - public ShapeEditText setSolidPressedColor(int color) { - mSolidPressedColor = color; - return this; - } - - @Override - public int getSolidPressedColor() { - return mSolidPressedColor; - } - - @Override - public ShapeEditText setSolidDisabledColor(int color) { - mSolidDisabledColor = color; - return this; - } - - @Override - public int getSolidDisabledColor() { - return mSolidDisabledColor; - } - - @Override - public ShapeEditText setSolidFocusedColor(int color) { - mSolidFocusedColor = color; - return this; - } - - @Override - public int getSolidFocusedColor() { - return mSolidFocusedColor; - } - - @Override - public ShapeEditText setSolidSelectedColor(int color) { - mSolidSelectedColor = color; - return this; - } - - @Override - public int getSolidSelectedColor() { - return mSolidSelectedColor; - } - - @Override - public ShapeEditText setTopLeftRadius(int radius) { - mTopLeftRadius = radius; - return this; - } - - @Override - public int getTopLeftRadius() { - return mTopLeftRadius; - } - - @Override - public ShapeEditText setTopRightRadius(int radius) { - mTopRightRadius = radius; - return this; - } - - @Override - public int getTopRightRadius() { - return mTopRightRadius; - } - - @Override - public ShapeEditText setBottomLeftRadius(int radius) { - mBottomLeftRadius = radius; - return this; - } - - @Override - public int getBottomLeftRadius() { - return mBottomLeftRadius; - } - - @Override - public ShapeEditText setBottomRightRadius(int radius) { - mBottomRightRadius = radius; - return this; - } - - @Override - public int getBottomRightRadius() { - return mBottomRightRadius; - } - - @Override - public ShapeEditText setStartColor(int color) { - mStartColor = color; - return this; - } - - @Override - public int getStartColor() { - return mStartColor; - } - - @Override - public ShapeEditText setCenterColor(int color) { - mCenterColor = color; - return this; - } - - @Override - public int getCenterColor() { - return mCenterColor; - } - - @Override - public ShapeEditText setEndColor(int color) { - mEndColor = color; - return this; - } - - @Override - public int getEndColor() { - return mEndColor; - } - - @Override - public ShapeEditText setUseLevel(boolean useLevel) { - mUseLevel = useLevel; - return this; - } - - @Override - public boolean isUseLevel() { - return mUseLevel; - } - - @Override - public ShapeEditText setAngle(int angle) { - mAngle = angle; - return this; - } - - @Override - public int getAngle() { - return mAngle; - } - - @Override - public ShapeEditText setGradientType(int type) { - mGradientType = type; - return this; - } - - @Override - public int getGradientType() { - return mGradientType; - } - - @Override - public ShapeEditText setCenterX(float x) { - mCenterX = x; - return this; - } - - @Override - public float getCenterX() { - return mCenterX; - } - - @Override - public ShapeEditText setCenterY(float y) { - mCenterY = y; - return this; - } - - @Override - public float getCenterY() { - return mCenterY; - } - - @Override - public ShapeEditText setGradientRadius(int radius) { - mGradientRadius = radius; - return this; - } - - @Override - public int getGradientRadius() { - return mGradientRadius; - } - - @Override - public ShapeEditText setStrokeColor(int color) { - if (mStrokePressedColor == mStrokeColor) { - mStrokePressedColor = color; - } - if (mStrokeDisabledColor == mStrokeColor) { - mStrokeDisabledColor = color; - } - if (mStrokeFocusedColor == mStrokeColor) { - mStrokeFocusedColor = color; - } - if (mStrokeSelectedColor == mStrokeColor) { - mStrokeSelectedColor = color; - } - mStrokeColor = color; - return this; - } - - @Override - public int getStrokeColor() { - return mStrokeColor; - } - - @Override - public ShapeEditText setStrokePressedColor(int color) { - mStrokePressedColor = color; - return this; - } - - @Override - public int getStrokePressedColor() { - return mStrokePressedColor; - } - - @Override - public ShapeEditText setStrokeDisabledColor(int color) { - mStrokeDisabledColor = color; - return this; - } - - @Override - public int getStrokeDisabledColor() { - return mStrokeDisabledColor; - } - - @Override - public ShapeEditText setStrokeFocusedColor(int color) { - mStrokeFocusedColor = color; - return this; - } - - @Override - public int getStrokeFocusedColor() { - return mStrokeFocusedColor; - } - - @Override - public ShapeEditText setStrokeSelectedColor(int color) { - mStrokeSelectedColor = color; - return this; - } - - @Override - public int getStrokeSelectedColor() { - return mStrokeSelectedColor; - } - - @Override - public ShapeEditText setStrokeWidth(int width) { - mStrokeWidth = width; - return this; - } - - @Override - public int getStrokeWidth() { - return mStrokeWidth; - } - - @Override - public ShapeEditText setDashWidth(int width) { - mDashWidth = width; - return this; - } - - @Override - public int getDashWidth() { - return mDashWidth; - } - - @Override - public ShapeEditText setDashGap(int gap) { - mDashGap = gap; - return this; - } - - @Override - public int getDashGap() { - return mDashGap; - } - - @Override - public ShapeEditText setInnerRadius(int radius) { - mInnerRadius = radius; - return this; - } - - @Override - public int getInnerRadius() { - return mInnerRadius; - } - - @Override - public ShapeEditText setInnerRadiusRatio(float ratio) { - mInnerRadiusRatio = ratio; - return this; - } - - @Override - public float getInnerRadiusRatio() { - return mInnerRadiusRatio; - } - - @Override - public ShapeEditText setThickness(int size) { - mThickness = size; - return this; - } - - @Override - public int getThickness() { - return mThickness; - } - - @Override - public ShapeEditText setThicknessRatio(float ratio) { - mThicknessRatio = ratio; - return this; - } - - @Override - public float getThicknessRatio() { - return mThicknessRatio; - } - - @Override - public ShapeEditText setShadowSize(int size) { - mShadowSize = size; - return this; - } - - @Override - public int getShadowSize() { - return mShadowSize; - } - - @Override - public ShapeEditText setShadowColor(int color) { - mShadowColor = color; - return this; - } - - @Override - public int getShadowColor() { - return mShadowColor; - } - - /** - * 获取文本阴影颜色(避免和 TextView 原有的方法冲突) - */ - public int getTextShadowColor() { - return super.getShadowColor(); - } - - @Override - public ShapeEditText setShadowOffsetX(int offsetX) { - mShadowOffsetX = offsetX; - return this; - } - - @Override - public int getShadowOffsetX() { - return mShadowOffsetX; - } - - @Override - public ShapeEditText setShadowOffsetY(int offsetY) { - mShadowOffsetY = offsetY; - return this; - } - - @Override - public int getShadowOffsetY() { - return mShadowOffsetY; - } - - @Override - public void intoBackground() { - Drawable drawable = buildBackgroundDrawable(); - if (drawable == null) { + if (mTextColorBuilder == null) { return; } - if (isDashLineEnable() || isShadowEnable()) { - // 需要关闭硬件加速,否则虚线或者阴影在某些手机上面无法生效 - setLayerType(View.LAYER_TYPE_SOFTWARE, null); - } - setBackground(drawable); - } - - /** - * {@link IShapeTextColor} - */ - - @Override - public ShapeEditText setNormalTextColor(int color) { - mTextColor = color; - return this; - } - - @Override - public int getNormalTextColor() { - return mTextColor; - } - - @Override - public ShapeEditText setTextPressedColor(int color) { - mTextPressedColor = color; - return this; - } - - @Override - public int getTextPressedColor() { - return mTextPressedColor; - } - - @Override - public ShapeEditText setTextDisabledColor(int color) { - mTextDisabledColor = color; - return this; - } - - @Override - public int getTextDisabledColor() { - return mTextDisabledColor; - } - - @Override - public ShapeEditText setTextFocusedColor(int color) { - mTextFocusedColor = color; - return this; - } - - @Override - public int getTextFocusedColor() { - return mTextFocusedColor; - } - - @Override - public ShapeEditText setTextSelectedColor(int color) { - mTextSelectedColor = color; - return this; - } - - @Override - public int getTextSelectedColor() { - return mTextSelectedColor; - } - - @Override - public void intoTextColor() { - setTextColor(buildColorState()); - } - - @Override - public ShapeEditText setTextStartColor(int color) { - mTextStartColor = color; - return this; - } - - @Override - public int getTextStartColor() { - return mTextStartColor; - } - - @Override - public ShapeEditText setTextCenterColor(int color) { - mTextCenterColor = color; - return this; - } - - @Override - public int getTextCenterColor() { - return mTextCenterColor; - } - - @Override - public ShapeEditText setTextEndColor(int color) { - mTextEndColor = color; - return this; - } - - @Override - public int getTextEndColor() { - return mTextEndColor; - } - - @Override - public ShapeEditText setTextGradientOrientation(int orientation) { - mTextGradientOrientation = orientation; - return this; - } - - @Override - public int getTextGradientOrientation() { - return mTextGradientOrientation; + mTextColorBuilder.setTextColor(color); + mTextColorBuilder.clearTextGradientColor(); } @Override public void setText(CharSequence text, BufferType type) { - if (isTextGradientColor()) { - super.setText(buildLinearGradientSpannable(text), type); + if (mTextColorBuilder != null && mTextColorBuilder.isTextGradientColor()) { + super.setText(mTextColorBuilder.buildLinearGradientSpannable(text), type); } else { super.setText(text, type); } } + + public ShapeDrawableBuilder getShapeDrawableBuilder() { + return mShapeDrawableBuilder; + } + + public TextColorBuilder getTextColorBuilder() { + return mTextColorBuilder; + } } \ No newline at end of file diff --git a/library/src/main/java/com/hjq/shape/view/ShapeImageView.java b/library/src/main/java/com/hjq/shape/view/ShapeImageView.java index c924c9d..53bee56 100644 --- a/library/src/main/java/com/hjq/shape/view/ShapeImageView.java +++ b/library/src/main/java/com/hjq/shape/view/ShapeImageView.java @@ -2,13 +2,12 @@ import android.content.Context; import android.content.res.TypedArray; -import android.graphics.drawable.Drawable; import android.support.v7.widget.AppCompatImageView; import android.util.AttributeSet; -import android.view.View; import com.hjq.shape.R; -import com.hjq.shape.core.IShapeDrawable; +import com.hjq.shape.builder.ShapeDrawableBuilder; +import com.hjq.shape.styleable.ShapeImageViewStyleable; /** * author : Android 轮子哥 @@ -16,52 +15,11 @@ * time : 2021/07/17 * desc : 支持直接定义 Shape 背景的 ImageView */ -public class ShapeImageView extends AppCompatImageView implements IShapeDrawable { +public class ShapeImageView extends AppCompatImageView { - private int mShapeType; - private int mShapeWidth; - private int mShapeHeight; + private static final ShapeImageViewStyleable STYLEABLE = new ShapeImageViewStyleable(); - private int mSolidColor; - private int mSolidPressedColor; - private int mSolidDisabledColor; - private int mSolidFocusedColor; - private int mSolidSelectedColor; - - private int mTopLeftRadius; - private int mTopRightRadius; - private int mBottomLeftRadius; - private int mBottomRightRadius; - - private int mStartColor; - private int mCenterColor; - private int mEndColor; - private boolean mUseLevel; - private int mAngle; - private int mGradientType; - private float mCenterX; - private float mCenterY; - private int mGradientRadius; - - private int mStrokeColor; - private int mStrokePressedColor; - private int mStrokeDisabledColor; - private int mStrokeFocusedColor; - private int mStrokeSelectedColor; - - private int mStrokeWidth; - private int mDashWidth; - private int mDashGap; - - private int mInnerRadius; - private float mInnerRadiusRatio; - private int mThickness; - private float mThicknessRatio; - - private int mShadowSize; - private int mShadowColor; - private int mShadowOffsetX; - private int mShadowOffsetY; + private final ShapeDrawableBuilder mShapeDrawableBuilder; public ShapeImageView(Context context) { this(context, null); @@ -75,505 +33,13 @@ public ShapeImageView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.ShapeImageView); - mShapeType = typedArray.getInt(R.styleable.ShapeImageView_shape, DEFAULT_SHAPE_TYPE); - mShapeWidth = typedArray.getDimensionPixelSize(R.styleable.ShapeImageView_shape_width, DEFAULT_SHAPE_WIDTH); - mShapeHeight = typedArray.getDimensionPixelSize(R.styleable.ShapeImageView_shape_height, DEFAULT_SHAPE_HEIGHT); - - mSolidColor = typedArray.getColor(R.styleable.ShapeImageView_shape_solidColor, DEFAULT_SHAPE_SOLID_COLOR); - mSolidPressedColor = typedArray.getColor(R.styleable.ShapeImageView_shape_solidPressedColor, mSolidColor); - mSolidDisabledColor = typedArray.getColor(R.styleable.ShapeImageView_shape_solidDisabledColor, mSolidColor); - mSolidFocusedColor = typedArray.getColor(R.styleable.ShapeImageView_shape_solidFocusedColor, mSolidColor); - mSolidSelectedColor = typedArray.getColor(R.styleable.ShapeImageView_shape_solidSelectedColor, mSolidColor); - - int radius = typedArray.getDimensionPixelSize(R.styleable.ShapeImageView_shape_radius, DEFAULT_SHAPE_RADIUS); - mTopLeftRadius = typedArray.getDimensionPixelSize(R.styleable.ShapeImageView_shape_topLeftRadius, radius); - mTopRightRadius = typedArray.getDimensionPixelSize(R.styleable.ShapeImageView_shape_topRightRadius, radius); - mBottomLeftRadius = typedArray.getDimensionPixelSize(R.styleable.ShapeImageView_shape_bottomLeftRadius, radius); - mBottomRightRadius = typedArray.getDimensionPixelSize(R.styleable.ShapeImageView_shape_bottomRightRadius, radius); - - mStartColor = typedArray.getColor(R.styleable.ShapeImageView_shape_startColor, mSolidColor); - mCenterColor = typedArray.getColor(R.styleable.ShapeImageView_shape_centerColor, mSolidColor); - mEndColor = typedArray.getColor(R.styleable.ShapeImageView_shape_endColor, mSolidColor); - mUseLevel = typedArray.getBoolean(R.styleable.ShapeImageView_shape_useLevel, DEFAULT_SHAPE_USE_LEVEL); - mAngle = (int) typedArray.getFloat(R.styleable.ShapeImageView_shape_angle, DEFAULT_SHAPE_ANGLE); - mGradientType = typedArray.getInt(R.styleable.ShapeImageView_shape_gradientType, DEFAULT_SHAPE_GRADIENT_TYPE); - mCenterX = typedArray.getFloat(R.styleable.ShapeImageView_shape_centerX, DEFAULT_SHAPE_CENTER_X); - mCenterY = typedArray.getFloat(R.styleable.ShapeImageView_shape_centerY, DEFAULT_SHAPE_CENTER_Y); - mGradientRadius = typedArray.getDimensionPixelSize(R.styleable.ShapeImageView_shape_gradientRadius, radius); - - mStrokeColor = typedArray.getColor(R.styleable.ShapeImageView_shape_strokeColor, DEFAULT_SHAPE_STROKE_COLOR); - mStrokePressedColor = typedArray.getColor(R.styleable.ShapeImageView_shape_strokePressedColor, mStrokeColor); - mStrokeDisabledColor = typedArray.getColor(R.styleable.ShapeImageView_shape_strokeDisabledColor, mStrokeColor); - mStrokeFocusedColor = typedArray.getColor(R.styleable.ShapeImageView_shape_strokeFocusedColor, mStrokeColor); - mStrokeSelectedColor = typedArray.getColor(R.styleable.ShapeImageView_shape_strokeSelectedColor, mStrokeColor); - - mStrokeWidth = typedArray.getDimensionPixelSize(R.styleable.ShapeImageView_shape_strokeWidth, DEFAULT_SHAPE_STROKE_WIDTH); - mDashWidth = typedArray.getDimensionPixelSize(R.styleable.ShapeImageView_shape_dashWidth, DEFAULT_SHAPE_DASH_WIDTH); - mDashGap = typedArray.getDimensionPixelSize(R.styleable.ShapeImageView_shape_dashGap, DEFAULT_SHAPE_DASH_GAP); - - mInnerRadius = typedArray.getDimensionPixelOffset(R.styleable.ShapeImageView_shape_innerRadius, DEFAULT_SHAPE_INNER_RADIUS); - mInnerRadiusRatio = typedArray.getFloat(R.styleable.ShapeImageView_shape_innerRadiusRatio, DEFAULT_SHAPE_INNER_RADIUS_RATIO); - mThickness = typedArray.getDimensionPixelOffset(R.styleable.ShapeImageView_shape_thickness, DEFAULT_SHAPE_THICKNESS); - mThicknessRatio = typedArray.getFloat(R.styleable.ShapeImageView_shape_thicknessRatio, DEFAULT_SHAPE_THICKNESS_RATIO); - - mShadowSize = typedArray.getDimensionPixelSize(R.styleable.ShapeImageView_shape_shadowSize, DEFAULT_SHAPE_SHADOW_SIZE); - mShadowColor = typedArray.getColor(R.styleable.ShapeImageView_shape_shadowColor, DEFAULT_SHAPE_SHADOW_COLOR); - mShadowOffsetX = typedArray.getDimensionPixelOffset(R.styleable.ShapeImageView_shape_shadowOffsetX, DEFAULT_SHAPE_SHADOW_OFFSET_X); - mShadowOffsetY = typedArray.getDimensionPixelOffset(R.styleable.ShapeImageView_shape_shadowOffsetY, DEFAULT_SHAPE_SHADOW_OFFSET_Y); - + mShapeDrawableBuilder = new ShapeDrawableBuilder(this, typedArray, STYLEABLE); typedArray.recycle(); - intoBackground(); - } - - /** - * {@link IShapeDrawable} - */ - - @Override - public ShapeImageView setShapeType(int type) { - mShapeType = type; - return this; - } - - @Override - public int getShapeType() { - return mShapeType; - } - - @Override - public ShapeImageView setShapeWidth(int width) { - mShapeWidth = width; - return this; - } - - @Override - public int getShapeWidth() { - return mShapeWidth; - } - - @Override - public ShapeImageView setShapeHeight(int height) { - mShapeHeight = height; - return this; - } - - @Override - public int getShapeHeight() { - return mShapeHeight; - } - - @Override - public ShapeImageView setSolidColor(int color) { - if (mSolidPressedColor == mSolidColor) { - mSolidPressedColor = color; - } - if (mSolidDisabledColor == mSolidColor) { - mSolidDisabledColor = color; - } - if (mSolidFocusedColor == mSolidColor) { - mSolidFocusedColor = color; - } - if (mSolidSelectedColor == mSolidColor) { - mSolidSelectedColor = color; - } - mSolidColor = color; - mStartColor = color; - mCenterColor = color; - mEndColor = color; - return this; - } - - @Override - public int getSolidColor() { - return mSolidColor; - } - - @Override - public ShapeImageView setSolidPressedColor(int color) { - mSolidPressedColor = color; - return this; - } - - @Override - public int getSolidPressedColor() { - return mSolidPressedColor; - } - - @Override - public ShapeImageView setSolidDisabledColor(int color) { - mSolidDisabledColor = color; - return this; - } - - @Override - public int getSolidDisabledColor() { - return mSolidDisabledColor; - } - - @Override - public ShapeImageView setSolidFocusedColor(int color) { - mSolidFocusedColor = color; - return this; - } - - @Override - public int getSolidFocusedColor() { - return mSolidFocusedColor; - } - - @Override - public ShapeImageView setSolidSelectedColor(int color) { - mSolidSelectedColor = color; - return this; - } - - @Override - public int getSolidSelectedColor() { - return mSolidSelectedColor; - } - - @Override - public ShapeImageView setTopLeftRadius(int radius) { - mTopLeftRadius = radius; - return this; - } - - @Override - public int getTopLeftRadius() { - return mTopLeftRadius; - } - - @Override - public ShapeImageView setTopRightRadius(int radius) { - mTopRightRadius = radius; - return this; - } - - @Override - public int getTopRightRadius() { - return mTopRightRadius; - } - - @Override - public ShapeImageView setBottomLeftRadius(int radius) { - mBottomLeftRadius = radius; - return this; - } - - @Override - public int getBottomLeftRadius() { - return mBottomLeftRadius; - } - - @Override - public ShapeImageView setBottomRightRadius(int radius) { - mBottomRightRadius = radius; - return this; - } - - @Override - public int getBottomRightRadius() { - return mBottomRightRadius; - } - - @Override - public ShapeImageView setStartColor(int color) { - mStartColor = color; - return this; - } - - @Override - public int getStartColor() { - return mStartColor; - } - - @Override - public ShapeImageView setCenterColor(int color) { - mCenterColor = color; - return this; - } - - @Override - public int getCenterColor() { - return mCenterColor; - } - - @Override - public ShapeImageView setEndColor(int color) { - mEndColor = color; - return this; - } - - @Override - public int getEndColor() { - return mEndColor; - } - - @Override - public ShapeImageView setUseLevel(boolean useLevel) { - mUseLevel = useLevel; - return this; - } - - @Override - public boolean isUseLevel() { - return mUseLevel; - } - - @Override - public ShapeImageView setAngle(int angle) { - mAngle = angle; - return this; - } - - @Override - public int getAngle() { - return mAngle; - } - - @Override - public ShapeImageView setGradientType(int type) { - mGradientType = type; - return this; - } - - @Override - public int getGradientType() { - return mGradientType; - } - - @Override - public ShapeImageView setCenterX(float x) { - mCenterX = x; - return this; - } - - @Override - public float getCenterX() { - return mCenterX; - } - - @Override - public ShapeImageView setCenterY(float y) { - mCenterY = y; - return this; - } - - @Override - public float getCenterY() { - return mCenterY; - } - - @Override - public ShapeImageView setGradientRadius(int radius) { - mGradientRadius = radius; - return this; - } - - @Override - public int getGradientRadius() { - return mGradientRadius; - } - - @Override - public ShapeImageView setStrokeColor(int color) { - if (mStrokePressedColor == mStrokeColor) { - mStrokePressedColor = color; - } - if (mStrokeDisabledColor == mStrokeColor) { - mStrokeDisabledColor = color; - } - if (mStrokeFocusedColor == mStrokeColor) { - mStrokeFocusedColor = color; - } - if (mStrokeSelectedColor == mStrokeColor) { - mStrokeSelectedColor = color; - } - mStrokeColor = color; - return this; - } - - @Override - public int getStrokeColor() { - return mStrokeColor; - } - - @Override - public ShapeImageView setStrokePressedColor(int color) { - mStrokePressedColor = color; - return this; - } - - @Override - public int getStrokePressedColor() { - return mStrokePressedColor; - } - - @Override - public ShapeImageView setStrokeDisabledColor(int color) { - mStrokeDisabledColor = color; - return this; - } - - @Override - public int getStrokeDisabledColor() { - return mStrokeDisabledColor; - } - - @Override - public ShapeImageView setStrokeFocusedColor(int color) { - mStrokeFocusedColor = color; - return this; - } - - @Override - public int getStrokeFocusedColor() { - return mStrokeFocusedColor; - } - - @Override - public ShapeImageView setStrokeSelectedColor(int color) { - mStrokeSelectedColor = color; - return this; - } - - @Override - public int getStrokeSelectedColor() { - return mStrokeSelectedColor; - } - - @Override - public ShapeImageView setStrokeWidth(int width) { - mStrokeWidth = width; - return this; - } - - @Override - public int getStrokeWidth() { - return mStrokeWidth; - } - - @Override - public ShapeImageView setDashWidth(int width) { - mDashWidth = width; - return this; - } - - @Override - public int getDashWidth() { - return mDashWidth; - } - - @Override - public ShapeImageView setDashGap(int gap) { - mDashGap = gap; - return this; - } - - @Override - public int getDashGap() { - return mDashGap; - } - - @Override - public ShapeImageView setInnerRadius(int radius) { - mInnerRadius = radius; - return this; - } - - @Override - public int getInnerRadius() { - return mInnerRadius; - } - - @Override - public ShapeImageView setInnerRadiusRatio(float ratio) { - mInnerRadiusRatio = ratio; - return this; - } - - @Override - public float getInnerRadiusRatio() { - return mInnerRadiusRatio; - } - - @Override - public ShapeImageView setThickness(int size) { - mThickness = size; - return this; - } - - @Override - public int getThickness() { - return mThickness; - } - - @Override - public ShapeImageView setThicknessRatio(float ratio) { - mThicknessRatio = ratio; - return this; - } - - @Override - public float getThicknessRatio() { - return mThicknessRatio; - } - - @Override - public ShapeImageView setShadowSize(int size) { - mShadowSize = size; - return this; - } - - @Override - public int getShadowSize() { - return mShadowSize; - } - - @Override - public ShapeImageView setShadowColor(int color) { - mShadowColor = color; - return this; - } - - @Override - public int getShadowColor() { - return mShadowColor; - } - - @Override - public ShapeImageView setShadowOffsetX(int offsetX) { - mShadowOffsetX = offsetX; - return this; - } - - @Override - public int getShadowOffsetX() { - return mShadowOffsetX; - } - - @Override - public ShapeImageView setShadowOffsetY(int offsetY) { - mShadowOffsetY = offsetY; - return this; - } - - @Override - public int getShadowOffsetY() { - return mShadowOffsetY; + mShapeDrawableBuilder.intoBackground(); } - @Override - public void intoBackground() { - Drawable drawable = buildBackgroundDrawable(); - if (drawable == null) { - return; - } - if (isDashLineEnable() || isShadowEnable()) { - // 需要关闭硬件加速,否则虚线或者阴影在某些手机上面无法生效 - setLayerType(View.LAYER_TYPE_SOFTWARE, null); - } - setBackground(drawable); + public ShapeDrawableBuilder getShapeDrawableBuilder() { + return mShapeDrawableBuilder; } } \ No newline at end of file diff --git a/library/src/main/java/com/hjq/shape/view/ShapeRadioButton.java b/library/src/main/java/com/hjq/shape/view/ShapeRadioButton.java index 1f6770a..d0537a4 100644 --- a/library/src/main/java/com/hjq/shape/view/ShapeRadioButton.java +++ b/library/src/main/java/com/hjq/shape/view/ShapeRadioButton.java @@ -5,11 +5,12 @@ import android.graphics.drawable.Drawable; import android.support.v7.widget.AppCompatRadioButton; import android.util.AttributeSet; -import android.view.View; import com.hjq.shape.R; -import com.hjq.shape.core.IShapeDrawable; -import com.hjq.shape.core.IShapeTextColor; +import com.hjq.shape.builder.ButtonDrawableBuilder; +import com.hjq.shape.builder.ShapeDrawableBuilder; +import com.hjq.shape.builder.TextColorBuilder; +import com.hjq.shape.styleable.ShapeRadioButtonStyleable; /** * author : Android 轮子哥 @@ -17,67 +18,13 @@ * time : 2021/07/17 * desc : 支持直接定义 Shape 背景的 RadioButton */ -public class ShapeRadioButton extends AppCompatRadioButton implements - IShapeDrawable, IShapeTextColor { +public class ShapeRadioButton extends AppCompatRadioButton { - private int mShapeType; - private int mShapeWidth; - private int mShapeHeight; + private static final ShapeRadioButtonStyleable STYLEABLE = new ShapeRadioButtonStyleable(); - private int mSolidColor; - private int mSolidPressedColor; - private int mSolidCheckedColor; - private int mSolidDisabledColor; - private int mSolidFocusedColor; - private int mSolidSelectedColor; - - private int mTopLeftRadius; - private int mTopRightRadius; - private int mBottomLeftRadius; - private int mBottomRightRadius; - - private int mStartColor; - private int mCenterColor; - private int mEndColor; - private boolean mUseLevel; - private int mAngle; - private int mGradientType; - private float mCenterX; - private float mCenterY; - private int mGradientRadius; - - private int mStrokeColor; - private int mStrokePressedColor; - private int mStrokeCheckedColor; - private int mStrokeDisabledColor; - private int mStrokeFocusedColor; - private int mStrokeSelectedColor; - - private int mStrokeWidth; - private int mDashWidth; - private int mDashGap; - - private int mInnerRadius; - private float mInnerRadiusRatio; - private int mThickness; - private float mThicknessRatio; - - private int mShadowSize; - private int mShadowColor; - private int mShadowOffsetX; - private int mShadowOffsetY; - - private int mTextColor; - private int mTextPressedColor; - private int mTextCheckedColor; - private int mTextDisabledColor; - private int mTextFocusedColor; - private int mTextSelectedColor; - - private int mTextStartColor; - private int mTextCenterColor; - private int mTextEndColor; - private int mTextGradientOrientation; + private final ShapeDrawableBuilder mShapeDrawableBuilder; + private final TextColorBuilder mTextColorBuilder; + private final ButtonDrawableBuilder mButtonDrawableBuilder; public ShapeRadioButton(Context context) { this(context, null); @@ -91,696 +38,59 @@ public ShapeRadioButton(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.ShapeRadioButton); - mShapeType = typedArray.getInt(R.styleable.ShapeRadioButton_shape, DEFAULT_SHAPE_TYPE); - mShapeWidth = typedArray.getDimensionPixelSize(R.styleable.ShapeRadioButton_shape_width, DEFAULT_SHAPE_WIDTH); - mShapeHeight = typedArray.getDimensionPixelSize(R.styleable.ShapeRadioButton_shape_height, DEFAULT_SHAPE_HEIGHT); - - mSolidColor = typedArray.getColor(R.styleable.ShapeRadioButton_shape_solidColor, DEFAULT_SHAPE_SOLID_COLOR); - mSolidPressedColor = typedArray.getColor(R.styleable.ShapeRadioButton_shape_solidPressedColor, mSolidColor); - mSolidCheckedColor = typedArray.getColor(R.styleable.ShapeRadioButton_shape_solidCheckedColor, mSolidColor); - mSolidDisabledColor = typedArray.getColor(R.styleable.ShapeRadioButton_shape_solidDisabledColor, mSolidColor); - mSolidFocusedColor = typedArray.getColor(R.styleable.ShapeRadioButton_shape_solidFocusedColor, mSolidColor); - mSolidSelectedColor = typedArray.getColor(R.styleable.ShapeRadioButton_shape_solidSelectedColor, mSolidColor); - - int radius = typedArray.getDimensionPixelSize(R.styleable.ShapeRadioButton_shape_radius, DEFAULT_SHAPE_RADIUS); - mTopLeftRadius = typedArray.getDimensionPixelSize(R.styleable.ShapeRadioButton_shape_topLeftRadius, radius); - mTopRightRadius = typedArray.getDimensionPixelSize(R.styleable.ShapeRadioButton_shape_topRightRadius, radius); - mBottomLeftRadius = typedArray.getDimensionPixelSize(R.styleable.ShapeRadioButton_shape_bottomLeftRadius, radius); - mBottomRightRadius = typedArray.getDimensionPixelSize(R.styleable.ShapeRadioButton_shape_bottomRightRadius, radius); - - mStartColor = typedArray.getColor(R.styleable.ShapeRadioButton_shape_startColor, mSolidColor); - mCenterColor = typedArray.getColor(R.styleable.ShapeRadioButton_shape_centerColor, mSolidColor); - mEndColor = typedArray.getColor(R.styleable.ShapeRadioButton_shape_endColor, mSolidColor); - mUseLevel = typedArray.getBoolean(R.styleable.ShapeRadioButton_shape_useLevel, DEFAULT_SHAPE_USE_LEVEL); - mAngle = (int) typedArray.getFloat(R.styleable.ShapeRadioButton_shape_angle, DEFAULT_SHAPE_ANGLE); - mGradientType = typedArray.getInt(R.styleable.ShapeRadioButton_shape_gradientType, DEFAULT_SHAPE_GRADIENT_TYPE); - mCenterX = typedArray.getFloat(R.styleable.ShapeRadioButton_shape_centerX, DEFAULT_SHAPE_CENTER_X); - mCenterY = typedArray.getFloat(R.styleable.ShapeRadioButton_shape_centerY, DEFAULT_SHAPE_CENTER_Y); - mGradientRadius = typedArray.getDimensionPixelSize(R.styleable.ShapeRadioButton_shape_gradientRadius, radius); - - mStrokeColor = typedArray.getColor(R.styleable.ShapeRadioButton_shape_strokeColor, DEFAULT_SHAPE_STROKE_COLOR); - mStrokePressedColor = typedArray.getColor(R.styleable.ShapeRadioButton_shape_strokePressedColor, mStrokeColor); - mStrokeCheckedColor = typedArray.getColor(R.styleable.ShapeRadioButton_shape_strokeCheckedColor, mStrokeColor); - mStrokeDisabledColor = typedArray.getColor(R.styleable.ShapeRadioButton_shape_strokeDisabledColor, mStrokeColor); - mStrokeFocusedColor = typedArray.getColor(R.styleable.ShapeRadioButton_shape_strokeFocusedColor, mStrokeColor); - mStrokeSelectedColor = typedArray.getColor(R.styleable.ShapeRadioButton_shape_strokeSelectedColor, mStrokeColor); - - mStrokeWidth = typedArray.getDimensionPixelSize(R.styleable.ShapeRadioButton_shape_strokeWidth, DEFAULT_SHAPE_STROKE_WIDTH); - mDashWidth = typedArray.getDimensionPixelSize(R.styleable.ShapeRadioButton_shape_dashWidth, DEFAULT_SHAPE_DASH_WIDTH); - mDashGap = typedArray.getDimensionPixelSize(R.styleable.ShapeRadioButton_shape_dashGap, DEFAULT_SHAPE_DASH_GAP); - - mInnerRadius = typedArray.getDimensionPixelOffset(R.styleable.ShapeRadioButton_shape_innerRadius, DEFAULT_SHAPE_INNER_RADIUS); - mInnerRadiusRatio = typedArray.getFloat(R.styleable.ShapeRadioButton_shape_innerRadiusRatio, DEFAULT_SHAPE_INNER_RADIUS_RATIO); - mThickness = typedArray.getDimensionPixelOffset(R.styleable.ShapeRadioButton_shape_thickness, DEFAULT_SHAPE_THICKNESS); - mThicknessRatio = typedArray.getFloat(R.styleable.ShapeRadioButton_shape_thicknessRatio, DEFAULT_SHAPE_THICKNESS_RATIO); - - mShadowSize = typedArray.getDimensionPixelSize(R.styleable.ShapeRadioButton_shape_shadowSize, DEFAULT_SHAPE_SHADOW_SIZE); - mShadowColor = typedArray.getColor(R.styleable.ShapeRadioButton_shape_shadowColor, DEFAULT_SHAPE_SHADOW_COLOR); - mShadowOffsetX = typedArray.getDimensionPixelOffset(R.styleable.ShapeRadioButton_shape_shadowOffsetX, DEFAULT_SHAPE_SHADOW_OFFSET_X); - mShadowOffsetY = typedArray.getDimensionPixelOffset(R.styleable.ShapeRadioButton_shape_shadowOffsetY, DEFAULT_SHAPE_SHADOW_OFFSET_Y); - - mTextColor = typedArray.getColor(R.styleable.ShapeRadioButton_shape_textColor, getTextColors().getDefaultColor()); - mTextPressedColor = typedArray.getColor(R.styleable.ShapeRadioButton_shape_textPressedColor, getTextColors().getColorForState(new int[]{android.R.attr.state_pressed}, mTextColor)); - mTextCheckedColor = typedArray.getColor(R.styleable.ShapeRadioButton_shape_textCheckedColor, getTextColors().getColorForState(new int[]{android.R.attr.state_checked}, mTextColor)); - mTextDisabledColor = typedArray.getColor(R.styleable.ShapeRadioButton_shape_textDisabledColor, getTextColors().getColorForState(new int[]{-android.R.attr.state_enabled}, mTextColor)); - mTextFocusedColor = typedArray.getColor(R.styleable.ShapeRadioButton_shape_textFocusedColor, getTextColors().getColorForState(new int[]{android.R.attr.state_focused}, mTextColor)); - mTextSelectedColor = typedArray.getColor(R.styleable.ShapeRadioButton_shape_textSelectedColor, getTextColors().getColorForState(new int[]{android.R.attr.state_selected}, mTextColor)); - - mTextStartColor = typedArray.getColor(R.styleable.ShapeRadioButton_shape_textStartColor, mTextColor); - mTextCenterColor = typedArray.getColor(R.styleable.ShapeRadioButton_shape_textCenterColor, mTextColor); - mTextEndColor = typedArray.getColor(R.styleable.ShapeRadioButton_shape_textEndColor, mTextColor); - mTextGradientOrientation = typedArray.getColor(R.styleable.ShapeRadioButton_shape_textGradientOrientation, SHAPE_TEXT_GRADIENT_TYPE_HORIZONTAL); - + mShapeDrawableBuilder = new ShapeDrawableBuilder(this, typedArray, STYLEABLE); + mTextColorBuilder = new TextColorBuilder(this, typedArray, STYLEABLE); + mButtonDrawableBuilder = new ButtonDrawableBuilder(this, typedArray, STYLEABLE); typedArray.recycle(); - intoBackground(); + mShapeDrawableBuilder.intoBackground(); - if (isTextGradientColor()) { + if (mTextColorBuilder.isTextGradientColor()) { setText(getText()); } else { - intoTextColor(); + mTextColorBuilder.intoTextColor(); } + + mButtonDrawableBuilder.intoButtonDrawable(); } @Override public void setTextColor(int color) { super.setTextColor(color); - mTextColor = color; - - clearTextGradientColor(); - } - - /** - * {@link IShapeDrawable} - */ - - @Override - public ShapeRadioButton setShapeType(int type) { - mShapeType = type; - return this; - } - - @Override - public int getShapeType() { - return mShapeType; - } - - @Override - public ShapeRadioButton setShapeWidth(int width) { - mShapeWidth = width; - return this; - } - - @Override - public int getShapeWidth() { - return mShapeWidth; - } - - @Override - public ShapeRadioButton setShapeHeight(int height) { - mShapeHeight = height; - return this; - } - - @Override - public int getShapeHeight() { - return mShapeHeight; - } - - @Override - public ShapeRadioButton setSolidColor(int color) { - if (mSolidPressedColor == mSolidColor) { - mSolidPressedColor = color; - } - if (mSolidCheckedColor == mSolidColor) { - mSolidCheckedColor = color; - } - if (mSolidDisabledColor == mSolidColor) { - mSolidDisabledColor = color; - } - if (mSolidFocusedColor == mSolidColor) { - mSolidFocusedColor = color; - } - if (mSolidSelectedColor == mSolidColor) { - mSolidSelectedColor = color; + if (mTextColorBuilder == null) { + return; } - mSolidColor = color; - mStartColor = color; - mCenterColor = color; - mEndColor = color; - return this; - } - - @Override - public int getSolidColor() { - return mSolidColor; - } - - @Override - public ShapeRadioButton setSolidPressedColor(int color) { - mSolidPressedColor = color; - return this; - } - - @Override - public int getSolidPressedColor() { - return mSolidPressedColor; - } - - @Override - public ShapeRadioButton setSolidCheckedColor(int color) { - mSolidCheckedColor = color; - return this; - } - - @Override - public int getSolidCheckedColor() { - return mSolidCheckedColor; - } - - @Override - public ShapeRadioButton setSolidDisabledColor(int color) { - mSolidDisabledColor = color; - return this; - } - - @Override - public int getSolidDisabledColor() { - return mSolidDisabledColor; - } - - @Override - public ShapeRadioButton setSolidFocusedColor(int color) { - mSolidFocusedColor = color; - return this; - } - - @Override - public int getSolidFocusedColor() { - return mSolidFocusedColor; - } - - @Override - public ShapeRadioButton setSolidSelectedColor(int color) { - mSolidSelectedColor = color; - return this; - } - - @Override - public int getSolidSelectedColor() { - return mSolidSelectedColor; - } - - @Override - public ShapeRadioButton setTopLeftRadius(int radius) { - mTopLeftRadius = radius; - return this; + mTextColorBuilder.setTextColor(color); + mTextColorBuilder.clearTextGradientColor(); } @Override - public int getTopLeftRadius() { - return mTopLeftRadius; - } - - @Override - public ShapeRadioButton setTopRightRadius(int radius) { - mTopRightRadius = radius; - return this; - } - - @Override - public int getTopRightRadius() { - return mTopRightRadius; - } - - @Override - public ShapeRadioButton setBottomLeftRadius(int radius) { - mBottomLeftRadius = radius; - return this; - } - - @Override - public int getBottomLeftRadius() { - return mBottomLeftRadius; - } - - @Override - public ShapeRadioButton setBottomRightRadius(int radius) { - mBottomRightRadius = radius; - return this; - } - - @Override - public int getBottomRightRadius() { - return mBottomRightRadius; - } - - @Override - public ShapeRadioButton setStartColor(int color) { - mStartColor = color; - return this; - } - - @Override - public int getStartColor() { - return mStartColor; - } - - @Override - public ShapeRadioButton setCenterColor(int color) { - mCenterColor = color; - return this; - } - - @Override - public int getCenterColor() { - return mCenterColor; - } - - @Override - public ShapeRadioButton setEndColor(int color) { - mEndColor = color; - return this; - } - - @Override - public int getEndColor() { - return mEndColor; - } - - @Override - public ShapeRadioButton setUseLevel(boolean useLevel) { - mUseLevel = useLevel; - return this; - } - - @Override - public boolean isUseLevel() { - return mUseLevel; - } - - @Override - public ShapeRadioButton setAngle(int angle) { - mAngle = angle; - return this; - } - - @Override - public int getAngle() { - return mAngle; - } - - @Override - public ShapeRadioButton setGradientType(int type) { - mGradientType = type; - return this; - } - - @Override - public int getGradientType() { - return mGradientType; - } - - @Override - public ShapeRadioButton setCenterX(float x) { - mCenterX = x; - return this; - } - - @Override - public float getCenterX() { - return mCenterX; - } - - @Override - public ShapeRadioButton setCenterY(float y) { - mCenterY = y; - return this; - } - - @Override - public float getCenterY() { - return mCenterY; - } - - @Override - public ShapeRadioButton setGradientRadius(int radius) { - mGradientRadius = radius; - return this; - } - - @Override - public int getGradientRadius() { - return mGradientRadius; - } - - @Override - public ShapeRadioButton setStrokeColor(int color) { - if (mStrokePressedColor == mStrokeColor) { - mStrokePressedColor = color; - } - if (mStrokeCheckedColor == mStrokeColor) { - mStrokeCheckedColor = color; - } - if (mStrokeDisabledColor == mStrokeColor) { - mStrokeDisabledColor = color; - } - if (mStrokeFocusedColor == mStrokeColor) { - mStrokeFocusedColor = color; - } - if (mStrokeSelectedColor == mStrokeColor) { - mStrokeSelectedColor = color; + public void setText(CharSequence text, BufferType type) { + if (mTextColorBuilder != null && mTextColorBuilder.isTextGradientColor()) { + super.setText(mTextColorBuilder.buildLinearGradientSpannable(text), type); + } else { + super.setText(text, type); } - mStrokeColor = color; - return this; - } - - @Override - public int getStrokeColor() { - return mStrokeColor; - } - - @Override - public ShapeRadioButton setStrokePressedColor(int color) { - mStrokePressedColor = color; - return this; - } - - @Override - public int getStrokePressedColor() { - return mStrokePressedColor; - } - - @Override - public ShapeRadioButton setStrokeCheckedColor(int color) { - mStrokeCheckedColor = color; - return this; - } - - @Override - public int getStrokeCheckedColor() { - return mStrokeCheckedColor; - } - - @Override - public ShapeRadioButton setStrokeDisabledColor(int color) { - mStrokeDisabledColor = color; - return this; - } - - @Override - public int getStrokeDisabledColor() { - return mStrokeDisabledColor; - } - - @Override - public ShapeRadioButton setStrokeFocusedColor(int color) { - mStrokeFocusedColor = color; - return this; - } - - @Override - public int getStrokeFocusedColor() { - return mStrokeFocusedColor; - } - - @Override - public ShapeRadioButton setStrokeSelectedColor(int color) { - mStrokeSelectedColor = color; - return this; - } - - @Override - public int getStrokeSelectedColor() { - return mStrokeSelectedColor; - } - - @Override - public ShapeRadioButton setStrokeWidth(int width) { - mStrokeWidth = width; - return this; - } - - @Override - public int getStrokeWidth() { - return mStrokeWidth; - } - - @Override - public ShapeRadioButton setDashWidth(int width) { - mDashWidth = width; - return this; - } - - @Override - public int getDashWidth() { - return mDashWidth; - } - - @Override - public ShapeRadioButton setDashGap(int gap) { - mDashGap = gap; - return this; - } - - @Override - public int getDashGap() { - return mDashGap; - } - - @Override - public ShapeRadioButton setInnerRadius(int radius) { - mInnerRadius = radius; - return this; - } - - @Override - public int getInnerRadius() { - return mInnerRadius; - } - - @Override - public ShapeRadioButton setInnerRadiusRatio(float ratio) { - mInnerRadiusRatio = ratio; - return this; - } - - @Override - public float getInnerRadiusRatio() { - return mInnerRadiusRatio; - } - - @Override - public ShapeRadioButton setThickness(int size) { - mThickness = size; - return this; - } - - @Override - public int getThickness() { - return mThickness; - } - - @Override - public ShapeRadioButton setThicknessRatio(float ratio) { - mThicknessRatio = ratio; - return this; - } - - @Override - public float getThicknessRatio() { - return mThicknessRatio; - } - - @Override - public ShapeRadioButton setShadowSize(int size) { - mShadowSize = size; - return this; - } - - @Override - public int getShadowSize() { - return mShadowSize; - } - - @Override - public ShapeRadioButton setShadowColor(int color) { - mShadowColor = color; - return this; } @Override - public int getShadowColor() { - return mShadowColor; - } - - /** - * 获取文本阴影颜色(避免和 TextView 原有的方法冲突) - */ - public int getTextShadowColor() { - return super.getShadowColor(); - } - - @Override - public ShapeRadioButton setShadowOffsetX(int offsetX) { - mShadowOffsetX = offsetX; - return this; - } - - @Override - public int getShadowOffsetX() { - return mShadowOffsetX; - } - - @Override - public ShapeRadioButton setShadowOffsetY(int offsetY) { - mShadowOffsetY = offsetY; - return this; - } - - @Override - public int getShadowOffsetY() { - return mShadowOffsetY; - } - - @Override - public void intoBackground() { - Drawable drawable = buildBackgroundDrawable(); - if (drawable == null) { + public void setButtonDrawable(Drawable drawable) { + super.setButtonDrawable(drawable); + if (mButtonDrawableBuilder == null) { return; } - if (isDashLineEnable() || isShadowEnable()) { - // 需要关闭硬件加速,否则虚线或者阴影在某些手机上面无法生效 - setLayerType(View.LAYER_TYPE_SOFTWARE, null); - } - setBackground(drawable); - } - - /** - * {@link IShapeTextColor} - */ - - @Override - public ShapeRadioButton setNormalTextColor(int color) { - mTextColor = color; - return this; - } - - @Override - public int getNormalTextColor() { - return mTextColor; + mButtonDrawableBuilder.setButtonDrawable(drawable); } - @Override - public ShapeRadioButton setTextPressedColor(int color) { - mTextPressedColor = color; - return this; - } - - @Override - public int getTextPressedColor() { - return mTextPressedColor; - } - - @Override - public ShapeRadioButton setTextCheckedColor(int color) { - mTextCheckedColor = color; - return this; + public ShapeDrawableBuilder getShapeDrawableBuilder() { + return mShapeDrawableBuilder; } - @Override - public int getTextCheckedColor() { - return mTextCheckedColor; - } - - @Override - public ShapeRadioButton setTextDisabledColor(int color) { - mTextDisabledColor = color; - return this; - } - - @Override - public int getTextDisabledColor() { - return mTextDisabledColor; - } - - @Override - public ShapeRadioButton setTextFocusedColor(int color) { - mTextFocusedColor = color; - return this; + public TextColorBuilder getTextColorBuilder() { + return mTextColorBuilder; } - @Override - public int getTextFocusedColor() { - return mTextFocusedColor; - } - - @Override - public ShapeRadioButton setTextSelectedColor(int color) { - mTextSelectedColor = color; - return this; - } - - @Override - public int getTextSelectedColor() { - return mTextSelectedColor; - } - - @Override - public void intoTextColor() { - setTextColor(buildColorState()); - } - - @Override - public ShapeRadioButton setTextStartColor(int color) { - mTextStartColor = color; - return this; - } - - @Override - public int getTextStartColor() { - return mTextStartColor; - } - - @Override - public ShapeRadioButton setTextCenterColor(int color) { - mTextCenterColor = color; - return this; - } - - @Override - public int getTextCenterColor() { - return mTextCenterColor; - } - - @Override - public ShapeRadioButton setTextEndColor(int color) { - mTextEndColor = color; - return this; - } - - @Override - public int getTextEndColor() { - return mTextEndColor; - } - - @Override - public ShapeRadioButton setTextGradientOrientation(int orientation) { - mTextGradientOrientation = orientation; - return this; - } - - @Override - public int getTextGradientOrientation() { - return mTextGradientOrientation; - } - - @Override - public void setText(CharSequence text, BufferType type) { - if (isTextGradientColor()) { - super.setText(buildLinearGradientSpannable(text), type); - } else { - super.setText(text, type); - } + public ButtonDrawableBuilder getButtonDrawableBuilder() { + return mButtonDrawableBuilder; } } \ No newline at end of file diff --git a/library/src/main/java/com/hjq/shape/view/ShapeTextView.java b/library/src/main/java/com/hjq/shape/view/ShapeTextView.java index eec892f..e376f26 100644 --- a/library/src/main/java/com/hjq/shape/view/ShapeTextView.java +++ b/library/src/main/java/com/hjq/shape/view/ShapeTextView.java @@ -2,14 +2,13 @@ import android.content.Context; import android.content.res.TypedArray; -import android.graphics.drawable.Drawable; import android.support.v7.widget.AppCompatTextView; import android.util.AttributeSet; -import android.view.View; import com.hjq.shape.R; -import com.hjq.shape.core.IShapeDrawable; -import com.hjq.shape.core.IShapeTextColor; +import com.hjq.shape.builder.ShapeDrawableBuilder; +import com.hjq.shape.builder.TextColorBuilder; +import com.hjq.shape.styleable.ShapeTextViewStyleable; /** * author : Android 轮子哥 @@ -17,64 +16,12 @@ * time : 2021/07/17 * desc : 支持直接定义 Shape 背景的 TextView */ -public class ShapeTextView extends AppCompatTextView implements - IShapeDrawable, IShapeTextColor { +public class ShapeTextView extends AppCompatTextView { - private int mShapeType; - private int mShapeWidth; - private int mShapeHeight; + private static final ShapeTextViewStyleable STYLEABLE = new ShapeTextViewStyleable(); - private int mSolidColor; - private int mSolidPressedColor; - private int mSolidDisabledColor; - private int mSolidFocusedColor; - private int mSolidSelectedColor; - - private int mTopLeftRadius; - private int mTopRightRadius; - private int mBottomLeftRadius; - private int mBottomRightRadius; - - private int mStartColor; - private int mCenterColor; - private int mEndColor; - private boolean mUseLevel; - private int mAngle; - private int mGradientType; - private float mCenterX; - private float mCenterY; - private int mGradientRadius; - - private int mStrokeColor; - private int mStrokePressedColor; - private int mStrokeDisabledColor; - private int mStrokeFocusedColor; - private int mStrokeSelectedColor; - - private int mStrokeWidth; - private int mDashWidth; - private int mDashGap; - - private int mInnerRadius; - private float mInnerRadiusRatio; - private int mThickness; - private float mThicknessRatio; - - private int mShadowSize; - private int mShadowColor; - private int mShadowOffsetX; - private int mShadowOffsetY; - - private int mTextColor; - private int mTextPressedColor; - private int mTextDisabledColor; - private int mTextFocusedColor; - private int mTextSelectedColor; - - private int mTextStartColor; - private int mTextCenterColor; - private int mTextEndColor; - private int mTextGradientOrientation; + private final ShapeDrawableBuilder mShapeDrawableBuilder; + private final TextColorBuilder mTextColorBuilder; public ShapeTextView(Context context) { this(context, null); @@ -88,654 +35,43 @@ public ShapeTextView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.ShapeTextView); - mShapeType = typedArray.getInt(R.styleable.ShapeTextView_shape, DEFAULT_SHAPE_TYPE); - mShapeWidth = typedArray.getDimensionPixelSize(R.styleable.ShapeTextView_shape_width, DEFAULT_SHAPE_WIDTH); - mShapeHeight = typedArray.getDimensionPixelSize(R.styleable.ShapeTextView_shape_height, DEFAULT_SHAPE_HEIGHT); - - mSolidColor = typedArray.getColor(R.styleable.ShapeTextView_shape_solidColor, DEFAULT_SHAPE_SOLID_COLOR); - mSolidPressedColor = typedArray.getColor(R.styleable.ShapeTextView_shape_solidPressedColor, mSolidColor); - mSolidDisabledColor = typedArray.getColor(R.styleable.ShapeTextView_shape_solidDisabledColor, mSolidColor); - mSolidFocusedColor = typedArray.getColor(R.styleable.ShapeTextView_shape_solidFocusedColor, mSolidColor); - mSolidSelectedColor = typedArray.getColor(R.styleable.ShapeTextView_shape_solidSelectedColor, mSolidColor); - - int radius = typedArray.getDimensionPixelSize(R.styleable.ShapeTextView_shape_radius, DEFAULT_SHAPE_RADIUS); - mTopLeftRadius = typedArray.getDimensionPixelSize(R.styleable.ShapeTextView_shape_topLeftRadius, radius); - mTopRightRadius = typedArray.getDimensionPixelSize(R.styleable.ShapeTextView_shape_topRightRadius, radius); - mBottomLeftRadius = typedArray.getDimensionPixelSize(R.styleable.ShapeTextView_shape_bottomLeftRadius, radius); - mBottomRightRadius = typedArray.getDimensionPixelSize(R.styleable.ShapeTextView_shape_bottomRightRadius, radius); - - mStartColor = typedArray.getColor(R.styleable.ShapeTextView_shape_startColor, mSolidColor); - mCenterColor = typedArray.getColor(R.styleable.ShapeTextView_shape_centerColor, mSolidColor); - mEndColor = typedArray.getColor(R.styleable.ShapeTextView_shape_endColor, mSolidColor); - mUseLevel = typedArray.getBoolean(R.styleable.ShapeTextView_shape_useLevel, DEFAULT_SHAPE_USE_LEVEL); - mAngle = (int) typedArray.getFloat(R.styleable.ShapeTextView_shape_angle, DEFAULT_SHAPE_ANGLE); - mGradientType = typedArray.getInt(R.styleable.ShapeTextView_shape_gradientType, DEFAULT_SHAPE_GRADIENT_TYPE); - mCenterX = typedArray.getFloat(R.styleable.ShapeTextView_shape_centerX, DEFAULT_SHAPE_CENTER_X); - mCenterY = typedArray.getFloat(R.styleable.ShapeTextView_shape_centerY, DEFAULT_SHAPE_CENTER_Y); - mGradientRadius = typedArray.getDimensionPixelSize(R.styleable.ShapeTextView_shape_gradientRadius, radius); - - mStrokeColor = typedArray.getColor(R.styleable.ShapeTextView_shape_strokeColor, DEFAULT_SHAPE_STROKE_COLOR); - mStrokePressedColor = typedArray.getColor(R.styleable.ShapeTextView_shape_strokePressedColor, mStrokeColor); - mStrokeDisabledColor = typedArray.getColor(R.styleable.ShapeTextView_shape_strokeDisabledColor, mStrokeColor); - mStrokeFocusedColor = typedArray.getColor(R.styleable.ShapeTextView_shape_strokeFocusedColor, mStrokeColor); - mStrokeSelectedColor = typedArray.getColor(R.styleable.ShapeTextView_shape_strokeSelectedColor, mStrokeColor); - - mStrokeWidth = typedArray.getDimensionPixelSize(R.styleable.ShapeTextView_shape_strokeWidth, DEFAULT_SHAPE_STROKE_WIDTH); - mDashWidth = typedArray.getDimensionPixelOffset(R.styleable.ShapeTextView_shape_dashWidth, DEFAULT_SHAPE_DASH_WIDTH); - mDashGap = typedArray.getDimensionPixelOffset(R.styleable.ShapeTextView_shape_dashGap, DEFAULT_SHAPE_DASH_GAP); - - mInnerRadius = typedArray.getDimensionPixelOffset(R.styleable.ShapeTextView_shape_innerRadius, DEFAULT_SHAPE_INNER_RADIUS); - mInnerRadiusRatio = typedArray.getFloat(R.styleable.ShapeTextView_shape_innerRadiusRatio, DEFAULT_SHAPE_INNER_RADIUS_RATIO); - mThickness = typedArray.getDimensionPixelOffset(R.styleable.ShapeTextView_shape_thickness, DEFAULT_SHAPE_THICKNESS); - mThicknessRatio = typedArray.getFloat(R.styleable.ShapeTextView_shape_thicknessRatio, DEFAULT_SHAPE_THICKNESS_RATIO); - - mShadowSize = typedArray.getDimensionPixelSize(R.styleable.ShapeTextView_shape_shadowSize, DEFAULT_SHAPE_SHADOW_SIZE); - mShadowColor = typedArray.getColor(R.styleable.ShapeTextView_shape_shadowColor, DEFAULT_SHAPE_SHADOW_COLOR); - mShadowOffsetX = typedArray.getDimensionPixelOffset(R.styleable.ShapeTextView_shape_shadowOffsetX, DEFAULT_SHAPE_SHADOW_OFFSET_X); - mShadowOffsetY = typedArray.getDimensionPixelOffset(R.styleable.ShapeTextView_shape_shadowOffsetY, DEFAULT_SHAPE_SHADOW_OFFSET_Y); - - mTextColor = typedArray.getColor(R.styleable.ShapeTextView_shape_textColor, getTextColors().getDefaultColor()); - mTextPressedColor = typedArray.getColor(R.styleable.ShapeTextView_shape_textPressedColor, getTextColors().getColorForState(new int[]{android.R.attr.state_pressed}, mTextColor)); - mTextDisabledColor = typedArray.getColor(R.styleable.ShapeTextView_shape_textDisabledColor, getTextColors().getColorForState(new int[]{-android.R.attr.state_enabled}, mTextColor)); - mTextFocusedColor = typedArray.getColor(R.styleable.ShapeTextView_shape_textFocusedColor, getTextColors().getColorForState(new int[]{android.R.attr.state_focused}, mTextColor)); - mTextSelectedColor = typedArray.getColor(R.styleable.ShapeTextView_shape_textSelectedColor, getTextColors().getColorForState(new int[]{android.R.attr.state_selected}, mTextColor)); - - mTextStartColor = typedArray.getColor(R.styleable.ShapeTextView_shape_textStartColor, mTextColor); - mTextCenterColor = typedArray.getColor(R.styleable.ShapeTextView_shape_textCenterColor, mTextColor); - mTextEndColor = typedArray.getColor(R.styleable.ShapeTextView_shape_textEndColor, mTextColor); - mTextGradientOrientation = typedArray.getColor(R.styleable.ShapeTextView_shape_textGradientOrientation, SHAPE_TEXT_GRADIENT_TYPE_HORIZONTAL); - + mShapeDrawableBuilder = new ShapeDrawableBuilder(this, typedArray, STYLEABLE); + mTextColorBuilder = new TextColorBuilder(this, typedArray, STYLEABLE); typedArray.recycle(); - intoBackground(); + mShapeDrawableBuilder.intoBackground(); - if (isTextGradientColor()) { + if (mTextColorBuilder.isTextGradientColor()) { setText(getText()); } else { - intoTextColor(); + mTextColorBuilder.intoTextColor(); } } @Override public void setTextColor(int color) { super.setTextColor(color); - mTextColor = color; - - clearTextGradientColor(); - } - - /** - * {@link IShapeDrawable} - */ - - @Override - public ShapeTextView setShapeType(int type) { - mShapeType = type; - return this; - } - - @Override - public int getShapeType() { - return mShapeType; - } - - @Override - public ShapeTextView setShapeWidth(int width) { - mShapeWidth = width; - return this; - } - - @Override - public int getShapeWidth() { - return mShapeWidth; - } - - @Override - public ShapeTextView setShapeHeight(int height) { - mShapeHeight = height; - return this; - } - - @Override - public int getShapeHeight() { - return mShapeHeight; - } - - @Override - public ShapeTextView setSolidColor(int color) { - if (mSolidPressedColor == mSolidColor) { - mSolidPressedColor = color; - } - if (mSolidDisabledColor == mSolidColor) { - mSolidDisabledColor = color; - } - if (mSolidFocusedColor == mSolidColor) { - mSolidFocusedColor = color; - } - if (mSolidSelectedColor == mSolidColor) { - mSolidSelectedColor = color; - } - mSolidColor = color; - mStartColor = color; - mCenterColor = color; - mEndColor = color; - return this; - } - - @Override - public int getSolidColor() { - return mSolidColor; - } - - @Override - public ShapeTextView setSolidPressedColor(int color) { - mSolidPressedColor = color; - return this; - } - - @Override - public int getSolidPressedColor() { - return mSolidPressedColor; - } - - @Override - public ShapeTextView setSolidDisabledColor(int color) { - mSolidDisabledColor = color; - return this; - } - - @Override - public int getSolidDisabledColor() { - return mSolidDisabledColor; - } - - @Override - public ShapeTextView setSolidFocusedColor(int color) { - mSolidFocusedColor = color; - return this; - } - - @Override - public int getSolidFocusedColor() { - return mSolidFocusedColor; - } - - @Override - public ShapeTextView setSolidSelectedColor(int color) { - mSolidSelectedColor = color; - return this; - } - - @Override - public int getSolidSelectedColor() { - return mSolidSelectedColor; - } - - @Override - public ShapeTextView setTopLeftRadius(int radius) { - mTopLeftRadius = radius; - return this; - } - - @Override - public int getTopLeftRadius() { - return mTopLeftRadius; - } - - @Override - public ShapeTextView setTopRightRadius(int radius) { - mTopRightRadius = radius; - return this; - } - - @Override - public int getTopRightRadius() { - return mTopRightRadius; - } - - @Override - public ShapeTextView setBottomLeftRadius(int radius) { - mBottomLeftRadius = radius; - return this; - } - - @Override - public int getBottomLeftRadius() { - return mBottomLeftRadius; - } - - @Override - public ShapeTextView setBottomRightRadius(int radius) { - mBottomRightRadius = radius; - return this; - } - - @Override - public int getBottomRightRadius() { - return mBottomRightRadius; - } - - @Override - public ShapeTextView setStartColor(int color) { - mStartColor = color; - return this; - } - - @Override - public int getStartColor() { - return mStartColor; - } - - @Override - public ShapeTextView setCenterColor(int color) { - mCenterColor = color; - return this; - } - - @Override - public int getCenterColor() { - return mCenterColor; - } - - @Override - public ShapeTextView setEndColor(int color) { - mEndColor = color; - return this; - } - - @Override - public int getEndColor() { - return mEndColor; - } - - @Override - public ShapeTextView setUseLevel(boolean useLevel) { - mUseLevel = useLevel; - return this; - } - - @Override - public boolean isUseLevel() { - return mUseLevel; - } - - @Override - public ShapeTextView setAngle(int angle) { - mAngle = angle; - return this; - } - - @Override - public int getAngle() { - return mAngle; - } - - @Override - public ShapeTextView setGradientType(int type) { - mGradientType = type; - return this; - } - - @Override - public int getGradientType() { - return mGradientType; - } - - @Override - public ShapeTextView setCenterX(float x) { - mCenterX = x; - return this; - } - - @Override - public float getCenterX() { - return mCenterX; - } - - @Override - public ShapeTextView setCenterY(float y) { - mCenterY = y; - return this; - } - - @Override - public float getCenterY() { - return mCenterY; - } - - @Override - public ShapeTextView setGradientRadius(int radius) { - mGradientRadius = radius; - return this; - } - - @Override - public int getGradientRadius() { - return mGradientRadius; - } - - @Override - public ShapeTextView setStrokeColor(int color) { - if (mStrokePressedColor == mStrokeColor) { - mStrokePressedColor = color; - } - if (mStrokeDisabledColor == mStrokeColor) { - mStrokeDisabledColor = color; - } - if (mStrokeFocusedColor == mStrokeColor) { - mStrokeFocusedColor = color; - } - if (mStrokeSelectedColor == mStrokeColor) { - mStrokeSelectedColor = color; - } - mStrokeColor = color; - return this; - } - - @Override - public int getStrokeColor() { - return mStrokeColor; - } - - @Override - public ShapeTextView setStrokePressedColor(int color) { - mStrokePressedColor = color; - return this; - } - - @Override - public int getStrokePressedColor() { - return mStrokePressedColor; - } - - @Override - public ShapeTextView setStrokeDisabledColor(int color) { - mStrokeDisabledColor = color; - return this; - } - - @Override - public int getStrokeDisabledColor() { - return mStrokeDisabledColor; - } - - @Override - public ShapeTextView setStrokeFocusedColor(int color) { - mStrokeFocusedColor = color; - return this; - } - - @Override - public int getStrokeFocusedColor() { - return mStrokeFocusedColor; - } - - @Override - public ShapeTextView setStrokeSelectedColor(int color) { - mStrokeSelectedColor = color; - return this; - } - - @Override - public int getStrokeSelectedColor() { - return mStrokeSelectedColor; - } - - @Override - public ShapeTextView setStrokeWidth(int width) { - mStrokeWidth = width; - return this; - } - - @Override - public int getStrokeWidth() { - return mStrokeWidth; - } - - @Override - public ShapeTextView setDashWidth(int width) { - mDashWidth = width; - return this; - } - - @Override - public int getDashWidth() { - return mDashWidth; - } - - @Override - public ShapeTextView setDashGap(int gap) { - mDashGap = gap; - return this; - } - - @Override - public int getDashGap() { - return mDashGap; - } - - @Override - public ShapeTextView setInnerRadius(int radius) { - mInnerRadius = radius; - return this; - } - - @Override - public int getInnerRadius() { - return mInnerRadius; - } - - @Override - public ShapeTextView setInnerRadiusRatio(float ratio) { - mInnerRadiusRatio = ratio; - return this; - } - - @Override - public float getInnerRadiusRatio() { - return mInnerRadiusRatio; - } - - @Override - public ShapeTextView setThickness(int size) { - mThickness = size; - return this; - } - - @Override - public int getThickness() { - return mThickness; - } - - @Override - public ShapeTextView setThicknessRatio(float ratio) { - mThicknessRatio = ratio; - return this; - } - - @Override - public float getThicknessRatio() { - return mThicknessRatio; - } - - @Override - public ShapeTextView setShadowSize(int size) { - mShadowSize = size; - return this; - } - - @Override - public int getShadowSize() { - return mShadowSize; - } - - @Override - public ShapeTextView setShadowColor(int color) { - mShadowColor = color; - return this; - } - - @Override - public int getShadowColor() { - return mShadowColor; - } - - /** - * 获取文本阴影颜色(避免和 TextView 原有的方法冲突) - */ - public int getTextShadowColor() { - return super.getShadowColor(); - } - - @Override - public ShapeTextView setShadowOffsetX(int offsetX) { - mShadowOffsetX = offsetX; - return this; - } - - @Override - public int getShadowOffsetX() { - return mShadowOffsetX; - } - - @Override - public ShapeTextView setShadowOffsetY(int offsetY) { - mShadowOffsetY = offsetY; - return this; - } - - @Override - public int getShadowOffsetY() { - return mShadowOffsetY; - } - - @Override - public void intoBackground() { - Drawable drawable = buildBackgroundDrawable(); - if (drawable == null) { + if (mTextColorBuilder == null) { return; } - if (isDashLineEnable() || isShadowEnable()) { - // 需要关闭硬件加速,否则虚线或者阴影在某些手机上面无法生效 - setLayerType(View.LAYER_TYPE_SOFTWARE, null); - } - setBackground(drawable); - } - - /** - * {@link IShapeTextColor} - */ - - @Override - public ShapeTextView setNormalTextColor(int color) { - mTextColor = color; - return this; - } - - @Override - public int getNormalTextColor() { - return mTextColor; - } - - @Override - public ShapeTextView setTextPressedColor(int color) { - mTextPressedColor = color; - return this; - } - - @Override - public int getTextPressedColor() { - return mTextPressedColor; - } - - @Override - public ShapeTextView setTextDisabledColor(int color) { - mTextDisabledColor = color; - return this; - } - - @Override - public int getTextDisabledColor() { - return mTextDisabledColor; - } - - @Override - public ShapeTextView setTextFocusedColor(int color) { - mTextFocusedColor = color; - return this; - } - - @Override - public int getTextFocusedColor() { - return mTextFocusedColor; - } - - @Override - public ShapeTextView setTextSelectedColor(int color) { - mTextSelectedColor = color; - return this; - } - - @Override - public int getTextSelectedColor() { - return mTextSelectedColor; - } - - @Override - public void intoTextColor() { - setTextColor(buildColorState()); - } - - @Override - public ShapeTextView setTextStartColor(int color) { - mTextStartColor = color; - return this; - } - - @Override - public int getTextStartColor() { - return mTextStartColor; - } - - @Override - public ShapeTextView setTextCenterColor(int color) { - mTextCenterColor = color; - return this; - } - - @Override - public int getTextCenterColor() { - return mTextCenterColor; - } - - @Override - public ShapeTextView setTextEndColor(int color) { - mTextEndColor = color; - return this; - } - - @Override - public int getTextEndColor() { - return mTextEndColor; - } - - @Override - public ShapeTextView setTextGradientOrientation(int orientation) { - mTextGradientOrientation = orientation; - return this; - } - - @Override - public int getTextGradientOrientation() { - return mTextGradientOrientation; + mTextColorBuilder.setTextColor(color); + mTextColorBuilder.clearTextGradientColor(); } @Override public void setText(CharSequence text, BufferType type) { - if (isTextGradientColor()) { - super.setText(buildLinearGradientSpannable(text), type); + if (mTextColorBuilder != null && mTextColorBuilder.isTextGradientColor()) { + super.setText(mTextColorBuilder.buildLinearGradientSpannable(text), type); } else { super.setText(text, type); } } + + public ShapeDrawableBuilder getShapeDrawableBuilder() { + return mShapeDrawableBuilder; + } + + public TextColorBuilder getTextColorBuilder() { + return mTextColorBuilder; + } } \ No newline at end of file diff --git a/library/src/main/java/com/hjq/shape/view/ShapeView.java b/library/src/main/java/com/hjq/shape/view/ShapeView.java index 0402b19..7c367a1 100644 --- a/library/src/main/java/com/hjq/shape/view/ShapeView.java +++ b/library/src/main/java/com/hjq/shape/view/ShapeView.java @@ -2,12 +2,12 @@ import android.content.Context; import android.content.res.TypedArray; -import android.graphics.drawable.Drawable; import android.util.AttributeSet; import android.view.View; import com.hjq.shape.R; -import com.hjq.shape.core.IShapeDrawable; +import com.hjq.shape.builder.ShapeDrawableBuilder; +import com.hjq.shape.styleable.ShapeViewStyleable; /** * author : Android 轮子哥 @@ -15,52 +15,11 @@ * time : 2021/07/17 * desc : 支持直接定义 Shape 背景的 View */ -public class ShapeView extends View implements IShapeDrawable { +public class ShapeView extends View { - private int mShapeType; - private int mShapeWidth; - private int mShapeHeight; + private static final ShapeViewStyleable STYLEABLE = new ShapeViewStyleable(); - private int mSolidColor; - private int mSolidPressedColor; - private int mSolidDisabledColor; - private int mSolidFocusedColor; - private int mSolidSelectedColor; - - private int mTopLeftRadius; - private int mTopRightRadius; - private int mBottomLeftRadius; - private int mBottomRightRadius; - - private int mStartColor; - private int mCenterColor; - private int mEndColor; - private boolean mUseLevel; - private int mAngle; - private int mGradientType; - private float mCenterX; - private float mCenterY; - private int mGradientRadius; - - private int mStrokeColor; - private int mStrokePressedColor; - private int mStrokeDisabledColor; - private int mStrokeFocusedColor; - private int mStrokeSelectedColor; - - private int mStrokeWidth; - private int mDashWidth; - private int mDashGap; - - private int mInnerRadius; - private float mInnerRadiusRatio; - private int mThickness; - private float mThicknessRatio; - - private int mShadowSize; - private int mShadowColor; - private int mShadowOffsetX; - private int mShadowOffsetY; + private final ShapeDrawableBuilder mShapeDrawableBuilder; public ShapeView(Context context) { this(context, null); @@ -74,505 +33,13 @@ public ShapeView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.ShapeView); - mShapeType = typedArray.getInt(R.styleable.ShapeView_shape, DEFAULT_SHAPE_TYPE); - mShapeWidth = typedArray.getDimensionPixelSize(R.styleable.ShapeView_shape_width, DEFAULT_SHAPE_WIDTH); - mShapeHeight = typedArray.getDimensionPixelSize(R.styleable.ShapeView_shape_height, DEFAULT_SHAPE_HEIGHT); - - mSolidColor = typedArray.getColor(R.styleable.ShapeView_shape_solidColor, DEFAULT_SHAPE_SOLID_COLOR); - mSolidPressedColor = typedArray.getColor(R.styleable.ShapeView_shape_solidPressedColor, mSolidColor); - mSolidDisabledColor = typedArray.getColor(R.styleable.ShapeView_shape_solidDisabledColor, mSolidColor); - mSolidFocusedColor = typedArray.getColor(R.styleable.ShapeView_shape_solidFocusedColor, mSolidColor); - mSolidSelectedColor = typedArray.getColor(R.styleable.ShapeView_shape_solidSelectedColor, mSolidColor); - - int radius = typedArray.getDimensionPixelSize(R.styleable.ShapeView_shape_radius, DEFAULT_SHAPE_RADIUS); - mTopLeftRadius = typedArray.getDimensionPixelSize(R.styleable.ShapeView_shape_topLeftRadius, radius); - mTopRightRadius = typedArray.getDimensionPixelSize(R.styleable.ShapeView_shape_topRightRadius, radius); - mBottomLeftRadius = typedArray.getDimensionPixelSize(R.styleable.ShapeView_shape_bottomLeftRadius, radius); - mBottomRightRadius = typedArray.getDimensionPixelSize(R.styleable.ShapeView_shape_bottomRightRadius, radius); - - mStartColor = typedArray.getColor(R.styleable.ShapeView_shape_startColor, mSolidColor); - mCenterColor = typedArray.getColor(R.styleable.ShapeView_shape_centerColor, mSolidColor); - mEndColor = typedArray.getColor(R.styleable.ShapeView_shape_endColor, mSolidColor); - mUseLevel = typedArray.getBoolean(R.styleable.ShapeView_shape_useLevel, DEFAULT_SHAPE_USE_LEVEL); - mAngle = (int) typedArray.getFloat(R.styleable.ShapeView_shape_angle, DEFAULT_SHAPE_ANGLE); - mGradientType = typedArray.getInt(R.styleable.ShapeView_shape_gradientType, DEFAULT_SHAPE_GRADIENT_TYPE); - mCenterX = typedArray.getFloat(R.styleable.ShapeView_shape_centerX, DEFAULT_SHAPE_CENTER_X); - mCenterY = typedArray.getFloat(R.styleable.ShapeView_shape_centerY, DEFAULT_SHAPE_CENTER_Y); - mGradientRadius = typedArray.getDimensionPixelSize(R.styleable.ShapeView_shape_gradientRadius, radius); - - mStrokeColor = typedArray.getColor(R.styleable.ShapeView_shape_strokeColor, DEFAULT_SHAPE_STROKE_COLOR); - mStrokePressedColor = typedArray.getColor(R.styleable.ShapeView_shape_strokePressedColor, mStrokeColor); - mStrokeDisabledColor = typedArray.getColor(R.styleable.ShapeView_shape_strokeDisabledColor, mStrokeColor); - mStrokeFocusedColor = typedArray.getColor(R.styleable.ShapeView_shape_strokeFocusedColor, mStrokeColor); - mStrokeSelectedColor = typedArray.getColor(R.styleable.ShapeView_shape_strokeSelectedColor, mStrokeColor); - - mStrokeWidth = typedArray.getDimensionPixelSize(R.styleable.ShapeView_shape_strokeWidth, DEFAULT_SHAPE_STROKE_WIDTH); - mDashWidth = typedArray.getDimensionPixelSize(R.styleable.ShapeView_shape_dashWidth, DEFAULT_SHAPE_DASH_WIDTH); - mDashGap = typedArray.getDimensionPixelSize(R.styleable.ShapeView_shape_dashGap, DEFAULT_SHAPE_DASH_GAP); - - mInnerRadius = typedArray.getDimensionPixelOffset(R.styleable.ShapeView_shape_innerRadius, DEFAULT_SHAPE_INNER_RADIUS); - mInnerRadiusRatio = typedArray.getFloat(R.styleable.ShapeView_shape_innerRadiusRatio, DEFAULT_SHAPE_INNER_RADIUS_RATIO); - mThickness = typedArray.getDimensionPixelOffset(R.styleable.ShapeView_shape_thickness, DEFAULT_SHAPE_THICKNESS); - mThicknessRatio = typedArray.getFloat(R.styleable.ShapeView_shape_thicknessRatio, DEFAULT_SHAPE_THICKNESS_RATIO); - - mShadowSize = typedArray.getDimensionPixelSize(R.styleable.ShapeView_shape_shadowSize, DEFAULT_SHAPE_SHADOW_SIZE); - mShadowColor = typedArray.getColor(R.styleable.ShapeView_shape_shadowColor, DEFAULT_SHAPE_SHADOW_COLOR); - mShadowOffsetX = typedArray.getDimensionPixelOffset(R.styleable.ShapeView_shape_shadowOffsetX, DEFAULT_SHAPE_SHADOW_OFFSET_X); - mShadowOffsetY = typedArray.getDimensionPixelOffset(R.styleable.ShapeView_shape_shadowOffsetY, DEFAULT_SHAPE_SHADOW_OFFSET_Y); - + mShapeDrawableBuilder = new ShapeDrawableBuilder(this, typedArray, STYLEABLE); typedArray.recycle(); - intoBackground(); - } - - /** - * {@link IShapeDrawable} - */ - - @Override - public ShapeView setShapeType(int type) { - mShapeType = type; - return this; - } - - @Override - public int getShapeType() { - return mShapeType; - } - - @Override - public ShapeView setShapeWidth(int width) { - mShapeWidth = width; - return this; - } - - @Override - public int getShapeWidth() { - return mShapeWidth; - } - - @Override - public ShapeView setShapeHeight(int height) { - mShapeHeight = height; - return this; - } - - @Override - public int getShapeHeight() { - return mShapeHeight; - } - - @Override - public ShapeView setSolidColor(int color) { - if (mSolidPressedColor == mSolidColor) { - mSolidPressedColor = color; - } - if (mSolidDisabledColor == mSolidColor) { - mSolidDisabledColor = color; - } - if (mSolidFocusedColor == mSolidColor) { - mSolidFocusedColor = color; - } - if (mSolidSelectedColor == mSolidColor) { - mSolidSelectedColor = color; - } - mSolidColor = color; - mStartColor = color; - mCenterColor = color; - mEndColor = color; - return this; - } - - @Override - public int getSolidColor() { - return mSolidColor; - } - - @Override - public ShapeView setSolidPressedColor(int color) { - mSolidPressedColor = color; - return this; - } - - @Override - public int getSolidPressedColor() { - return mSolidPressedColor; - } - - @Override - public ShapeView setSolidDisabledColor(int color) { - mSolidDisabledColor = color; - return this; - } - - @Override - public int getSolidDisabledColor() { - return mSolidDisabledColor; - } - - @Override - public ShapeView setSolidFocusedColor(int color) { - mSolidFocusedColor = color; - return this; - } - - @Override - public int getSolidFocusedColor() { - return mSolidFocusedColor; - } - - @Override - public ShapeView setSolidSelectedColor(int color) { - mSolidSelectedColor = color; - return this; - } - - @Override - public int getSolidSelectedColor() { - return mSolidSelectedColor; - } - - @Override - public ShapeView setTopLeftRadius(int radius) { - mTopLeftRadius = radius; - return this; - } - - @Override - public int getTopLeftRadius() { - return mTopLeftRadius; - } - - @Override - public ShapeView setTopRightRadius(int radius) { - mTopRightRadius = radius; - return this; - } - - @Override - public int getTopRightRadius() { - return mTopRightRadius; - } - - @Override - public ShapeView setBottomLeftRadius(int radius) { - mBottomLeftRadius = radius; - return this; - } - - @Override - public int getBottomLeftRadius() { - return mBottomLeftRadius; - } - - @Override - public ShapeView setBottomRightRadius(int radius) { - mBottomRightRadius = radius; - return this; - } - - @Override - public int getBottomRightRadius() { - return mBottomRightRadius; - } - - @Override - public ShapeView setStartColor(int color) { - mStartColor = color; - return this; - } - - @Override - public int getStartColor() { - return mStartColor; - } - - @Override - public ShapeView setCenterColor(int color) { - mCenterColor = color; - return this; - } - - @Override - public int getCenterColor() { - return mCenterColor; - } - - @Override - public ShapeView setEndColor(int color) { - mEndColor = color; - return this; - } - - @Override - public int getEndColor() { - return mEndColor; - } - - @Override - public ShapeView setUseLevel(boolean useLevel) { - mUseLevel = useLevel; - return this; - } - - @Override - public boolean isUseLevel() { - return mUseLevel; - } - - @Override - public ShapeView setAngle(int angle) { - mAngle = angle; - return this; - } - - @Override - public int getAngle() { - return mAngle; - } - - @Override - public ShapeView setGradientType(int type) { - mGradientType = type; - return this; - } - - @Override - public int getGradientType() { - return mGradientType; - } - - @Override - public ShapeView setCenterX(float x) { - mCenterX = x; - return this; - } - - @Override - public float getCenterX() { - return mCenterX; - } - - @Override - public ShapeView setCenterY(float y) { - mCenterY = y; - return this; - } - - @Override - public float getCenterY() { - return mCenterY; - } - - @Override - public ShapeView setGradientRadius(int radius) { - mGradientRadius = radius; - return this; - } - - @Override - public int getGradientRadius() { - return mGradientRadius; - } - - @Override - public ShapeView setStrokeColor(int color) { - if (mStrokePressedColor == mStrokeColor) { - mStrokePressedColor = color; - } - if (mStrokeDisabledColor == mStrokeColor) { - mStrokeDisabledColor = color; - } - if (mStrokeFocusedColor == mStrokeColor) { - mStrokeFocusedColor = color; - } - if (mStrokeSelectedColor == mStrokeColor) { - mStrokeSelectedColor = color; - } - mStrokeColor = color; - return this; - } - - @Override - public int getStrokeColor() { - return mStrokeColor; - } - - @Override - public ShapeView setStrokePressedColor(int color) { - mStrokePressedColor = color; - return this; - } - - @Override - public int getStrokePressedColor() { - return mStrokePressedColor; - } - - @Override - public ShapeView setStrokeDisabledColor(int color) { - mStrokeDisabledColor = color; - return this; - } - - @Override - public int getStrokeDisabledColor() { - return mStrokeDisabledColor; - } - - @Override - public ShapeView setStrokeFocusedColor(int color) { - mStrokeFocusedColor = color; - return this; - } - - @Override - public int getStrokeFocusedColor() { - return mStrokeFocusedColor; - } - - @Override - public ShapeView setStrokeSelectedColor(int color) { - mStrokeSelectedColor = color; - return this; - } - - @Override - public int getStrokeSelectedColor() { - return mStrokeSelectedColor; - } - - @Override - public ShapeView setStrokeWidth(int width) { - mStrokeWidth = width; - return this; - } - - @Override - public int getStrokeWidth() { - return mStrokeWidth; - } - - @Override - public ShapeView setDashWidth(int width) { - mDashWidth = width; - return this; - } - - @Override - public int getDashWidth() { - return mDashWidth; - } - - @Override - public ShapeView setDashGap(int gap) { - mDashGap = gap; - return this; - } - - @Override - public int getDashGap() { - return mDashGap; - } - - @Override - public ShapeView setInnerRadius(int radius) { - mInnerRadius = radius; - return this; - } - - @Override - public int getInnerRadius() { - return mInnerRadius; - } - - @Override - public ShapeView setInnerRadiusRatio(float ratio) { - mInnerRadiusRatio = ratio; - return this; - } - - @Override - public float getInnerRadiusRatio() { - return mInnerRadiusRatio; - } - - @Override - public ShapeView setThickness(int size) { - mThickness = size; - return this; - } - - @Override - public int getThickness() { - return mThickness; - } - - @Override - public ShapeView setThicknessRatio(float ratio) { - mThicknessRatio = ratio; - return this; - } - - @Override - public float getThicknessRatio() { - return mThicknessRatio; - } - - @Override - public ShapeView setShadowSize(int size) { - mShadowSize = size; - return this; - } - - @Override - public int getShadowSize() { - return mShadowSize; - } - - @Override - public ShapeView setShadowColor(int color) { - mShadowColor = color; - return this; - } - - @Override - public int getShadowColor() { - return mShadowColor; - } - - @Override - public ShapeView setShadowOffsetX(int offsetX) { - mShadowOffsetX = offsetX; - return this; - } - - @Override - public int getShadowOffsetX() { - return mShadowOffsetX; - } - - @Override - public ShapeView setShadowOffsetY(int offsetY) { - mShadowOffsetY = offsetY; - return this; - } - - @Override - public int getShadowOffsetY() { - return mShadowOffsetY; + mShapeDrawableBuilder.intoBackground(); } - @Override - public void intoBackground() { - Drawable drawable = buildBackgroundDrawable(); - if (drawable == null) { - return; - } - if (isDashLineEnable() || isShadowEnable()) { - // 需要关闭硬件加速,否则虚线或者阴影在某些手机上面无法生效 - setLayerType(View.LAYER_TYPE_SOFTWARE, null); - } - setBackground(drawable); + public ShapeDrawableBuilder getShapeDrawableBuilder() { + return mShapeDrawableBuilder; } } \ No newline at end of file diff --git a/library/src/main/res/values/attrs.xml b/library/src/main/res/values/attrs.xml index 04e6a3e..93ba6e7 100644 --- a/library/src/main/res/values/attrs.xml +++ b/library/src/main/res/values/attrs.xml @@ -34,7 +34,7 @@ - + @@ -132,6 +132,19 @@ + + + + + + + + + + + + + @@ -295,6 +308,13 @@ + + + + + + + @@ -352,6 +372,13 @@ + + + + + + + diff --git a/picture/shape_select_background.jpg b/picture/shape_select_background.jpg index 75032d0..129f442 100644 Binary files a/picture/shape_select_background.jpg and b/picture/shape_select_background.jpg differ diff --git a/picture/shape_select_compound_button.jpg b/picture/shape_select_compound_button.jpg new file mode 100644 index 0000000..b3613a4 Binary files /dev/null and b/picture/shape_select_compound_button.jpg differ