From 712c2576d828a4307691790ea5234d50a3c3573d Mon Sep 17 00:00:00 2001 From: 15296721452 <123> Date: Mon, 19 Apr 2021 18:43:23 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90=E4=BF=AE=E6=94=B9=E5=86=85=E5=AE=B9?= =?UTF-8?q?=E3=80=91checkstyle=EF=BC=8CChangelog?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ChangeLog.md | 5 + README.md | 2 +- .../rwidgethelper/slice/MainAbilitySlice.java | 2 +- .../library/widget/helper/RBaseHelper.java | 73 ++++++----- .../widget/helper/RImageViewHelper.java | 39 +++--- .../widget/helper/RTextViewHelper.java | 113 ++++++++++-------- .../widget/iface/ITextViewFeature.java | 16 +++ .../library/widget/rounded/RoundDrawable.java | 64 +++++----- .../library/widget/utils/AttrUtils.java | 3 +- .../ruffian/library/widget/utils/LogUtil.java | 1 + .../library/widget/utils/ShadowDrawable.java | 83 +++++++++++-- .../library/widget/utils/TextViewUtils.java | 17 ++- .../library/widget/utils/TypedAttrUtils.java | 8 ++ 13 files changed, 285 insertions(+), 141 deletions(-) create mode 100644 ChangeLog.md diff --git a/ChangeLog.md b/ChangeLog.md new file mode 100644 index 0000000..d7b86e8 --- /dev/null +++ b/ChangeLog.md @@ -0,0 +1,5 @@ +## 0.0.2-SNAPSHOT +* optimization: Optimize code + +## 0.0.1-SNAPSHOT +* refactor: For module library Android-to-OpenHarmony \ No newline at end of file diff --git a/README.md b/README.md index 9ba7d53..bc04524 100644 --- a/README.md +++ b/README.md @@ -183,7 +183,7 @@ VirusTotal病毒安全检测通过 #### 版本迭代 -- 0.0.1-SNAPSHOT +- 0.0.2-SNAPSHOT ## 版权和许可信息 diff --git a/entry/src/main/java/com/example/rwidgethelper/slice/MainAbilitySlice.java b/entry/src/main/java/com/example/rwidgethelper/slice/MainAbilitySlice.java index f2d8756..cc42a55 100644 --- a/entry/src/main/java/com/example/rwidgethelper/slice/MainAbilitySlice.java +++ b/entry/src/main/java/com/example/rwidgethelper/slice/MainAbilitySlice.java @@ -26,12 +26,12 @@ public class MainAbilitySlice extends AbilitySlice implements Component.ClickedL @Override public void onActive() { super.onActive(); + btnFlag = false; } @Override public void onForeground(Intent intent) { super.onForeground(intent); - btnFlag = false; } @Override diff --git a/library/src/main/java/com/ruffian/library/widget/helper/RBaseHelper.java b/library/src/main/java/com/ruffian/library/widget/helper/RBaseHelper.java index 1131de2..1ccf91d 100644 --- a/library/src/main/java/com/ruffian/library/widget/helper/RBaseHelper.java +++ b/library/src/main/java/com/ruffian/library/widget/helper/RBaseHelper.java @@ -484,9 +484,11 @@ public class RBaseHelper implements IClip, Component.Layout } } - /********************* - * Gradient - ********************/ + /** + * 渐变 圆角 + * + * @return 圆角 + */ public float getGradientRadius() { return mGradientRadius; } @@ -528,7 +530,7 @@ public class RBaseHelper implements IClip, Component.Layout * 设置渐变样式/类型 * * @param gradientType {LINEAR_GRADIENT=0, RADIAL_GRADIENT=1, SWEEP_GRADIENT=2} - * @return + * @return 上下文 */ public RBaseHelper setGradientType(int gradientType) { if (gradientType < 0 || gradientType > 2) { @@ -560,10 +562,16 @@ public class RBaseHelper implements IClip, Component.Layout mBackgroundSelected.setCornerRadius(mGradientRadius); } - /********************* - * BackgroundColor - ********************/ - + /** + * 背景颜色 + * + * @param normal 默认 + * @param pressed 选中时 + * @param unable 初始 + * @param checked 选中后 + * @param selected 选中后 + * @return 上下文 + */ public RBaseHelper setStateBackgroundColor(Color normal, Color pressed, Color unable, Color checked, Color selected) { mBackgroundColorNormal = normal; mBackgroundColorPressed = pressed; @@ -922,9 +930,7 @@ public class RBaseHelper implements IClip, Component.Layout hasCustom = true; } - /** - * 未设置自定义属性,获取原生背景并且设置 - */ + // 未设置自定义属性,获取原生背景并且设置 if (!hasCustom && !useRipple()) { mBackgroundDrawable = mViewBackground; // 使用原生背景 @@ -966,10 +972,11 @@ public class RBaseHelper implements IClip, Component.Layout } } - /********************* - * Shadow - *********************/ - + /** + * 阴影 + * + * @return 默认 + */ public boolean useShadow() { return mShadowRadius >= 0; } @@ -1014,9 +1021,12 @@ public class RBaseHelper implements IClip, Component.Layout return mShadowDy; } - /********************* - * Ripple - *********************/ + /** + * 波纹 + * + * @param useRipple + * @return 上下文 + */ public RBaseHelper setUseRipple(boolean useRipple) { this.isUseRipple = useRipple; @@ -1051,9 +1061,12 @@ public class RBaseHelper implements IClip, Component.Layout return mRippleMaskDrawable; } - /********************* - * border - *********************/ + /** + * 边框 + * + * @param width 宽度 + * @return 上下文 + */ public RBaseHelper setBorderWidthNormal(int width) { this.mBorderWidthNormal = width; @@ -1302,9 +1315,11 @@ public class RBaseHelper implements IClip, Component.Layout setBackgroundState(); } - /********************* - * radius - ********************/ + /** + * 圆角 + * + * @param radius float类型 + */ public void setCornerRadius(float radius) { this.mCornerRadius = radius; @@ -1429,7 +1444,7 @@ public class RBaseHelper implements IClip, Component.Layout * 数组[3]:图片资源drawable,可能为null * 单个图片背景 或多个颜色背景 * - * @return + * @return 背景 */ private Object[] getBackgroundInfo(AttrSet attr, String key) { Color[] colors = null; @@ -1467,7 +1482,7 @@ public class RBaseHelper implements IClip, Component.Layout /** * 获取渐变方向 * - * @return + * @return 方向 */ private ShapeElement.Orientation getGradientOrientation(AttrSet attr) { String orientationValue = AttrUtils.getString(attr, mGradient, BL_TR).toUpperCase(); @@ -1506,7 +1521,7 @@ public class RBaseHelper implements IClip, Component.Layout * * @param element GradientDrawable * @param colors 颜色数组 - * @return + * @return 背景颜色 */ private ShapeElement setColors(ShapeElement element, int[] colors) { if (element == null) { @@ -1528,7 +1543,7 @@ public class RBaseHelper implements IClip, Component.Layout * * @param x * @param y - * @return + * @return 数值 */ protected boolean isOutsideView(int x, int y) { boolean flag = false; @@ -1543,7 +1558,7 @@ public class RBaseHelper implements IClip, Component.Layout /** * 是否使用Ripple * - * @return + * @return true 是,反之false */ private boolean isUseRipple() { return isUseRipple; diff --git a/library/src/main/java/com/ruffian/library/widget/helper/RImageViewHelper.java b/library/src/main/java/com/ruffian/library/widget/helper/RImageViewHelper.java index 32bd224..ed0b549 100644 --- a/library/src/main/java/com/ruffian/library/widget/helper/RImageViewHelper.java +++ b/library/src/main/java/com/ruffian/library/widget/helper/RImageViewHelper.java @@ -126,7 +126,6 @@ public class RImageViewHelper { mIconSelected = mIconNormal; } - // unable,focused,pressed,selected,normal states[0] = new int[]{COMPONENT_STATE_DISABLED}; // unable states[1] = new int[]{COMPONENT_STATE_PRESSED}; // pressed @@ -145,7 +144,6 @@ public class RImageViewHelper { * 初始化设置 */ private void init() { - // 统一设置圆角弧度优先 updateCornerBorderRadii(); updateCornerBitmapRadii(); @@ -193,7 +191,6 @@ public class RImageViewHelper { * @param canvas */ private void drawBitmap(Canvas canvas) { - //drawable Element drawable = mView.getImageElement(); if (drawable == null) { @@ -234,14 +231,13 @@ public class RImageViewHelper { * * @param element * @param scaleType - * @param bmpW 图片宽 - * @param bmpH 图片高 - * @param w 控件宽 - * @param h 控件高 - * @return + * @param bmpW 图片宽 + * @param bmpH 图片高 + * @param w 控件宽 + * @param h 控件高 + * @return 图片 */ private PixelMap getBitmapFromDrawable(Element element, Image.ScaleMode scaleType, int bmpW, int bmpH, int w, int h) { - /** * 支持padding 考虑边框宽度 */ @@ -389,9 +385,12 @@ public class RImageViewHelper { mView.invalidate(); } - /************************ - * Icon - ************************/ + /** + * 颜色 + * + * @param icon 颜色值 + * @return this + */ public RImageViewHelper setIconNormal(Element icon) { this.mIconNormal = icon; @@ -447,9 +446,11 @@ public class RImageViewHelper { mView.invalidate(); } - /************************ - * Border - ************************/ + /** + * 边框宽度 + * + * @return 宽度 + */ public int getBorderWidth() { return mBorderWidth; @@ -471,9 +472,11 @@ public class RImageViewHelper { return this; } - /************************ - * Corner - ************************/ + /** + * 角 + * + * @return 角度 + */ public float getCorner() { return mCorner; } diff --git a/library/src/main/java/com/ruffian/library/widget/helper/RTextViewHelper.java b/library/src/main/java/com/ruffian/library/widget/helper/RTextViewHelper.java index b4a1acd..a544477 100644 --- a/library/src/main/java/com/ruffian/library/widget/helper/RTextViewHelper.java +++ b/library/src/main/java/com/ruffian/library/widget/helper/RTextViewHelper.java @@ -41,7 +41,9 @@ public class RTextViewHelper extends RBaseHelper implements ITextViewFeatu protected Color mTextColorUnable; protected Color mTextColorSelected; protected int[][] states = new int[6][]; + // Icon(兼容老版本) + private Element mIcon = null; private Element mIconNormal; private Element mIconPressed; @@ -85,10 +87,12 @@ public class RTextViewHelper extends RBaseHelper implements ITextViewFeatu private Element mIconPressedRight; private Element mIconUnableRight; private Element mIconSelectedRight; + // typeface + private String mTypefacePath; - private boolean mDrawableWithText = false; + private boolean isDrawableWithText = false; // TextView本身设置的padding protected int mPaddingLeft, mPaddingRight, mPaddingTop, mPaddingBottom; @@ -102,7 +106,7 @@ public class RTextViewHelper extends RBaseHelper implements ITextViewFeatu private float zj; private Paint paint; private Color text_ripple_color; - private boolean is_ripple; + private boolean isRipple; public RTextViewHelper(Context context, Text view, AttrSet attrs) { super(context, view, attrs); @@ -119,20 +123,23 @@ public class RTextViewHelper extends RBaseHelper implements ITextViewFeatu paint.setStyle(Paint.Style.FILL_STYLE); animatorValue.setDuration(500); animatorValue.setCurveType(Animator.CurveType.LINEAR); + // 监听View改变 + addOnViewChangeListener(); } /** - * 设置View变化监听 + * 设置变化监听 */ private void addOnViewChangeListener() { if (mView == null) { return; } - if (!mDrawableWithText) { + if (!isDrawableWithText) { return; } + // 文本改变 mView.addTextObserver(this::onTextUpdated); } @@ -149,7 +156,6 @@ public class RTextViewHelper extends RBaseHelper implements ITextViewFeatu * @param attrs */ private void initAttributeSet(Context context, AttrSet attrs) { - if (context == null || attrs == null) { setup(); return; @@ -159,8 +165,7 @@ public class RTextViewHelper extends RBaseHelper implements ITextViewFeatu // icon text_ripple_color = AttrUtils.getStringToColor(attrs, "text_ripple_color", Color.TRANSPARENT); - is_ripple = AttrUtils.getStringToBool(attrs, "text_ripple", false); - + isRipple = AttrUtils.getStringToBool(attrs, "text_ripple", false); mIconNormalLeft = TypedAttrUtils.getDrawable(attrs, "icon_normal_left", null); mIconPressedLeft = TypedAttrUtils.getDrawable(attrs, "icon_pressed_left", null); mIconUnableLeft = TypedAttrUtils.getDrawable(attrs, "icon_unable_left", null); @@ -176,8 +181,8 @@ public class RTextViewHelper extends RBaseHelper implements ITextViewFeatu mIconNormalBottom = TypedAttrUtils.getDrawable(attrs, "icon_normal_bottom", null); mIconPressedBottom = TypedAttrUtils.getDrawable(attrs, "icon_pressed_bottom", null); mIconUnableBottom = TypedAttrUtils.getDrawable(attrs, "icon_unable_bottom", null); - mIconSelectedBottom = TypedAttrUtils.getDrawable(attrs, "icon_selected_bottom", null); + // 版本兼容代码 mIconNormal = TypedAttrUtils.getDrawable(attrs, "icon_src_normal", null); mIconPressed = TypedAttrUtils.getDrawable(attrs, "icon_src_pressed", null); @@ -193,6 +198,7 @@ public class RTextViewHelper extends RBaseHelper implements ITextViewFeatu mIconHeightBottom = TypedAttrUtils.getDimensionPixelSize(attrs, "icon_height_bottom", 0); mIconWidthTop = TypedAttrUtils.getDimensionPixelSize(attrs, "icon_width_top", 0); mIconHeightTop = TypedAttrUtils.getDimensionPixelSize(attrs, "icon_height_top", 0); + // 兼容代码 mIconWidth = TypedAttrUtils.getDimensionPixelSize(attrs, "icon_width", 0); mIconHeight = TypedAttrUtils.getDimensionPixelSize(attrs, "icon_height", 0); @@ -201,11 +207,12 @@ public class RTextViewHelper extends RBaseHelper implements ITextViewFeatu mTextColorPressed = AttrUtils.getStringToColor(attrs, "text_color_pressed", mView.getTextColor()); mTextColorUnable = AttrUtils.getStringToColor(attrs, "text_color_unable", mView.getTextColor()); mTextColorSelected = AttrUtils.getStringToColor(attrs, "text_color_selected", mView.getTextColor()); + // typeface mTypefacePath = TypedAttrUtils.getString(attrs, "text_typeface", null); // drawableWithText - mDrawableWithText = AttrUtils.getStringToBool(attrs, "icon_with_text", false); + isDrawableWithText = AttrUtils.getStringToBool(attrs, "icon_with_text", false); isHasPressedTextColor = mTextColorPressed != null; isHasUnableTextColor = mTextColorUnable != null; isHasSelectedTextColor = mTextColorSelected != null; @@ -273,9 +280,12 @@ public class RTextViewHelper extends RBaseHelper implements ITextViewFeatu setTypeface(); } - /************************ - * Typeface - ************************/ + /** + * 字型 + * + * @param typefacePath 路径 + * @return this + */ public RTextViewHelper setTypeface(String typefacePath) { this.mTypefacePath = typefacePath; @@ -439,7 +449,10 @@ public class RTextViewHelper extends RBaseHelper implements ITextViewFeatu } /** - * ******* 新版本逻辑******** + * 默认背景色 + * + * @param icon 背景 + * @return this */ public RTextViewHelper setIconNormalLeft(Element icon) { this.mIconNormalLeft = icon; @@ -574,8 +587,8 @@ public class RTextViewHelper extends RBaseHelper implements ITextViewFeatu /** * 未选中的置于左方图片 * - * @param icon - * @return + * @param icon 初始左边icon + * @return this */ public RTextViewHelper setIconUnableLeft(Element icon) { this.mIconUnableLeft = icon; @@ -587,8 +600,8 @@ public class RTextViewHelper extends RBaseHelper implements ITextViewFeatu /** * 未选中的置于右方图片 * - * @param icon - * @return + * @param icon 初始右边icon + * @return this */ public RTextViewHelper setIconUnableRight(Element icon) { this.mIconUnableRight = icon; @@ -797,7 +810,8 @@ public class RTextViewHelper extends RBaseHelper implements ITextViewFeatu /** * 主要用于子类调用 - * 备注:用于库内确定逻辑的调用,不建议开发者直接调用 + * + * @param element 背景 */ @Deprecated @SuppressWarnings("unchecked") @@ -808,8 +822,8 @@ public class RTextViewHelper extends RBaseHelper implements ITextViewFeatu /** * 主要用于子类调用 - * 备注:用于库内确定逻辑的调用,不建议开发者直接调用 - * 备注:添加上下左右之后此处功能默认 drawableLeft + * + * @param element 背景 */ @SuppressWarnings("unchecked") protected void setIconLeft(Element element) { @@ -902,14 +916,14 @@ public class RTextViewHelper extends RBaseHelper implements ITextViewFeatu if (drawableBottom != null) { drawableBottom.setBounds(0, 0, mIconWidthBottom, mIconHeightBottom); } + // setDrawable mView.setAroundElements(drawableLeft, drawableTop, drawableRight, drawableBottom); + // drawable间距 - Log.i("---- mDrawableWithText" + mDrawableWithText); - if (!mDrawableWithText) { + if (!isDrawableWithText) { return; } - int drawablePadding = mView.getAroundElementsPadding(); int drawablePaddingHorizontal = 0, drawablePaddingVertical = 0; if (mIconLeft != null) { @@ -936,29 +950,27 @@ public class RTextViewHelper extends RBaseHelper implements ITextViewFeatu float bodyWidth = textWidth + drawableWidthFinal + drawablePaddingHorizontalFinal + 200; // 内容宽度 float actualWidth = mView.getWidth() - (mPaddingLeft + mPaddingRight); // 实际可用宽度 int translateX = (int) (actualWidth - bodyWidth) / 2; // 两边使用 - if (translateX < 0) { + if (0 > translateX) { translateX = 0; } + // 垂直方向计算 float textHeight = TextViewUtils.get().getTextHeight(mView, drawableHeightFinal, mPaddingTop, mPaddingBottom, drawablePaddingVerticalFinal); float bodyHeight = textHeight + drawableHeightFinal + drawablePaddingVerticalFinal; // 内容高度 float actualHeight = mView.getHeight() - (mPaddingTop + mPaddingBottom); // 实际可用高度 int translateY = (int) (actualHeight - bodyHeight) / 2; - if (translateY < 0) { + if (0 > translateY) { translateY = 0; } + // 关键技术点 mView.setPadding(translateX + mPaddingLeft, translateY + mPaddingTop, translateX + mPaddingRight, translateY + mPaddingBottom); } }, 2000); - } else { - Log.i("---- 没有左上右下"); } } - /************************ - * text color - ************************/ + /** * 默认字体颜色 * @@ -1087,18 +1099,20 @@ public class RTextViewHelper extends RBaseHelper implements ITextViewFeatu /** * 初始化按下状态文本颜色 - * 备注:当存在 Checked 状态并且没有设置 Pressed 时,Pressed = Checked 更符合常规UI - * 备注:使用子类 RCheckHelper 中的属性,提供方法入口 + * + * @param isCheckedTextColor 是否是选中的text颜色 + * @param textColorChecked 颜色 */ - protected void initPressedTextColor(boolean hasCheckedTextColor, Color textColorChecked) { + protected void initPressedTextColor(boolean isCheckedTextColor, Color textColorChecked) { if (!isHasPressedTextColor) { - mTextColorPressed = hasCheckedTextColor ? textColorChecked : mTextColorNormal; + mTextColorPressed = isCheckedTextColor ? textColorChecked : mTextColorNormal; } } /** * 设置是否启用 - * 备注:用于库内确定逻辑的调用,不建议开发者直接调用 + * + * @param isEnabled true 是,反之 */ @Override @SuppressWarnings("unchecked") @@ -1142,7 +1156,8 @@ public class RTextViewHelper extends RBaseHelper implements ITextViewFeatu /** * 设置是否选中 - * 备注:用于库内确定逻辑的调用,不建议开发者直接调用 + * + * @param isSelected true 是,反之 */ @Override @SuppressWarnings("unchecked") @@ -1191,7 +1206,10 @@ public class RTextViewHelper extends RBaseHelper implements ITextViewFeatu /** * 触摸事件逻辑 - * 备注:用于库内确定逻辑的调用,不建议开发者直接调用 + * + * @param component 视图 + * @param event + * @return true */ @Override public boolean onTouchEvent(Component component, TouchEvent event) { @@ -1215,7 +1233,7 @@ public class RTextViewHelper extends RBaseHelper implements ITextViewFeatu if (mIconNormal != null) { mIcon = mView.isSelected() ? mIconSelected : mIconNormal; } - if (is_ripple && mView.isEnabled()) { + if (isRipple && mView.isEnabled()) { isTouch = true; paint.setAlpha(0f); } @@ -1250,14 +1268,13 @@ public class RTextViewHelper extends RBaseHelper implements ITextViewFeatu if (mIconPressed != null) { mIcon = mIconPressed; } - if (is_ripple && mView.isEnabled()) { + if (isRipple && mView.isEnabled()) { pointerPosition = event.getPointerPosition(0); float px = pointerPosition.getX(); float py = pointerPosition.getY(); int[] parentLocationOnScreen = mView.getLocationOnScreen(); int yy = (int) (py - parentLocationOnScreen[1]); sx = component.getWidth()/2; -// sy = yy + 300; sy = component.getHeight()/ 2; animatorValue.start(); isTouch = false; @@ -1272,8 +1289,10 @@ public class RTextViewHelper extends RBaseHelper implements ITextViewFeatu } /** - * View可见性变化时更新textView icon与文本一起居中 - * 备注:用于库内确定逻辑的调用,不建议开发者直接调用 + * 可见性变化时更新text icon与文本一起居中 + * + * @param changedView 视图 + * @param visibility 是否显示 */ @Override @SuppressWarnings("unchecked") @@ -1285,12 +1304,12 @@ public class RTextViewHelper extends RBaseHelper implements ITextViewFeatu @Override public void onDraw(Component component, Canvas canvas) { - if (is_ripple && mView.isEnabled()) { + if (isRipple && mView.isEnabled()) { animatorValue.setValueUpdateListener(new AnimatorValue.ValueUpdateListener() { @Override - public void onUpdate(AnimatorValue animatorValue, float v) { - zj = v; - if (v > 0.85 && isTouch) { + public void onUpdate(AnimatorValue animatorValue, float value) { + zj = value; + if (value > 0.85 && isTouch) { paint.setAlpha(0f); } else { paint.setAlpha(0.2f); @@ -1309,7 +1328,7 @@ public class RTextViewHelper extends RBaseHelper implements ITextViewFeatu @Override public void onRefreshed(Component component) { super.onRefreshed(component); - if (mDrawableWithText) { + if (isDrawableWithText) { mPaddingLeft = mView.getPaddingLeft(); mPaddingRight = mView.getPaddingRight(); mPaddingTop = mView.getPaddingTop(); diff --git a/library/src/main/java/com/ruffian/library/widget/iface/ITextViewFeature.java b/library/src/main/java/com/ruffian/library/widget/iface/ITextViewFeature.java index e7dec3f..8f7a983 100644 --- a/library/src/main/java/com/ruffian/library/widget/iface/ITextViewFeature.java +++ b/library/src/main/java/com/ruffian/library/widget/iface/ITextViewFeature.java @@ -8,9 +8,25 @@ import ohos.agp.components.Component; * @author ZhongDaFeng */ public interface ITextViewFeature { + /** + * 是否选中 + * + * @param isEnabled true是,反之 + */ void setEnabled(boolean isEnabled); + /** + * 是否选中 + * + * @param isSelected true是,反之 + */ void setSelected(boolean isSelected); + /** + * 显示隐藏 + * + * @param changedView 视图 + * @param visibility 显隐 + */ void onVisibilityChanged(Component changedView, int visibility); } diff --git a/library/src/main/java/com/ruffian/library/widget/rounded/RoundDrawable.java b/library/src/main/java/com/ruffian/library/widget/rounded/RoundDrawable.java index 8e45623..6d93829 100644 --- a/library/src/main/java/com/ruffian/library/widget/rounded/RoundDrawable.java +++ b/library/src/main/java/com/ruffian/library/widget/rounded/RoundDrawable.java @@ -36,7 +36,7 @@ public class RoundDrawable extends ShapeElement implements Component.DrawTask { private final RectFloat mBounds = new RectFloat(); private final RectFloat mBoundsFinal = new RectFloat(); - private boolean mRebuildShader = true; + private boolean isRebuildShader = true; private final Matrix mShaderMatrix = new Matrix(); private ScaleMode mScaleType = ScaleMode.ZOOM_CENTER; @@ -51,7 +51,7 @@ public class RoundDrawable extends ShapeElement implements Component.DrawTask { private float mBorderWidth = 0; private Color mBorderColor = Color.BLACK; // 是否圆形 - private boolean mCircle = true; + private boolean isCircle = true; private float mWidthHeight = 2; public RoundDrawable(PixelMap bitmap) { @@ -122,7 +122,7 @@ public class RoundDrawable extends ShapeElement implements Component.DrawTask { /** * 根据ScaleType更新ShaderMatrix - * 此函数涉及更新的属性:mBorderWidth || mScaleType || mCircle + * 此函数涉及更新的属性:mBorderWidth || mScaleType || isCircle */ private void updateShaderMatrix() { float scale; @@ -155,7 +155,7 @@ public class RoundDrawable extends ShapeElement implements Component.DrawTask { dy = (int) ((mBounds.getHeight() - mBitmapHeight * scale) * 0.5f + 0.5f); mRectF = new RectFloat(dx, dy, width + dx, height + dy); - mRectF.fuse(mCircle ? mBorderWidth : half, mCircle ? mBorderWidth : half); // 非圆 1/2兼容圆角 + mRectF.fuse(isCircle ? mBorderWidth : half, isCircle ? mBorderWidth : half); // 非圆 1/2兼容圆角 mShaderMatrix.reset(); mShaderMatrix.setScale(scale, scale); mShaderMatrix.postTranslate(dx, dy); @@ -168,19 +168,19 @@ public class RoundDrawable extends ShapeElement implements Component.DrawTask { float cutOrMarginH = mBounds.getHeight() - mBitmapRect.getHeight(); float cutOrMarginW = mBounds.getWidth() - mBitmapRect.getWidth(); float halfH = cutOrMarginH / 2f, halfW = cutOrMarginW / 2f; - float top = halfH > 0 ? halfH : 0; - float left = halfW > 0 ? halfW : 0; + float top = 0 < halfH ? halfH : 0; + float left = 0 < halfW ? halfW : 0; dx = halfW; dy = halfH; mRectF = new RectFloat(left, top, left + width, top + height); - mRectF.fuse(mCircle ? mBorderWidth : half, mCircle ? mBorderWidth : half); // 非圆 1/2兼容圆角 + mRectF.fuse(isCircle ? mBorderWidth : half, isCircle ? mBorderWidth : half); // 非圆 1/2兼容圆角 mShaderMatrix.reset(); mShaderMatrix.postTranslate((int) (dx + 0.5f) + half, (int) (dy + 0.5f) + half); break; case CLIP_CENTER: mRectF.fuse(mBounds); - mRectF.fuse(mCircle ? mBorderWidth : half, mCircle ? mBorderWidth : half); // 非圆 1/2兼容圆角 + mRectF.fuse(isCircle ? mBorderWidth : half, isCircle ? mBorderWidth : half); // 非圆 1/2兼容圆角 if (mBitmapWidth * mRectF.getHeight() > mRectF.getWidth() * mBitmapHeight) { scale = mRectF.getHeight() / (float) mBitmapHeight; dx = (mRectF.getWidth() - mBitmapWidth * scale) * 0.5f; @@ -196,27 +196,27 @@ public class RoundDrawable extends ShapeElement implements Component.DrawTask { case ZOOM_CENTER: case ZOOM_END: case ZOOM_START: - mBounds.fuse(mCircle ? mBorderWidth : half, mCircle ? mBorderWidth : half); // 非圆 1/2兼容圆角 + mBounds.fuse(isCircle ? mBorderWidth : half, isCircle ? mBorderWidth : half); // 非圆 1/2兼容圆角 mRectF.fuse(mBitmapRect); mShaderMatrix.setRectToRect(mBitmapRect, mBounds, scaleTypeToScaleToFit(mScaleType)); mShaderMatrix.mapRect(mRectF); mShaderMatrix.setRectToRect(mBitmapRect, mRectF, Matrix.ScaleToFit.FILL); break; case STRETCH: - mBounds.fuse(mCircle ? mBorderWidth : half, mCircle ? mBorderWidth : half); // 非圆 1/2兼容圆角 + mBounds.fuse(isCircle ? mBorderWidth : half, isCircle ? mBorderWidth : half); // 非圆 1/2兼容圆角 mRectF.fuse(mBounds); mShaderMatrix.reset(); mShaderMatrix.setRectToRect(mBitmapRect, mRectF, Matrix.ScaleToFit.FILL); break; } - if (mCircle) { + if (isCircle) { mBorderRect.fuse(mRectF.left - half, mRectF.top - half, mRectF.right + half, mRectF.bottom + half); // 还原 } else { mBorderRect.fuse(mBoundsFinal); mBorderRect.fuse(half, half); } mDrawableRect.fuse(mRectF); - mRebuildShader = true; + isRebuildShader = true; } /** @@ -234,17 +234,17 @@ public class RoundDrawable extends ShapeElement implements Component.DrawTask { /** * 统一设置参数 * - * @param scaleType - * @param borderWidth - * @param borderColor - * @param circle - * @param corner - * @param topLeft - * @param topRight - * @param bottomLeft - * @param bottomRight + * @param scaleType img类型 + * @param borderWidth 宽度 + * @param borderColor 颜色 + * @param isCircle 是否 + * @param corner 圆角 + * @param topLeft 左上 + * @param topRight 右上 + * @param bottomLeft 下左 + * @param bottomRight 下右 */ - public void setParams(Image.ScaleMode scaleType, float borderWidth, Color borderColor, boolean circle, float corner, float topLeft, float topRight, float bottomLeft, float bottomRight) { + public void setParams(Image.ScaleMode scaleType, float borderWidth, Color borderColor, boolean isCircle, float corner, float topLeft, float topRight, float bottomLeft, float bottomRight) { // scaleType if (scaleType == null) { scaleType = ScaleMode.ZOOM_CENTER; @@ -259,8 +259,8 @@ public class RoundDrawable extends ShapeElement implements Component.DrawTask { mBorderColor = borderColor; updateBorder(); - // circle - mCircle = circle; + // isCircle + this.isCircle = isCircle; // corner mCorner = corner; @@ -291,8 +291,14 @@ public class RoundDrawable extends ShapeElement implements Component.DrawTask { return this; } - public RoundDrawable setCircle(boolean circle) { - mCircle = circle; + /** + * 设置圆 + * + * @param isCircle true是,反之 + * @return 本类 + */ + public RoundDrawable setCircle(boolean isCircle) { + this.isCircle = isCircle; updateShaderMatrix(); // 更新变化矩阵 return this; } @@ -421,16 +427,16 @@ public class RoundDrawable extends ShapeElement implements Component.DrawTask { @Override public void onDraw(Component component, Canvas canvas) { - if (mRebuildShader) { + if (isRebuildShader) { PixelMapHolder holder = new PixelMapHolder(mBitmap); PixelMapShader bitmapShader = new PixelMapShader(holder, Shader.TileMode.CLAMP_TILEMODE, Shader.TileMode.CLAMP_TILEMODE); bitmapShader.setShaderMatrix(mShaderMatrix); // Shader.TileMode.CLAMP mBitmapPaint.setShader(bitmapShader, Paint.ShaderType.PIXELMAP_SHADER); - mRebuildShader = false; + isRebuildShader = false; } - if (mCircle) { + if (isCircle) { // mDrawableRect 宽高是实际显示图片的宽高,类似于 marginTop 了 mDrawableRect.left float cx = mDrawableRect.getWidth() / mWidthHeight + mDrawableRect.left; float cy = mDrawableRect.getHeight() / mWidthHeight + mDrawableRect.top; diff --git a/library/src/main/java/com/ruffian/library/widget/utils/AttrUtils.java b/library/src/main/java/com/ruffian/library/widget/utils/AttrUtils.java index c120932..39d5f1b 100644 --- a/library/src/main/java/com/ruffian/library/widget/utils/AttrUtils.java +++ b/library/src/main/java/com/ruffian/library/widget/utils/AttrUtils.java @@ -1,11 +1,10 @@ - /* * Copyright (C) 2021 The Chinese Software International Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, diff --git a/library/src/main/java/com/ruffian/library/widget/utils/LogUtil.java b/library/src/main/java/com/ruffian/library/widget/utils/LogUtil.java index afa6e8a..736e4c1 100644 --- a/library/src/main/java/com/ruffian/library/widget/utils/LogUtil.java +++ b/library/src/main/java/com/ruffian/library/widget/utils/LogUtil.java @@ -13,6 +13,7 @@ * limitations under the License. * */ + package com.ruffian.library.widget.utils; import ohos.hiviewdfx.HiLog; diff --git a/library/src/main/java/com/ruffian/library/widget/utils/ShadowDrawable.java b/library/src/main/java/com/ruffian/library/widget/utils/ShadowDrawable.java index e966fbe..c86eb79 100644 --- a/library/src/main/java/com/ruffian/library/widget/utils/ShadowDrawable.java +++ b/library/src/main/java/com/ruffian/library/widget/utils/ShadowDrawable.java @@ -13,6 +13,7 @@ * limitations under the License. * */ + package com.ruffian.library.widget.utils; import ohos.agp.components.Component; @@ -105,6 +106,11 @@ public class ShadowDrawable extends ShapeElement { mShadowPaint.setAlpha(alpha); } + /** + * 颜色 + * + * @param colorFilter 颜色 + */ public void setColorFilter(ColorFilter colorFilter) { mShadowPaint.setColorFilter(colorFilter); } @@ -113,6 +119,12 @@ public class ShadowDrawable extends ShapeElement { return 0; } + /** + * 阴影图 + * + * @param view 视图 + * @param drawable 背景 + */ public static void setShadowDrawable(Component view, Element drawable) { view.setBackground(drawable); } @@ -180,7 +192,7 @@ public class ShadowDrawable extends ShapeElement { /** * 为指定View设置指定形状并带阴影的背景 * - * @param view 目标View + * @param view 目标视图 * @param shape View的形状 取值可为:GradientDrawable.RECTANGLE, GradientDrawable.OVAL, GradientDrawable.RING * @param bgColor View背景色 * @param shapeRadius View的圆角 @@ -212,15 +224,15 @@ public class ShadowDrawable extends ShapeElement { } /** - * 为指定View设置带阴影的渐变背景 + * 为指定视图设置带阴影的渐变背景 * - * @param view - * @param bgColor - * @param shapeRadius - * @param shadowColor - * @param shadowRadius - * @param offsetX - * @param offsetY + * @param view 视图 + * @param bgColor 颜色 + * @param shapeRadius 圆角 + * @param shadowColor 颜色 + * @param shadowRadius 阴影圆角 + * @param offsetX x + * @param offsetY y */ public static void setShadowDrawable(Component view, int[] bgColor, int shapeRadius, int shadowColor, int shadowRadius, int offsetX, int offsetY) { ShadowDrawable drawable = new Builder() @@ -261,46 +273,99 @@ public class ShadowDrawable extends ShapeElement { mBgColor[0] = Color.TRANSPARENT.getValue(); } + /** + * 形状 + * + * @param shape int类型 + * @return 本类 + */ public Builder setShape(int shape) { this.mShape = shape; return this; } + /** + * 阴影圆角 + * + * @param shapeRadius 圆角 + * @return 本类 + */ public Builder setShapeRadius(int shapeRadius) { this.mShapeRadius = shapeRadius; return this; } + /** + * 阴影颜色 + * + * @param shadowColor 颜色 + * @return 本类 + */ public Builder setShadowColor(int shadowColor) { this.mShadowColor = shadowColor; return this; } + /** + * 阴影圆角 + * + * @param shadowRadius 圆角 + * @return 本类this + */ public Builder setShadowRadius(int shadowRadius) { this.mShadowRadius = shadowRadius; return this; } + /** + * x距离 + * + * @param offsetX x + * @return 本类 + */ public Builder setOffsetX(int offsetX) { this.mOffsetX = offsetX; return this; } + /** + * 距离 + * + * @param offsetY y + * @return 本类 + */ public Builder setOffsetY(int offsetY) { this.mOffsetY = offsetY; return this; } + /** + * 颜色 + * + * @param bgColor 背景颜色 + * @return 本类 + */ public Builder setBgColor(int bgColor) { this.mBgColor[0] = bgColor; return this; } + /** + * 颜色 + * + * @param bgColor 背景颜色数组 + * @return 本类 + */ public Builder setBgColor(int[] bgColor) { this.mBgColor = bgColor; return this; } + /** + * 阴影 + * + * @return 阴影 + */ public ShadowDrawable builder() { return new ShadowDrawable(mShape, mBgColor, mShapeRadius, mShadowColor, mShadowRadius, mOffsetX, mOffsetY); } diff --git a/library/src/main/java/com/ruffian/library/widget/utils/TextViewUtils.java b/library/src/main/java/com/ruffian/library/widget/utils/TextViewUtils.java index 4d0ce02..44c7558 100644 --- a/library/src/main/java/com/ruffian/library/widget/utils/TextViewUtils.java +++ b/library/src/main/java/com/ruffian/library/widget/utils/TextViewUtils.java @@ -18,6 +18,11 @@ public class TextViewUtils { private TextViewUtils() { } + /** + * 获取工具类 + * + * @return 单列 + */ public static TextViewUtils get() { if (instance == null) { synchronized (TextViewUtils.class) { @@ -33,7 +38,7 @@ public class TextViewUtils { * 获取Text实际宽度 * 备注:单行最大宽度 * - * @param view view + * @param view text * @param drawableWidth drawable宽度 * @param paddingLeft 内边距-左 * @param paddingRight 内边距-右 @@ -79,7 +84,7 @@ public class TextViewUtils { * 获取Text实际高度 * 备注:多行最大高度 * - * @param view view + * @param view text * @param drawableHeight drawable高度 * @param paddingTop 内边距-左 * @param paddingBottom 内边距-右 @@ -96,12 +101,14 @@ public class TextViewUtils { */ Paint paint = new Paint(); - - float singleLineHeight = Math.abs((paint.getFontMetrics().bottom - paint.getFontMetrics().top)); //单行高度 + // 单行高度 + float singleLineHeight = Math.abs( + (paint.getFontMetrics().bottom - paint.getFontMetrics().top)); float textHeight = singleLineHeight * view.getMaxTextLines(); // 最大高度临界值,不允许超过最大高度临界值 - int maxHeight = view.getHeight() - drawableHeight - paddingTop - paddingBottom - drawablePaddingVertical;//最大允许的宽度 + int maxHeight = view.getHeight() - drawableHeight - paddingTop + - paddingBottom - drawablePaddingVertical; // 最大允许的宽度 if (textHeight > maxHeight) { textHeight = maxHeight; } diff --git a/library/src/main/java/com/ruffian/library/widget/utils/TypedAttrUtils.java b/library/src/main/java/com/ruffian/library/widget/utils/TypedAttrUtils.java index 73ce7f4..7bfb983 100644 --- a/library/src/main/java/com/ruffian/library/widget/utils/TypedAttrUtils.java +++ b/library/src/main/java/com/ruffian/library/widget/utils/TypedAttrUtils.java @@ -48,6 +48,14 @@ public class TypedAttrUtils { } } + /** + * 获取颜色 + * + * @param attrs 值 + * @param attrName 名字 + * @param defValue 颜色 + * @return 颜色 + */ public static Color getColor(AttrSet attrs, String attrName, Color defValue) { Attr attr = attrNoSuchElement(attrs, attrName); if (attr == null) { -- Gitee