溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Android怎么自定義開源庫EasyView

發(fā)布時(shí)間:2023-04-26 10:09:19 來源:億速云 閱讀:74 作者:zzz 欄目:開發(fā)技術(shù)

這篇文章主要介紹“Android怎么自定義開源庫EasyView”的相關(guān)知識,小編通過實(shí)際案例向大家展示操作過程,操作方法簡單快捷,實(shí)用性強(qiáng),希望這篇“Android怎么自定義開源庫EasyView”文章能幫助大家解決問題。

配置EasyView

這是一個(gè)簡單方便的Android自定義View庫,我一直有一個(gè)想法弄一個(gè)開源庫,現(xiàn)在這個(gè)想法付諸實(shí)現(xiàn)了,如果有什么需要自定義的View可以提出來,不一定都會采納,合理的會采納,時(shí)間周期不保證,咱要量力而行呀,踏實(shí)一點(diǎn)。

1. 工程build.gradle 或 settings.gradle配置

代碼已經(jīng)推送到MavenCentral(),在Android Studio 4.2以后的版本中默認(rèn)在創(chuàng)建工程的時(shí)候使用MavenCentral(),而不是jcenter()。

如果是之前的版本則需要在repositories{}閉包中添加mavenCentral(),不同的是,老版本的Android Studio是在工程的build.gradle中添加,而新版本是工程的settings.gradle中添加,如果已經(jīng)添加,則不要重復(fù)添加。

repositories {
    ...
    mavenCentral()
}

2. 使用模塊的build.gradle配置

例如在app模塊中使用,則打開app模塊下的build.gradle,在dependencies{}閉包下添加即可,之后記得要Sync Now。

dependencies {
    implementation 'io.github.lilongweidev:easyview:1.0.2'
}

使用EasyView

這是一個(gè)自定義View的庫,會慢慢豐富里面的自定義View,我先畫個(gè)餅再說。

一、MacAddressEditText

MacAddressEditText是一個(gè)藍(lán)牙Mac地址輸入控件,點(diǎn)擊之后出現(xiàn)一個(gè)定制的Hex鍵盤,用于輸入值。

1. xml中使用

首先是在xml中添加如下代碼,具體參考app模塊中的activity_main.xml。

    <com.easy.view.MacAddressEditText
        android:id="@+id/mac_et"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:boxBackgroundColor="@color/white"
        app:boxStrokeColor="@color/black"
        app:boxStrokeWidth="2dp"
        app:boxWidth="48dp"
        app:separator=":"
        app:textColor="@color/black"
        app:textSize="14sp" />

2. 屬性介紹

這里使用了MacAddressEditText的所有屬性,可以自行進(jìn)行設(shè)置,使用說明參考下表。

屬性說明
app:boxBackgroundColor設(shè)置輸入框的背景顏色
app:boxStrokeColor設(shè)置輸入框的邊框顏色
app:boxStrokeWidth設(shè)置輸入框的邊框大小
app:boxWidth設(shè)置輸入框大小
app:separatorMac地址的分隔符,例如分號:
app:textColor設(shè)置輸入框文字顏色
app:textSize設(shè)置輸入框文字大小

3. 代碼中使用

    MacAddressEditText macEt = findViewById(R.id.mac_et);
    String macAddress = macEt.getMacAddress();

&emsp; &emsp;macAddress可能會是空字符串,使用之前請判斷一下,參考app模塊中的MainActivity中的使用方式。

二、CircularProgressBar

CircularProgressBar是圓環(huán)進(jìn)度條控件。

1. xml中使用

&emsp; &emsp;首先是在xml中添加如下代碼,具體參考app模塊中的activity_main.xml。

    <com.easy.view.CircularProgressBar
        android:id="@+id/cpb_test"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="30dp"
        app:maxProgress="100"
        app:progress="10"
        app:progressbarBackgroundColor="@color/purple_500"
        app:progressbarColor="@color/purple_200"
        app:radius="80dp"
        app:strokeWidth="16dp"
        app:text="10%"
        app:textColor="@color/teal_200"
        app:textSize="28sp" />

2. 屬性介紹

這里使用了MacAddressEditText的所有屬性,可以自行進(jìn)行設(shè)置,使用說明參考下表。

屬性說明
app:maxProgress最大進(jìn)度
app:progress當(dāng)前進(jìn)度
app:progressbarBackgroundColor進(jìn)度條背景顏色
app:progressbarColor進(jìn)度顏色
app:radius半徑,用于設(shè)置圓環(huán)的大小
app:strokeWidth進(jìn)度條大小
app:text進(jìn)度條中心文字
app:textColor進(jìn)度條中心文字顏色
app:textSize進(jìn)度條中心文字大小

3. 代碼中使用

    CircularProgressBar cpbTest = findViewById(R.id.cpb_test);
    int progress = 10;
    cpbTest.setText(progress + "%");
    cpbTest.setProgress(progress);

參考app模塊中的MainActivity中的使用方式。

三、TimingTextView

TimingTextView是計(jì)時(shí)文字控件

1. xml中使用

首先是在xml中添加如下代碼,具體參考app模塊中的activity_main.xml。

    <com.easy.view.TimingTextView
        android:id="@+id/tv_timing"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="30dp"
        android:text="計(jì)時(shí)文字"
        android:textColor="@color/black"
        android:textSize="32sp"
        app:countdown="false"
        app:max="60"
        app:unit="s" />

2. 屬性介紹

這里使用了TimingTextView的自定義屬性不多,只有3個(gè),TextView的屬性就不列舉說明,使用說明參考下表。

屬性說明
app:countdown是否倒計(jì)時(shí)
app:max最大時(shí)間長度
app:unit時(shí)間單位:s(秒)、m(分)、h(時(shí))

3. 代碼中使用

    TimingTextView tvTiming = findViewById(R.id.tv_timing);
    tvTiming.setMax(6);//最大時(shí)間
    tvTiming.setCountDown(false);//是否倒計(jì)時(shí)
    tvTiming.setUnit(3);//單位 秒
    tvTiming.setListener(new TimingListener() {
        @Override
        public void onEnd() {
            //定時(shí)結(jié)束
        }
    });
    //開始計(jì)時(shí)
    tvTiming.start();
    //停止計(jì)時(shí)
    //tvTiming.end();

關(guān)于“Android怎么自定義開源庫EasyView”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識,可以關(guān)注億速云行業(yè)資訊頻道,小編每天都會為大家更新不同的知識點(diǎn)。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI