Android開源:AvatarLabelView - 可配置的迷你版輕量級 Label 輔助類

AvatarLabelView

一個可配置的迷你版輕量級 Label 輔助類,支持多種配置效果,具體不同配置展示效果如下圖。

說明文檔

如下是關于 Label View 的相關使用方式、屬性說明、拓展自定義的解釋說明。

使用樣例

<cn.label.avatarlabelview.LabelImageView
    app:textContent="晚場"
    app:textContentSize="14sp"
    app:direction="rightTop"
    app:labelTopDistance="20dp"
    app:labelTopPadding="10dp"
    app:labelBottomPadding="10dp"
    app:backgroundColor="@color/colorPrimaryDark"
    android:src="@mipmap/ic_launcher"
    android:background="#f3a212"
    android:layout_width="150dp"
    android:layout_height="100dp"/>

已實現類說明

類別 類名 說明
library LabelViewHelper 標簽輔助核心實現類
library LabelView 基于 LabelViewHelper 實現的一個純標簽 View,可嵌套在 ViewGroup 中使用等
demo LabelImageView 基于 LabelViewHelper 實現的一個具備標簽的 ImageView,可屬性配置等
demo LabelLinearLayout 基于 LabelViewHelper 實現的一個具備標簽的 LinearLayout,可屬性配置等
customer XxxView 類比上面 demo 中基于 LabelViewHelper 實現自己的 Label View

屬性說明

屬性 含義
app:backgroundColor Label 的背景顏色
app:textTitleColor 第一行文字的顏色,如果 Label 作為單行(不設置textTitle)則無效
app:textContentColor 第二行文字的顏色
app:textTitle 第一行文字的內容,如果文字過長注意調節 labelTopPadding 的值變大,單行 Label 時不要設置此值
app:textContent 第二行文字的內容,單行顯示時推薦用此
app:textTitleSize 第一行文字的大小,默認10sp,如果 Label 作為單行(不設置textTitle)則無效
app:textContentSize 第二行文字的大小,默認12sp
app:labelTopPadding 第一行文字上邊緣距離背景頂部(注意三角形或者梯形)的偏移量,默認為15dp
app:labelCenterPadding 第一行文字底部與第二行文字頂部之間的偏移距離
app:labelBottomPadding textContent 文字與 Label 背景底部的空隙距離,默認為 10dp
app:labelTopDistance 當設置該值大于0時顯示的 Label 為梯形的樣式,梯形上頂寬度與該值成正比;當不設置時 Label 為三角形樣式
app:textTitleStyle 第一行文字的樣式,normal、italic、bold,如果 Label 作為單行(不設置textTitle)則無效
app:textContentStyle 第二行文字的樣式,normal、italic、bold
app:direction Label 的位置,leftTop 或者 rightTop

拓展為自己 View 使用方式

  1. 在自己的自定義 View 構造方法創建 LabelViewHelper 對象。
  2. 在自己的自定義 View 相關繪制方法(onDraw、dispatchDraw 等)中調用 LabelViewHelper 的 drawLabel 方法。
  3. 至此你的自定義 View 就支持可配置的 Label 效果了,如需別的拓展可以參考 demo 或者查看 LabelViewHelper 其他方法。

具體拓展應用到自己自定義的其他控件中如下:

public class LabelImageView extends YourCustomerView {
    //......
    public LabelImageView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mLabelViewHelper = new LabelViewHelper(context, attrs);
    }

//注意:ViewGroup 最好重寫 dispatchDraw 方法
@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    mLabelViewHelper.drawLabel(this, canvas);
}
//......

}</code></pre>

 

 

 本文由用戶 DortheaRBP 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!