Android實(shí)現(xiàn)儀表盤(pán)控件開(kāi)發(fā)
儀表盤(pán)在工業(yè)軟件中很常見(jiàn),今天整一個(gè)圖片式儀表盤(pán)控件(非幾何圖形繪制)。實(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è)可自行百度搜索想要的字體。然后寫(xiě)一個(gè)儀表盤(pán)控件類(lèi),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中Java根據(jù)文件頭獲取文件類(lèi)型的方法
這篇文章主要介紹了Android中Java根據(jù)文件頭獲取文件類(lèi)型的方法,涉及Android針對(duì)文件屬性的相關(guān)操作技巧,需要的朋友可以參考下2016-08-08詳解關(guān)于Android Studio中安裝和gradle的一些坑
本篇文章主要介紹了關(guān)于Android Studio中安裝和gradle的一些坑,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-10-10Android項(xiàng)目實(shí)戰(zhàn)之Glide 高斯模糊效果的實(shí)例代碼
這篇文章主要介紹了Android項(xiàng)目實(shí)戰(zhàn)之Glide 高斯模糊效果的實(shí)例代碼,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-06-06Activity配置、啟動(dòng)和關(guān)閉activity實(shí)例詳解
這篇文章主要介紹了Activity配置、啟動(dòng)和關(guān)閉activity實(shí)例詳解,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-09-09用原生VideoView進(jìn)行全屏播放時(shí)的問(wèn)題
本篇文章主要介紹了用原生VideoView進(jìn)行全屏播放時(shí)的問(wèn)題,具有一定的參考價(jià)值,下面跟著小編一起來(lái)看下吧2017-01-01android開(kāi)發(fā)教程之listview顯示sqlite數(shù)據(jù)
這篇文章主要介紹了android使用listview顯示sqlite數(shù)據(jù)的方法,需要的朋友可以參考下2014-03-03Android高級(jí)xml布局之輸入框EditText設(shè)計(jì)
這篇文章主要為大家詳細(xì)介紹了Android高級(jí)xml布局之輸入框EditText設(shè)計(jì),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12Android RadioGroup多行顯示效果 解決單選問(wèn)題
這篇文章主要為大家詳細(xì)介紹了Android RadioGroup多行顯示效果,解決單選問(wèn)題,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-11-11