diff --git a/README.md b/README.md index 957aa800b46c18d3867455cef043c6236b01c8ab..a0c589922a45852d23fbccc5665577b650998455 100644 --- a/README.md +++ b/README.md @@ -1,11 +1,11 @@ ## 项目介绍 -本项目是基于开源项目 AndroidScreenAdaptation 进行鸿蒙化的移植和开发的, +本项目是基于开源项目 AndroidScreenAdaptation 进行openharmony化的移植和开发的, 可以通过项目标签以及github地址(https://github.com/linger1216/labelview)追踪到原安卓项目版本 项目名称:labeView -所属系列:鸿蒙的第三方组件适配移植 +所属系列:openharmony的第三方组件适配移植 功能:在按钮 文字 图片上添加角标。 @@ -21,16 +21,18 @@ 原项目Doc地址:https://github.com/linger1216/labelview +基线版本:labelview-master组件Releases1.1.2 + ## 效果图展示 -![]() +![](https://gitee.com/wu361/labelview/blob/master/img/labelview.gif) #### 项目介绍 - 编程语言:Java #### 安装教程 -1、下载Labelview的har包Labelview.har(位于:https://gitee.com/chinasoft_ohos/skeleton_ohos/releases)。 +1、下载Labelview的har包Labelview.har(位于: https://gitee.com/chinasoft_ohos/skeleton/releases )。 2、启动 DevEco Studio,将下载的har包,导入工程目录“entry->libs”下。 @@ -40,8 +42,8 @@ dependencies { implementation fileTree(dir: 'libs', include: ['.jar', '.har']) #### 使用说明 - ## 将xml代码放入布局中,如下所示: - ## LabelButtonView +#### 将xml代码放入布局中,如下所示: +#### LabelButtonView ```java ``` - ## LabelImageView +#### LabelImageView ```java ``` - ## LabelTextView +#### LabelTextView ```java categoryDataList; private AbilitySlice slice; + /** + * CategoryAdapter + * + * @param categoryAdapterList + * @param slice + */ public CategoryAdapter(List categoryAdapterList, AbilitySlice slice) { this.categoryDataList = categoryAdapterList; this.slice = slice; @@ -169,7 +188,7 @@ public class ListViewActivity extends AbilitySlice { if (categoryDataList != null && i >= 0 && i < categoryDataList.size()) { return categoryDataList.get(i); } - return null; + return Collections.emptyList(); } @Override @@ -197,7 +216,7 @@ public class ListViewActivity extends AbilitySlice { Text line = (Text) cpt.findComponentById(ResourceTable.Id_line); line.setWidth(ComponentContainer.LayoutConfig.MATCH_PARENT); - line.setHeight(4); + line.setHeight(INT_4); return cpt; } diff --git a/entry/src/main/java/com/lid/labelview/slice/MainAbilitySlice.java b/entry/src/main/java/com/lid/labelview/slice/MainAbilitySlice.java index 5592dee002f82c15be789e4928a23508933d9a2e..c793745daa38373484d3a3e044ddfbe8d502db7b 100644 --- a/entry/src/main/java/com/lid/labelview/slice/MainAbilitySlice.java +++ b/entry/src/main/java/com/lid/labelview/slice/MainAbilitySlice.java @@ -4,15 +4,22 @@ import com.lid.labelview.ResourceTable; import com.lid.lib.LabelButtonView; import com.lid.lib.LabelImageView; import com.lid.lib.LabelTextView; + import ohos.aafwk.ability.AbilitySlice; import ohos.aafwk.content.Intent; import ohos.agp.components.Component; +/** + * MainAbilitySlice; + * + * @since 2021-04-13 + */ public class MainAbilitySlice extends AbilitySlice { private static final int DISTANCE = 40; private static final int ORIENTATION = 3; /** + * onStart * * @param intent */ diff --git a/entry/src/main/java/com/lid/labelview/slice/RecyclerViewActivity.java b/entry/src/main/java/com/lid/labelview/slice/RecyclerViewActivity.java index 775eb1734bdb3cef0d83d253f6e287ecd3037989..cd9a6174a5349d3111859f89a0df7892afa5945e 100644 --- a/entry/src/main/java/com/lid/labelview/slice/RecyclerViewActivity.java +++ b/entry/src/main/java/com/lid/labelview/slice/RecyclerViewActivity.java @@ -2,16 +2,28 @@ package com.lid.labelview.slice; import com.lid.labelview.ResourceTable; import com.lid.lib.LabelImageView; + import ohos.aafwk.ability.AbilitySlice; import ohos.aafwk.content.Intent; -import ohos.agp.components.*; +import ohos.agp.components.BaseItemProvider; +import ohos.agp.components.Component; +import ohos.agp.components.ComponentContainer; +import ohos.agp.components.LayoutScatter; +import ohos.agp.components.ListContainer; +import ohos.agp.components.Text; import ohos.global.resource.solidxml.TypedAttribute; import ohos.hiviewdfx.HiLog; import ohos.hiviewdfx.HiLogLabel; import java.util.ArrayList; +import java.util.Collections; import java.util.List; +/** + * RecyclerViewActivity; + * + * @since 2021-04-13 + */ public class RecyclerViewActivity extends AbilitySlice { private static final HiLogLabel LABEL = new HiLogLabel(HiLog.LOG_APP, 0x00201, "RecyclerViewActivity"); @@ -132,16 +144,32 @@ public class RecyclerViewActivity extends AbilitySlice { categoryAdapter.notifyDataChanged(); } + /** + * CategoryData + * + * @since 2021-04-13 + */ public class CategoryData { private String image; private String text; private String label; } + /** + * CategoryAdapter + * + * @since 2021-04-13 + */ public class CategoryAdapter extends BaseItemProvider { private List categoryDataList; private AbilitySlice slice; + /** + * CategoryAdapter + * + * @param categoryAdapterList + * @param slice + */ public CategoryAdapter(List categoryAdapterList, AbilitySlice slice) { this.categoryDataList = categoryAdapterList; this.slice = slice; @@ -157,7 +185,7 @@ public class RecyclerViewActivity extends AbilitySlice { if (categoryDataList != null && i >= 0 && i < categoryDataList.size()) { return categoryDataList.get(i); } - return null; + return Collections.emptyList(); } @Override diff --git a/img/labelview.gif b/img/labelview.gif new file mode 100644 index 0000000000000000000000000000000000000000..fc6eaccdd22b4d896cfa3fd75fece457a5e4c7db Binary files /dev/null and b/img/labelview.gif differ diff --git a/lib/src/main/java/com/lid/lib/LabelButtonView.java b/lib/src/main/java/com/lid/lib/LabelButtonView.java index befc5a03d96b3ca9717200fff0512569f9b045fb..25ec0da8cba9a63d06ea0058a64b439c898d1189 100644 --- a/lib/src/main/java/com/lid/lib/LabelButtonView.java +++ b/lib/src/main/java/com/lid/lib/LabelButtonView.java @@ -1,24 +1,49 @@ package com.lid.lib; -import ohos.agp.components.*; +import ohos.agp.components.AttrSet; +import ohos.agp.components.Button; +import ohos.agp.components.Component; import ohos.agp.render.Canvas; import ohos.agp.utils.Color; import ohos.app.Context; import ohos.hiviewdfx.HiLog; import ohos.hiviewdfx.HiLogLabel; +/** + * LabelButtonView; + * + * @since 2021-04-13 + */ public class LabelButtonView extends Button implements Component.DrawTask { private static final HiLogLabel LABEL = new HiLogLabel(HiLog.LOG_APP, 0x00201, "LabelButtonView"); LabelViewHelper utils; + /** + * LabelButtonView + * + * @param context + */ public LabelButtonView(Context context) { this(context, null); } + /** + * LabelButtonView + * + * @param context + * @param attrs + */ public LabelButtonView(Context context, AttrSet attrs) { this(context, attrs, 0); } + /** + * LabelButtonView + * + * @param context + * @param attrs + * @param defStyleAttr + */ public LabelButtonView(Context context, AttrSet attrs, int defStyleAttr) { super(context, attrs, String.valueOf(defStyleAttr)); utils = new LabelViewHelper(context, attrs, defStyleAttr); @@ -31,6 +56,11 @@ public class LabelButtonView extends Button implements Component.DrawTask { getHeight() - getPaddingTop() - getPaddingBottom()); } + /** + * setLabelHeight + * + * @param height + */ public void setLabelHeight(int height) { utils.setLabelHeight(this, height); } @@ -39,6 +69,11 @@ public class LabelButtonView extends Button implements Component.DrawTask { return utils.getLabelHeight(); } + /** + * setLabelDistance + * + * @param distance + */ public void setLabelDistance(int distance) { utils.setLabelDistance(this, distance); } @@ -51,6 +86,11 @@ public class LabelButtonView extends Button implements Component.DrawTask { return utils.isLabelVisual(); } + /** + * setLabelVisual + * + * @param isEnable + */ public void setLabelVisual(boolean isEnable) { utils.setLabelVisual(this, isEnable); } @@ -59,6 +99,11 @@ public class LabelButtonView extends Button implements Component.DrawTask { return utils.getLabelOrientation(); } + /** + * setLabelOrientation + * + * @param orientation + */ public void setLabelOrientation(int orientation) { utils.setLabelOrientation(this, orientation); } @@ -67,6 +112,11 @@ public class LabelButtonView extends Button implements Component.DrawTask { return utils.getLabelTextColor(); } + /** + * setLabelTextColor + * + * @param textColor + */ public void setLabelTextColor(Color textColor) { utils.setLabelTextColor(this, textColor); } @@ -75,6 +125,11 @@ public class LabelButtonView extends Button implements Component.DrawTask { return utils.getLabelBackgroundColor(); } + /** + * setLabelBackgroundColor + * + * @param backgroundColor + */ public void setLabelBackgroundColor(Color backgroundColor) { utils.setLabelBackgroundColor(this, backgroundColor); } @@ -83,6 +138,11 @@ public class LabelButtonView extends Button implements Component.DrawTask { return utils.getLabelText(); } + /** + * setLabelText + * + * @param text + */ public void setLabelText(String text) { utils.setLabelText(this, text); } @@ -91,6 +151,11 @@ public class LabelButtonView extends Button implements Component.DrawTask { return utils.getLabelTextSize(); } + /** + * setLabelTextSize + * + * @param textSize + */ public void setLabelTextSize(int textSize) { utils.setLabelTextSize(this, textSize); } @@ -99,6 +164,11 @@ public class LabelButtonView extends Button implements Component.DrawTask { return utils.getLabelTextStyle(); } + /** + * setLabelTextStyle + * + * @param textStyle + */ public void setLabelTextStyle(int textStyle) { utils.setLabelTextStyle(this, textStyle); } diff --git a/lib/src/main/java/com/lid/lib/LabelImageView.java b/lib/src/main/java/com/lid/lib/LabelImageView.java index fe041ae99bb95cf1577daeff792c036911b1619c..7363a36c5645036ba835e0ebe76eb1805217591d 100644 --- a/lib/src/main/java/com/lid/lib/LabelImageView.java +++ b/lib/src/main/java/com/lid/lib/LabelImageView.java @@ -9,20 +9,43 @@ import ohos.app.Context; import ohos.hiviewdfx.HiLog; import ohos.hiviewdfx.HiLogLabel; +/** + * LabelButtonView; + * + * @since 2021-04-13 + */ public class LabelImageView extends Image implements Component.DrawTask { private static final HiLogLabel LABEL = new HiLogLabel(HiLog.LOG_APP, 0x00201, "LabelImageView"); LabelViewHelper utils; + /** + * LabelImageView + * + * @param context + */ public LabelImageView(Context context) { this(context, null); HiLog.info(LABEL, "LabelImageView -----1111"); } + /** + * LabelImageView + * + * @param context + * @param attrs + */ public LabelImageView(Context context, AttrSet attrs) { this(context, attrs, 0); HiLog.info(LABEL, "LabelImageView"); } + /** + * LabelButtonView + * + * @param context + * @param attrs + * @param defStyleAttr + */ public LabelImageView(Context context, AttrSet attrs, int defStyleAttr) { super(context, attrs, String.valueOf(defStyleAttr)); utils = new LabelViewHelper(context, attrs, defStyleAttr); @@ -35,6 +58,11 @@ public class LabelImageView extends Image implements Component.DrawTask { getHeight() - getPaddingTop() - getPaddingBottom()); } + /** + * setLabelHeight + * + * @param height + */ public void setLabelHeight(int height) { utils.setLabelHeight(this, height); } @@ -43,6 +71,11 @@ public class LabelImageView extends Image implements Component.DrawTask { return utils.getLabelHeight(); } + /** + * setLabelDistance + * + * @param distance + */ public void setLabelDistance(int distance) { utils.setLabelDistance(this, distance); } @@ -55,6 +88,11 @@ public class LabelImageView extends Image implements Component.DrawTask { return utils.isLabelVisual(); } + /** + * setLabelVisual + * + * @param isEnable + */ public void setLabelVisual(boolean isEnable) { utils.setLabelVisual(this, isEnable); } @@ -63,6 +101,11 @@ public class LabelImageView extends Image implements Component.DrawTask { return utils.getLabelOrientation(); } + /** + * setLabelOrientation + * + * @param orientation + */ public void setLabelOrientation(int orientation) { utils.setLabelOrientation(this, orientation); } @@ -71,6 +114,11 @@ public class LabelImageView extends Image implements Component.DrawTask { return utils.getLabelTextColor(); } + /** + * setLabelTextColor + * + * @param textColor + */ public void setLabelTextColor(Color textColor) { utils.setLabelTextColor(this, textColor); } @@ -79,18 +127,24 @@ public class LabelImageView extends Image implements Component.DrawTask { return utils.getLabelBackgroundColor(); } + /** + * setLabelBackgroundColor + * + * @param backgroundColor + */ public void setLabelBackgroundColor(Color backgroundColor) { utils.setLabelBackgroundColor(this, backgroundColor); } - public void setLabelBackgroundAlpha(int alpha) { - utils.setLabelBackgroundAlpha(this, alpha); - } - public String getLabelText() { return utils.getLabelText(); } + /** + * setLabelText + * + * @param text + */ public void setLabelText(String text) { utils.setLabelText(this, text); } @@ -99,6 +153,11 @@ public class LabelImageView extends Image implements Component.DrawTask { return utils.getLabelTextSize(); } + /** + * setLabelTextSize + * + * @param textSize + */ public void setLabelTextSize(int textSize) { utils.setLabelTextSize(this, textSize); } @@ -107,6 +166,11 @@ public class LabelImageView extends Image implements Component.DrawTask { return utils.getLabelTextStyle(); } + /** + * setLabelTextStyle + * + * @param textStyle + */ public void setLabelTextStyle(int textStyle) { utils.setLabelTextStyle(this, textStyle); } diff --git a/lib/src/main/java/com/lid/lib/LabelTextView.java b/lib/src/main/java/com/lid/lib/LabelTextView.java index 35d81cb86f5d2746fe4ebf7dc0329ac422b0bef2..d9d505a63e8e84209d0f443fd6c57e0f0ee396b0 100644 --- a/lib/src/main/java/com/lid/lib/LabelTextView.java +++ b/lib/src/main/java/com/lid/lib/LabelTextView.java @@ -7,17 +7,40 @@ import ohos.agp.render.Canvas; import ohos.agp.utils.Color; import ohos.app.Context; +/** + * LabelButtonView; + * + * @since 2021-04-13 + */ public class LabelTextView extends Text implements Component.DrawTask { LabelViewHelper utils; + /** + * LabelTextView + * + * @param context + */ public LabelTextView(Context context) { this(context, null); } + /** + * LabelTextView + * + * @param context + * @param attrs + */ public LabelTextView(Context context, AttrSet attrs) { this(context, attrs, 0); } + /** + * LabelTextView + * + * @param context + * @param attrs + * @param defStyleAttr + */ public LabelTextView(Context context, AttrSet attrs, int defStyleAttr) { super(context, attrs, String.valueOf(defStyleAttr)); utils = new LabelViewHelper(context, attrs, defStyleAttr); @@ -30,6 +53,11 @@ public class LabelTextView extends Text implements Component.DrawTask { getHeight() - getPaddingTop() - getPaddingBottom()); } + /** + * setLabelHeight + * + * @param height + */ public void setLabelHeight(int height) { utils.setLabelHeight(this, height); } @@ -38,6 +66,11 @@ public class LabelTextView extends Text implements Component.DrawTask { return utils.getLabelHeight(); } + /** + * setLabelDistance + * + * @param distance + */ public void setLabelDistance(int distance) { utils.setLabelDistance(this, distance); } @@ -46,11 +79,16 @@ public class LabelTextView extends Text implements Component.DrawTask { return utils.getLabelDistance(); } - public boolean isLabelEnable() { + public boolean isLabelVisual() { return utils.isLabelVisual(); } - public void setLabelEnable(boolean isEnable) { + /** + * setLabelVisual + * + * @param isEnable + */ + public void setLabelVisual(boolean isEnable) { utils.setLabelVisual(this, isEnable); } @@ -58,6 +96,11 @@ public class LabelTextView extends Text implements Component.DrawTask { return utils.getLabelOrientation(); } + /** + * setLabelOrientation + * + * @param orientation + */ public void setLabelOrientation(int orientation) { utils.setLabelOrientation(this, orientation); } @@ -66,6 +109,11 @@ public class LabelTextView extends Text implements Component.DrawTask { return utils.getLabelTextColor(); } + /** + * setLabelTextColor + * + * @param textColor + */ public void setLabelTextColor(Color textColor) { utils.setLabelTextColor(this, textColor); } @@ -74,6 +122,11 @@ public class LabelTextView extends Text implements Component.DrawTask { return utils.getLabelBackgroundColor(); } + /** + * setLabelBackgroundColor + * + * @param backgroundColor + */ public void setLabelBackgroundColor(Color backgroundColor) { utils.setLabelBackgroundColor(this, backgroundColor); } @@ -82,6 +135,11 @@ public class LabelTextView extends Text implements Component.DrawTask { return utils.getLabelText(); } + /** + * setLabelText + * + * @param text + */ public void setLabelText(String text) { utils.setLabelText(this, text); } @@ -90,6 +148,11 @@ public class LabelTextView extends Text implements Component.DrawTask { return utils.getLabelTextSize(); } + /** + * setLabelTextSize + * + * @param textSize + */ public void setLabelTextSize(int textSize) { utils.setLabelTextSize(this, textSize); } @@ -98,6 +161,11 @@ public class LabelTextView extends Text implements Component.DrawTask { return utils.getLabelTextStyle(); } + /** + * setLabelTextStyle + * + * @param textStyle + */ public void setLabelTextStyle(int textStyle) { utils.setLabelTextStyle(this, textStyle); } diff --git a/lib/src/main/java/com/lid/lib/LabelView.java b/lib/src/main/java/com/lid/lib/LabelView.java index 5312e75393b266b1a328802a046f38f82a4b98fa..77beb11c25e3451f91c4c407586e6bb8fcc6abc4 100644 --- a/lib/src/main/java/com/lid/lib/LabelView.java +++ b/lib/src/main/java/com/lid/lib/LabelView.java @@ -1,7 +1,15 @@ package com.lid.lib; +import static ohos.agp.components.AttrHelper.getDensity; + import ohos.agp.colors.RgbColor; -import ohos.agp.components.*; +import ohos.agp.components.AttrHelper; +import ohos.agp.components.AttrSet; +import ohos.agp.components.Component; +import ohos.agp.components.ComponentContainer; +import ohos.agp.components.DependentLayout; +import ohos.agp.components.StackLayout; +import ohos.agp.components.Text; import ohos.agp.components.element.ShapeElement; import ohos.agp.text.Font; import ohos.agp.utils.Color; @@ -9,8 +17,11 @@ import ohos.app.Context; import java.util.concurrent.atomic.AtomicInteger; -import static ohos.agp.components.AttrHelper.getDensity; - +/** + * LabelView; + * + * @since 2021-04-13 + */ public class LabelView extends Text { private static final int INT = 2; private static final float DENSITY = 0.5f; @@ -21,6 +32,7 @@ public class LabelView extends Text { private static final int BLUE = 255; private static final int LABELVIEWCONTAINERID = -1; private static final double FLOAT = 1.414; + private static AtomicInteger sNextGeneratedId = new AtomicInteger(1); private float offsetx; private float offsety; private float anchorx; @@ -28,18 +40,45 @@ public class LabelView extends Text { private float angel; private int labelViewContainerId; + /** + * Gravity; + * + * @since 2021-04-13 + */ public enum Gravity { + /** + * LEFT_TOP, RIGHT_TOP + * + */ LEFT_TOP, RIGHT_TOP } + /** + * LabelView + * + * @param context + */ public LabelView(Context context) { this(context, null); } + /** + * LabelView + * + * @param context + * @param attrs + */ public LabelView(Context context, AttrSet attrs) { this(context, attrs, 0); } + /** + * LabelView + * + * @param context + * @param attrs + * @param defStyle + */ public LabelView(Context context, AttrSet attrs, int defStyle) { super(context, attrs, String.valueOf(defStyle)); init(); @@ -64,16 +103,31 @@ public class LabelView extends Text { setBackground(shapeElement); } + /** + * setTargetView + * + * @param target + * @param distance + * @param gravity + */ public void setTargetView(Component target, int distance, Gravity gravity) { if (!replaceLayout(target)) { return; } - final int d = dip2Px(getContext(), distance); - final Gravity g = gravity; - final Component v = target; + final int dd = dip2Px(getContext(), distance); + final Gravity gg = gravity; + final Component vv = target; } + /** + * setTargetViewInBaseAdapter + * + * @param target + * @param targetWidth + * @param distance + * @param gravity + */ public void setTargetViewInBaseAdapter(Component target, int targetWidth, int distance, Gravity gravity) { if (!replaceLayout(target)) { return; @@ -81,6 +135,10 @@ public class LabelView extends Text { calcOffset(dip2Px(getContext(), targetWidth), distance, gravity, targetWidth, true); } + /** + * remove + * + */ public void remove() { if (getComponentParent() == null || labelViewContainerId == LABELVIEWCONTAINERID) { return; @@ -168,10 +226,10 @@ public class LabelView extends Text { } private void calcOffset(int labelWidth, int distance, Gravity gravity, int targetWidth, boolean isDp) { - int d = dip2Px(getContext(), distance); + int dd = dip2Px(getContext(), distance); int tw = isDp ? dip2Px(getContext(), targetWidth) : targetWidth; - float edge = (float) ((labelWidth - INT * d) / (INT * FLOAT)); + float edge = (float) ((labelWidth - INT * dd) / (INT * FLOAT)); if (gravity == Gravity.LEFT_TOP) { anchorx = -edge; offsetx = anchorx; @@ -182,16 +240,26 @@ public class LabelView extends Text { angel = RIGHT_TOP; } - anchory = (float) (FLOAT * d + edge); + anchory = (float) (FLOAT * dd + edge); offsety = anchory; } + /** + * dip2Px + * + * @param context + * @param fp + * @return dip2Px + */ public static int dip2Px(final Context context, final float fp) { return AttrHelper.fp2px(fp, getDensity(context) + DENSITY); } - private static AtomicInteger sNextGeneratedId = new AtomicInteger(1); - + /** + * generateViewId + * + * @return int + */ public static int generateViewId() { for (; ; ) { final int result = sNextGeneratedId.get(); diff --git a/lib/src/main/java/com/lid/lib/LabelViewHelper.java b/lib/src/main/java/com/lid/lib/LabelViewHelper.java index 5035d790a6f3a57afc20f9558b561e52f5533b2f..e5a6413db0172bc6f025f0f35cfae21b596e0c9b 100644 --- a/lib/src/main/java/com/lid/lib/LabelViewHelper.java +++ b/lib/src/main/java/com/lid/lib/LabelViewHelper.java @@ -1,5 +1,7 @@ package com.lid.lib; +import static ohos.agp.components.AttrHelper.getDensity; + import com.lid.lib.bean.LabelBean; import com.lid.lib.utils.TypedAttrUtils; @@ -19,8 +21,11 @@ import ohos.hiviewdfx.HiLogLabel; import java.util.ArrayList; import java.util.List; -import static ohos.agp.components.AttrHelper.getDensity; - +/** + * LabelViewHelper; + * + * @since 2021-04-13 + */ public class LabelViewHelper { private static final int LEFT_TOP = 1; private static final int RIGHT_TOP = 2; @@ -40,19 +45,21 @@ public class LabelViewHelper { private static final float DENSITY = 0.5f; private static final float ACTUALDISTANCE = 1.4142135f; private static final int ORIENTATION = 4; + private static final int INT_5 = 5; + private static final int INT_2 = 2; private static final HiLogLabel LABEL = new HiLogLabel(HiLog.LOG_APP, 0x00201, "LabelViewHelper"); - private int distance = DEFAULT_DISTANCE; - private int height = DEFAULT_HEIGHT; - private int strokeWidth = DEFAULT_STROKE_WIDTH; - private String text = "HD"; - private Color backgroundColor = Color.BLACK; - private Color textColor = Color.BLACK; - private int strokeColor; - private int textSize = DEFAULT_TEXT_SIZE; - private int textStyle = 1; - private boolean isVisual = true; - private int orientation = 1; + private int mDistance = DEFAULT_DISTANCE; + private int mHeight = DEFAULT_HEIGHT; + private int mStrokeWidth = DEFAULT_STROKE_WIDTH; + private String mText = "HD"; + private Color mBackgroundColor = Color.BLACK; + private Color mTextColor = Color.BLACK; + private int mStrokeColor; + private int mTextSize = DEFAULT_TEXT_SIZE; + private int mTextStyle = 1; + private boolean isMvisual = true; + private int mOrientation = 1; private Paint rectPaint; private Paint rectStrokePaint; @@ -63,12 +70,19 @@ public class LabelViewHelper { private Rect textBound; private Context context; - private int alpha; - - List fontList = new ArrayList(); + private int mAlpha; + private List fontList; + /** + * LabelViewHelper + * + * @param context + * @param attrs + * @param defStyleAttr + */ public LabelViewHelper(Context context, AttrSet attrs, int defStyleAttr) { this.context = context; + fontList = new ArrayList(); fontList.add(Font.DEFAULT_BOLD); fontList.add(Font.DEFAULT); @@ -109,127 +123,134 @@ public class LabelViewHelper { ArrayList labelBeanList = new ArrayList<>(); LabelBean labelBean = new LabelBean(); - for (int i = 0; i <= 5; i++) { - distance = TypedAttrUtils.getDimensionPixelSize(attrs, "label_distance", DEFAULT_DISTANCE); - height = TypedAttrUtils.getDimensionPixelSize(attrs, "label_height", DEFAULT_HEIGHT); - text = TypedAttrUtils.getString(attrs, "label_text", "HD"); - backgroundColor = TypedAttrUtils.getColor(attrs, "label_backgroundColor", Color.BLACK); - textColor = TypedAttrUtils.getColor(attrs, "label_color", Color.WHITE); - textSize = TypedAttrUtils.getDimensionPixelSize(attrs, "label_textSize", DEFAULT_TEXT_SIZE); - textStyle = TypedAttrUtils.getDimensionPixelSize(attrs, "label_textStyle", 1); - orientation = TypedAttrUtils.getDimensionPixelSize(attrs, "label_orientation", 1); - - labelBean.setDistance(distance); - labelBean.setHeight(height); - labelBean.setText(text); - labelBean.setBackgroundColor(backgroundColor); - labelBean.setTextSize(textSize); - labelBean.setTextStyle(textStyle); - labelBean.setOrientation(orientation); - labelBean.setTextcolor(textColor); + for (int i = 0; i <= INT_5; i++) { + mDistance = TypedAttrUtils.getDimensionPixelSize(attrs, "label_distance", DEFAULT_DISTANCE); + mHeight = TypedAttrUtils.getDimensionPixelSize(attrs, "label_height", DEFAULT_HEIGHT); + mText = TypedAttrUtils.getString(attrs, "label_text", "HD"); + mBackgroundColor = TypedAttrUtils.getColor(attrs, "label_backgroundColor", Color.BLACK); + mTextColor = TypedAttrUtils.getColor(attrs, "label_color", Color.WHITE); + mTextSize = TypedAttrUtils.getDimensionPixelSize(attrs, "label_textSize", DEFAULT_TEXT_SIZE); + mTextStyle = TypedAttrUtils.getDimensionPixelSize(attrs, "label_textStyle", 1); + mOrientation = TypedAttrUtils.getDimensionPixelSize(attrs, "label_orientation", 1); + + labelBean.setDistance(mDistance); + labelBean.setHeight(mHeight); + labelBean.setText(mText); + labelBean.setBackgroundColor(mBackgroundColor); + labelBean.setTextSize(mTextSize); + labelBean.setTextStyle(mTextStyle); + labelBean.setOrientation(mOrientation); + labelBean.setTextcolor(mTextColor); labelBeanList.add(labelBean); } - for (int i = 0; i <= 5; i++) { + for (int i = 0; i <= INT_5; i++) { LabelBean labelBean1 = labelBeanList.get(i); - distance = labelBean1.getDistance(); - height = labelBean1.getHeight(); - text = labelBean1.getText(); - backgroundColor = labelBean1.getBackgroundColor(); - textSize = labelBean1.getTextSize(); - textStyle = labelBean1.getTextStyle(); - orientation = labelBean1.getOrientation(); - textColor = labelBean1.getTextcolor(); + mDistance = labelBean1.getDistance(); + mHeight = labelBean1.getHeight(); + mText = labelBean1.getText(); + mBackgroundColor = labelBean1.getBackgroundColor(); + mTextSize = labelBean1.getTextSize(); + mTextStyle = labelBean1.getTextStyle(); + mOrientation = labelBean1.getOrientation(); + mTextColor = labelBean1.getTextcolor(); } } + /** + * onDraw + * + * @param canvas + * @param measuredWidth + * @param measuredHeight + */ public void onDraw(Canvas canvas, int measuredWidth, int measuredHeight) { - if (!isVisual || text == null) { + if (!isMvisual || mText == null) { return; } calcOffset(measuredWidth, measuredHeight); - rectPaint.setColor(backgroundColor); - if (alpha != 0) { - rectPaint.setAlpha(alpha); + rectPaint.setColor(mBackgroundColor); + if (mAlpha != 0) { + rectPaint.setAlpha(mAlpha); } rectStrokePaint.setColor(Color.WHITE); - rectStrokePaint.setStrokeWidth(strokeWidth); + rectStrokePaint.setStrokeWidth(mStrokeWidth); canvas.drawPath(rectPath, rectPaint); canvas.drawPath(rectPath, rectStrokePaint); - textPaint.setTextSize(textSize); - textPaint.setColor(textColor); - textBound = textPaint.getTextBounds(text); + textPaint.setTextSize(mTextSize); + textPaint.setColor(mTextColor); + textBound = textPaint.getTextBounds(mText); - if (textStyle == 0) { + if (mTextStyle == 0) { textPaint.setFakeBoldText(true); textPaint.setFont(fontList.get(0)); - } else if (textStyle == 1) { + } else if (mTextStyle == 1) { textPaint.setFont(fontList.get(1)); - } else if (textStyle == INTIGER) { - textPaint.setFont(fontList.get(2)); + } else if (mTextStyle == INTIGER) { + textPaint.setFont(fontList.get(INT_2)); } - float actualDistance = distance + height / INTIGER; + float actualDistance = mDistance + mHeight / INTIGER; float beginwoffset = ((ACTUALDISTANCE * actualDistance) / INTIGER - textBound.getWidth() / INTIGER); if (beginwoffset < 0) { beginwoffset = 0; } - canvas.drawTextOnPath(textPaint, text, textPath, beginwoffset, textBound.getHeight() / INTIGER); + canvas.drawTextOnPath(textPaint, mText, textPath, beginwoffset, textBound.getHeight() / INTIGER); } private void calcOffset(int measuredWidth, int measuredHeight) { - float startPosX = measuredWidth - distance - height; + float startPosX = measuredWidth - mDistance - mHeight; float endPosX = measuredWidth; - float startPosY = measuredHeight - distance - height; + float startPosY = measuredHeight - mDistance - mHeight; float endPosY = measuredHeight; - float middle = height / INTIGER; + float middle = mHeight / INTIGER; - switch (orientation) { + switch (mOrientation) { case LEFT_TOP: // LEFT_TOP rectPath.reset(); - rectPath.moveTo(0, distance); - rectPath.lineTo(distance, 0); - rectPath.lineTo(distance + height, 0); - rectPath.lineTo(0, distance + height); + rectPath.moveTo(0, mDistance); + rectPath.lineTo(mDistance, 0); + rectPath.lineTo(mDistance + mHeight, 0); + rectPath.lineTo(0, mDistance + mHeight); rectPath.close(); textPath.reset(); - textPath.moveTo(0, distance + middle); - textPath.lineTo(distance + middle, 0); + textPath.moveTo(0, mDistance + middle); + textPath.lineTo(mDistance + middle, 0); textPath.close(); break; case RIGHT_TOP: // RIGHT_TOP rectPath.reset(); rectPath.moveTo(startPosX, 0); - rectPath.lineTo(startPosX + height, 0); - rectPath.lineTo(endPosX, distance); - rectPath.lineTo(endPosX, distance + height); + rectPath.lineTo(startPosX + mHeight, 0); + rectPath.lineTo(endPosX, mDistance); + rectPath.lineTo(endPosX, mDistance + mHeight); rectPath.close(); textPath.reset(); textPath.moveTo(startPosX + middle, 0); - textPath.lineTo(endPosX, distance + middle); + textPath.lineTo(endPosX, mDistance + middle); textPath.close(); break; case LEFT_BOTTOM: // LEFT_BOTTOM rectPath.reset(); rectPath.moveTo(0, startPosY); - rectPath.lineTo(distance + height, endPosY); - rectPath.lineTo(distance, endPosY); - rectPath.lineTo(0, startPosY + height); + rectPath.lineTo(mDistance + mHeight, endPosY); + rectPath.lineTo(mDistance, endPosY); + rectPath.lineTo(0, startPosY + mHeight); rectPath.close(); textPath.reset(); textPath.moveTo(0, startPosY + middle); - textPath.lineTo(distance + middle, endPosY); + textPath.lineTo(mDistance + middle, endPosY); textPath.close(); break; case RIGHT_BOTTOM: // RIGHT_BOTTOM @@ -237,8 +258,8 @@ public class LabelViewHelper { rectPath.reset(); rectPath.moveTo(startPosX, endPosY); rectPath.lineTo(measuredWidth, startPosY); - rectPath.lineTo(measuredWidth, startPosY + height); - rectPath.lineTo(startPosX + height, endPosY); + rectPath.lineTo(measuredWidth, startPosY + mHeight); + rectPath.lineTo(startPosX + mHeight, endPosY); rectPath.close(); textPath.reset(); @@ -251,140 +272,226 @@ public class LabelViewHelper { } } + /** + * dip2Px + * + * @param context + * @param fp + * @return int + */ public static int dip2Px(final Context context, final float fp) { return AttrHelper.fp2px(fp, getDensity(context) + DENSITY); } + /** + * px2Dip + * + * @param context + * @param px + * @return int + */ public static int px2Dip(final Context context, final float px) { return Math.round(px * getDensity(context) + DENSITY); } + /** + * setLabelHeight + * + * @param view + * @param height + */ public void setLabelHeight(Component view, int height) { - if (this.height != dip2Px(context, height)) { - this.height = dip2Px(context, height); + if (this.mHeight != dip2Px(context, height)) { + this.mHeight = dip2Px(context, height); view.invalidate(); } } public int getLabelHeight() { - return height; + return mHeight; } + /** + * setLabelDistance + * + * @param view + * @param distance + */ public void setLabelDistance(Component view, int distance) { - if (this.distance != dip2Px(context, distance)) { - this.distance = dip2Px(context, distance); + if (this.mDistance != dip2Px(context, distance)) { + this.mDistance = dip2Px(context, distance); view.invalidate(); } } public int getLabelStrokeWidth() { - return px2Dip(context, this.strokeWidth); + return px2Dip(context, this.mStrokeWidth); } + /** + * setLabelStrokeWidth + * + * @param view + * @param strokeWidth + */ public void setLabelStrokeWidth(Component view, int strokeWidth) { - if (this.strokeWidth != dip2Px(context, strokeWidth)) { - this.strokeWidth = dip2Px(context, strokeWidth); + if (this.mStrokeWidth != dip2Px(context, strokeWidth)) { + this.mStrokeWidth = dip2Px(context, strokeWidth); view.invalidate(); } } public int getLabelDistance() { - return distance; + return mDistance; } public boolean isLabelVisual() { - return isVisual; + return isMvisual; } + /** + * setLabelVisual + * + * @param view + * @param isVisual + */ public void setLabelVisual(Component view, boolean isVisual) { - if (this.isVisual != isVisual) { - this.isVisual = isVisual; + if (this.isMvisual != isVisual) { + this.isMvisual = isVisual; view.invalidate(); } } public int getLabelOrientation() { - return orientation; + return mOrientation; } + /** + * setLabelOrientation + * + * @param view + * @param orientation + */ public void setLabelOrientation(Component view, int orientation) { - if (this.orientation != orientation && orientation <= ORIENTATION && orientation >= 1) { - this.orientation = orientation; + if (this.mOrientation != orientation && orientation <= ORIENTATION && orientation >= 1) { + this.mOrientation = orientation; view.invalidate(); } } public Color getLabelTextColor() { - return textColor; + return mTextColor; } + /** + * setLabelTextColor + * + * @param view + * @param textColor + */ public void setLabelTextColor(Component view, Color textColor) { - if (this.textColor != textColor) { - this.textColor = textColor; + if (this.mTextColor != textColor) { + this.mTextColor = textColor; view.invalidate(); } } public Color getLabelBackgroundColor() { - return backgroundColor; + return mBackgroundColor; } + /** + * setLabelBackgroundColor + * + * @param view + * @param backgroundColor + */ public void setLabelBackgroundColor(Component view, Color backgroundColor) { - if (this.backgroundColor != backgroundColor) { - this.backgroundColor = backgroundColor; + if (this.mBackgroundColor != backgroundColor) { + this.mBackgroundColor = backgroundColor; view.invalidate(); } } public int getLabelStrokeColor() { - return strokeColor; + return mStrokeColor; } + /** + * setLabelStrokeColor + * + * @param view + * @param strokeColor + */ public void setLabelStrokeColor(Component view, int strokeColor) { - if (this.strokeColor != strokeColor) { - this.strokeColor = strokeColor; + if (this.mStrokeColor != strokeColor) { + this.mStrokeColor = strokeColor; view.invalidate(); } } + /** + * setLabelBackgroundAlpha + * + * @param view + * @param alpha + */ public void setLabelBackgroundAlpha(Component view, int alpha) { - if (this.alpha != alpha) { - this.alpha = alpha; + if (this.mAlpha != alpha) { + this.mAlpha = alpha; view.invalidate(); } } public String getLabelText() { - return text; + return mText; } + /** + * setLabelText + * + * @param view + * @param text + */ public void setLabelText(Component view, String text) { - if (this.text == null || !this.text.equals(text)) { - this.text = text; + if (this.mText == null || !this.mText.equals(text)) { + this.mText = text; view.invalidate(); } } public int getLabelTextSize() { - return textSize; + return mTextSize; } + /** + * setLabelTextSize + * + * @param view + * @param textSize + */ public void setLabelTextSize(Component view, int textSize) { - if (this.textSize != textSize) { - this.textSize = textSize; + if (this.mTextSize != textSize) { + this.mTextSize = textSize; view.invalidate(); } } public int getLabelTextStyle() { - return textStyle; + return mTextStyle; } + /** + * setLabelTextStyle + * + * @param view + * @param textStyle + */ public void setLabelTextStyle(Component view, int textStyle) { - if (this.textStyle == textStyle) { + if (this.mTextStyle == textStyle) { return; } - this.textStyle = textStyle; + this.mTextStyle = textStyle; view.invalidate(); } } diff --git a/lib/src/main/java/com/lid/lib/bean/LabelBean.java b/lib/src/main/java/com/lid/lib/bean/LabelBean.java index ebdd2edc73b6bd46e4d74f482c378bb652d8e4ae..581003ec133ffd3cf2dc8ca74272dd2abf21cef1 100644 --- a/lib/src/main/java/com/lid/lib/bean/LabelBean.java +++ b/lib/src/main/java/com/lid/lib/bean/LabelBean.java @@ -2,6 +2,11 @@ package com.lid.lib.bean; import ohos.agp.utils.Color; +/** + * LabelBean; + * + * @since 2021-04-13 + */ public class LabelBean { private int distance; private int height; @@ -91,8 +96,8 @@ public class LabelBean { return isVisual; } - public void setVisual(boolean isVisual) { - this.isVisual = isVisual; + public void setVisual(boolean ismVisual) { + this.isVisual = ismVisual; } public int getOrientation() { diff --git a/lib/src/main/java/com/lid/lib/utils/DensityUtils.java b/lib/src/main/java/com/lid/lib/utils/DensityUtils.java index 92adc66a3b5cda4ef3183ced5b31bad615fe6208..7d8814e0137da26cd541ee79069e146095c568bd 100644 --- a/lib/src/main/java/com/lid/lib/utils/DensityUtils.java +++ b/lib/src/main/java/com/lid/lib/utils/DensityUtils.java @@ -1,5 +1,7 @@ package com.lid.lib.utils; +import static ohos.agp.components.AttrHelper.getDensity; + import ohos.agp.colors.RgbColor; import ohos.agp.components.AttrHelper; import ohos.agp.text.Font; @@ -11,8 +13,11 @@ import ohos.app.Context; import java.io.File; import java.util.Optional; -import static ohos.agp.components.AttrHelper.getDensity; - +/** + * DensityUtils; + * + * @since 2021-04-13 + */ public class DensityUtils { /** * fp 转 Px diff --git a/lib/src/main/java/com/lid/lib/utils/TypedAttrUtils.java b/lib/src/main/java/com/lid/lib/utils/TypedAttrUtils.java index 72ab11fd753715131ff0a8dc6af5a2d05359bfc1..fdcedf334c8ffe1d75411e1ea1827661bef1fe75 100644 --- a/lib/src/main/java/com/lid/lib/utils/TypedAttrUtils.java +++ b/lib/src/main/java/com/lid/lib/utils/TypedAttrUtils.java @@ -25,9 +25,22 @@ import ohos.hiviewdfx.HiLogLabel; import java.util.NoSuchElementException; +/** + * TypedAttrUtils; + * + * @since 2021-04-13 + */ public final class TypedAttrUtils { static final HiLogLabel LABEL = new HiLogLabel(HiLog.LOG_APP, 0x00201, "TypedAttrUtils"); + /** + * getIntColor + * + * @param attrs + * @param attrName + * @param defValue + * @return int + */ public static int getIntColor(AttrSet attrs, String attrName, int defValue) { Attr attr = attrNoSuchElement(attrs, attrName); if (attr == null) { @@ -37,6 +50,14 @@ public final class TypedAttrUtils { } } + /** + * getColor + * + * @param attrs + * @param attrName + * @param defValue + * @return Color + */ public static Color getColor(AttrSet attrs, String attrName, Color defValue) { Attr attr = attrNoSuchElement(attrs, attrName); if (attr == null) { @@ -46,6 +67,14 @@ public final class TypedAttrUtils { } } + /** + * getBoolean + * + * @param attrs + * @param attrName + * @param isDefValue + * @return boolean + */ public static boolean getBoolean(AttrSet attrs, String attrName, boolean isDefValue) { Attr attr = attrNoSuchElement(attrs, attrName); if (attr == null) { @@ -55,6 +84,14 @@ public final class TypedAttrUtils { } } + /** + * getString + * + * @param attrs + * @param attrName + * @param defValue + * @return String + */ public static String getString(AttrSet attrs, String attrName, String defValue) { Attr attr = attrNoSuchElement(attrs, attrName); if (attr == null) { @@ -64,6 +101,14 @@ public final class TypedAttrUtils { } } + /** + * getDimensionPixelSize + * + * @param attrs + * @param attrName + * @param defValue + * @return int + */ public static int getDimensionPixelSize(AttrSet attrs, String attrName, int defValue) { Attr attr = attrNoSuchElement(attrs, attrName); if (attr == null) { @@ -73,6 +118,14 @@ public final class TypedAttrUtils { } } + /** + * getLayoutDimension + * + * @param attrs + * @param attrName + * @param defValue + * @return int + */ public static int getLayoutDimension(AttrSet attrs, String attrName, int defValue) { Attr attr = attrNoSuchElement(attrs, attrName); if (attr == null) { @@ -82,6 +135,13 @@ public final class TypedAttrUtils { } } + /** + * attrNoSuchElement + * + * @param attrs + * @param attrName + * @return Attr + */ private static Attr attrNoSuchElement(AttrSet attrs, String attrName) { Attr attr = null; try {