您好,登錄后才能下訂單哦!
這篇文章主要介紹“Android怎么自定義開源庫EasyView”的相關(guān)知識,小編通過實(shí)際案例向大家展示操作過程,操作方法簡單快捷,實(shí)用性強(qiáng),希望這篇“Android怎么自定義開源庫EasyView”文章能幫助大家解決問題。
這是一個(gè)簡單方便的Android自定義View庫,我一直有一個(gè)想法弄一個(gè)開源庫,現(xiàn)在這個(gè)想法付諸實(shí)現(xiàn)了,如果有什么需要自定義的View可以提出來,不一定都會采納,合理的會采納,時(shí)間周期不保證,咱要量力而行呀,踏實(shí)一點(diǎn)。
代碼已經(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() }
例如在app
模塊中使用,則打開app模塊下的build.gradle
,在dependencies{}
閉包下添加即可,之后記得要Sync Now
。
dependencies { implementation 'io.github.lilongweidev:easyview:1.0.2' }
這是一個(gè)自定義View的庫,會慢慢豐富里面的自定義View,我先畫個(gè)餅再說。
MacAddressEditText是一個(gè)藍(lán)牙Mac地址輸入控件,點(diǎn)擊之后出現(xiàn)一個(gè)定制的Hex鍵盤,用于輸入值。
首先是在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" />
這里使用了MacAddressEditText的所有屬性,可以自行進(jìn)行設(shè)置,使用說明參考下表。
屬性 | 說明 |
---|---|
app:boxBackgroundColor | 設(shè)置輸入框的背景顏色 |
app:boxStrokeColor | 設(shè)置輸入框的邊框顏色 |
app:boxStrokeWidth | 設(shè)置輸入框的邊框大小 |
app:boxWidth | 設(shè)置輸入框大小 |
app:separator | Mac地址的分隔符,例如分號: |
app:textColor | 設(shè)置輸入框文字顏色 |
app:textSize | 設(shè)置輸入框文字大小 |
MacAddressEditText macEt = findViewById(R.id.mac_et); String macAddress = macEt.getMacAddress();
   macAddress可能會是空字符串,使用之前請判斷一下,參考app模塊中的MainActivity中的使用方式。
CircularProgressBar是圓環(huán)進(jìn)度條控件。
   首先是在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" />
這里使用了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)度條中心文字大小 |
CircularProgressBar cpbTest = findViewById(R.id.cpb_test); int progress = 10; cpbTest.setText(progress + "%"); cpbTest.setProgress(progress);
參考app模塊中的MainActivity中的使用方式。
TimingTextView是計(jì)時(shí)文字控件
首先是在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" />
這里使用了TimingTextView的自定義屬性不多,只有3個(gè),TextView的屬性就不列舉說明,使用說明參考下表。
屬性 | 說明 |
---|---|
app:countdown | 是否倒計(jì)時(shí) |
app:max | 最大時(shí)間長度 |
app:unit | 時(shí)間單位:s(秒)、m(分)、h(時(shí)) |
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)。
免責(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)容。