Android實(shí)現(xiàn)儀表盤控件開發(fā)
儀表盤在工業(yè)軟件中很常見,今天整一個(gè)圖片式儀表盤控件(非幾何圖形繪制)。實(shí)現(xiàn)非常簡(jiǎn)單,一張背景圖,一張指針。創(chuàng)建一個(gè)RelativeLayout布局文件,然后在里面布置好控件的位置,代碼如下
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/id_dial"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="@mipmap/meter_h_bk" />
<ImageView
android:id="@+id/id_dial_point"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@id/id_dial"
android:layout_centerInParent="true"
android:src="@mipmap/meter_fr" />
<com.tech.view.DigitalText
android:id="@+id/id_value"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@id/id_dial"
android:layout_centerHorizontal="true"
android:layout_marginBottom="2dp"
android:text="29.9"
android:textColor="@android:color/white"
android:textSize="21sp" />
</RelativeLayout>
DigitalText是使用第三方字體控件,這個(gè)可自行百度搜索想要的字體。然后寫一個(gè)儀表盤控件類,DialBoard
代碼如下
public class DialBoard {
private static final String TAG = DialBoard.class.getName();
private ImageView dialPoint;
private TextView text;
private static final float MAX = 120.0f;
private static final float MIN = -120.0f;
private boolean animationDone = true;
public DialBoard(Activity activity) {
dialPoint = activity.findViewById(R.id.id_dial_point);
text = activity.findViewById(R.id.id_value);
text.setText(String.format("%.02f", 0.0f));
setRotation(MIN, true);
}
public synchronized void moveTo(float progress) {
if (animationDone) new Thread(() -> move(progress)).start();
}
private void move(float progress) {
animationDone = false;
float offset = (MAX - MIN) / 100;
final float STEP = 2.0f;//理論上要小于offset
float curProgress = (dialPoint.getRotation() - MIN) / offset;
//計(jì)算當(dāng)前進(jìn)度和目標(biāo)進(jìn)度方向,步數(shù)
float value = Math.abs(curProgress - progress);
while (value > 0.1f) {
value = (value < 1.0f ? 0.2f : STEP);
if (progress < curProgress) value = -value;
setRotation(dialPoint.getRotation() + value, false);
Tools.sleep(10);
curProgress = (dialPoint.getRotation() - MIN) / offset;
value = Math.abs(curProgress - progress);
final float textValue = curProgress;
text.post(() -> text.setText(String.format("%.02f", textValue)));
}
text.post(() -> text.setText(String.format("%.02f", progress)));
animationDone = true;
}
/**
* @param rotation MIN~MAX
*/
public void setRotation(float rotation) {
this.setRotation(rotation, false);
}
private void setRotation(float rotation, boolean onCreate) {
if (onCreate) {
int w = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
int h = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
dialPoint.measure(w, h);//測(cè)量控件大小
}
int width = dialPoint.getMeasuredWidth();
int height = dialPoint.getMeasuredHeight();
dialPoint.post(() -> {
dialPoint.setPivotX(width * 0.5f);
dialPoint.setPivotY(height * 0.82666f);
dialPoint.setRotation(rotation);
});
}
}
最終效果圖

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
android layout XML解析錯(cuò)誤的解決方法
從別的地方復(fù)制過來(lái)XML時(shí),layout預(yù)覽時(shí)提示解析錯(cuò)誤。2013-04-04
Android Listview點(diǎn)贊問題關(guān)于圖片重復(fù)問題
最近在開發(fā)android方面的項(xiàng)目時(shí),遇到很多問題,下面小編以listview 與 baseadapter結(jié)合使用為例,給大家分享下關(guān)于點(diǎn)贊的的時(shí)候 圖片重復(fù)問題的解決方法,一起看看吧2016-11-11
Android自定義scrollview實(shí)現(xiàn)回彈效果
這篇文章主要為大家詳細(xì)介紹了Android自定義scrollview實(shí)現(xiàn)回彈效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04
Android開發(fā)中PopupWindow用法實(shí)例分析
這篇文章主要介紹了Android開發(fā)中PopupWindow用法,結(jié)合實(shí)例形式分析了PopupWindow彈出窗口效果的使用技巧,需要的朋友可以參考下2016-02-02
Android 5秒學(xué)會(huì)使用手勢(shì)解鎖功能
本文講述的是一個(gè)手勢(shì)解鎖的庫(kù),可以定制顯示隱藏宮格點(diǎn)、路徑、并且?guī)в行【艑m格顯示圖,和震動(dòng)!讓你學(xué)會(huì)使用這個(gè)簡(jiǎn)單,高效的庫(kù),好了,具體內(nèi)容詳情大家通過本文學(xué)習(xí)吧2017-12-12
Android打賞功能實(shí)現(xiàn)代碼(支付寶轉(zhuǎn)賬)
這篇文章主要介紹了Android打賞功能之支付寶轉(zhuǎn)賬 ,需要的朋友可以參考下2017-12-12
詳解有關(guān)Android截圖與錄屏功能的學(xué)習(xí)
這篇文章主要介紹了詳解有關(guān)Android截圖與錄屏功能的學(xué)習(xí),詳細(xì)介紹如何使用MediaProjection,MediaCodec以及MediaMuxer來(lái)實(shí)現(xiàn)簡(jiǎn)單的截屏和錄屏功能。2017-04-04
Android仿微信和QQ多圖合并框架(類似群頭像)的實(shí)現(xiàn)方法
這篇文章主要給大家介紹了關(guān)于Android仿微信和QQ多圖合并框架的相關(guān)資料,其實(shí)就是我們平時(shí)所見的群聊頭像,文中通過示例代碼介紹的非常詳細(xì),對(duì)各位Android開發(fā)者們具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2017-12-12

