Android?自定義開源庫?EasyView實現詳解
配置EasyView
這是一個簡單方便的Android自定義View庫,我一直有一個想法弄一個開源庫,現在這個想法付諸實現了,如果有什么需要自定義的View可以提出來,不一定都會采納,合理的會采納,時間周期不保證,咱要量力而行呀,踏實一點。
1. 工程build.gradle 或 settings.gradle配置
代碼已經推送到MavenCentral(),在Android Studio 4.2以后的版本中默認在創(chuàng)建工程的時候使用MavenCentral(),而不是jcenter()。
如果是之前的版本則需要在repositories{}閉包中添加mavenCentral(),不同的是,老版本的Android Studio是在工程的build.gradle中添加,而新版本是工程的settings.gradle中添加,如果已經添加,則不要重復添加。
repositories {
...
mavenCentral()
}
2. 使用模塊的build.gradle配置
例如在app模塊中使用,則打開app模塊下的build.gradle,在dependencies{}閉包下添加即可,之后記得要Sync Now。
dependencies {
implementation 'io.github.lilongweidev:easyview:1.0.2'
}
使用EasyView
這是一個自定義View的庫,會慢慢豐富里面的自定義View,我先畫個餅再說。
一、MacAddressEditText
MacAddressEditText是一個藍牙Mac地址輸入控件,點擊之后出現一個定制的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的所有屬性,可以自行進行設置,使用說明參考下表。
| 屬性 | 說明 |
|---|---|
| app:boxBackgroundColor | 設置輸入框的背景顏色 |
| app:boxStrokeColor | 設置輸入框的邊框顏色 |
| app:boxStrokeWidth | 設置輸入框的邊框大小 |
| app:boxWidth | 設置輸入框大小 |
| app:separator | Mac地址的分隔符,例如分號: |
| app:textColor | 設置輸入框文字顏色 |
| app:textSize | 設置輸入框文字大小 |
3. 代碼中使用
MacAddressEditText macEt = findViewById(R.id.mac_et);
String macAddress = macEt.getMacAddress();
macAddress可能會是空字符串,使用之前請判斷一下,參考app模塊中的MainActivity中的使用方式。
二、CircularProgressBar
CircularProgressBar是圓環(huán)進度條控件。
1. xml中使用
首先是在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的所有屬性,可以自行進行設置,使用說明參考下表。
| 屬性 | 說明 |
|---|---|
| app:maxProgress | 最大進度 |
| app:progress | 當前進度 |
| app:progressbarBackgroundColor | 進度條背景顏色 |
| app:progressbarColor | 進度顏色 |
| app:radius | 半徑,用于設置圓環(huán)的大小 |
| app:strokeWidth | 進度條大小 |
| app:text | 進度條中心文字 |
| app:textColor | 進度條中心文字顏色 |
| app:textSize | 進度條中心文字大小 |
3. 代碼中使用
CircularProgressBar cpbTest = findViewById(R.id.cpb_test);
int progress = 10;
cpbTest.setText(progress + "%");
cpbTest.setProgress(progress);
參考app模塊中的MainActivity中的使用方式。
三、TimingTextView
TimingTextView是計時文字控件
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="計時文字"
android:textColor="@color/black"
android:textSize="32sp"
app:countdown="false"
app:max="60"
app:unit="s" />
2. 屬性介紹
這里使用了TimingTextView的自定義屬性不多,只有3個,TextView的屬性就不列舉說明,使用說明參考下表。
| 屬性 | 說明 |
|---|---|
| app:countdown | 是否倒計時 |
| app:max | 最大時間長度 |
| app:unit | 時間單位:s(秒)、m(分)、h(時) |
3. 代碼中使用
TimingTextView tvTiming = findViewById(R.id.tv_timing);
tvTiming.setMax(6);//最大時間
tvTiming.setCountDown(false);//是否倒計時
tvTiming.setUnit(3);//單位 秒
tvTiming.setListener(new TimingListener() {
@Override
public void onEnd() {
//定時結束
}
});
//開始計時
tvTiming.start();
//停止計時
//tvTiming.end();
參考app模塊中的MainActivity中的使用方式。
以上就是Android 自定義開源庫 EasyView的詳細內容,更多關于Android自定義EasyView的資料請關注腳本之家其它相關文章!
相關文章
Android開發(fā)調用WebService的方法示例
這篇文章主要介紹了Android開發(fā)調用WebService的方法,結合實例形式較為詳細的分析了Android調用WebService的原理、實現方法與相關操作注意事項,需要的朋友可以參考下2017-10-10
Android中DrawerLayout+ViewPager滑動沖突的解決方法
這篇文章主要為大家詳細介紹了Android中DrawerLayout+ViewPager滑動沖突的解決方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-06-06
Android中ViewFlipper的使用及設置動畫效果實例詳解
這篇文章主要介紹了Android中ViewFlipper的使用及設置動畫效果的方法,以實例形式較為詳細的分析了ViewFlipper的功能、原理及設置與使用技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-10-10

