av一区二区在线观看_亚洲男人的天堂网站_日韩亚洲视频_在线成人免费_欧美日韩精品免费观看视频_久草视

您的位置:首頁技術(shù)文章
文章詳情頁

Android實現(xiàn)儀表盤控件開發(fā)

瀏覽:93日期:2022-09-18 13:34:15

儀表盤在工業(yè)軟件中很常見,今天整一個圖片式儀表盤控件(非幾何圖形繪制)。實現(xiàn)非常簡單,一張背景圖,一張指針。創(chuàng)建一個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'> <ImageViewandroid: android:layout_width='wrap_content'android:layout_height='wrap_content'android:layout_centerInParent='true'android:src='http://www.4tl426be.cn/bcjs/@mipmap/meter_h_bk' /> <ImageViewandroid: android:layout_width='wrap_content'android:layout_height='wrap_content'android:layout_alignBottom='@id/id_dial'android:layout_centerInParent='true'android:src='http://www.4tl426be.cn/bcjs/@mipmap/meter_fr' /> <com.tech.view.DigitalTextandroid: 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是使用第三方字體控件,這個可自行百度搜索想要的字體。然后寫一個儀表盤控件類,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;//理論上要小于offsetfloat curProgress = (dialPoint.getRotation() - MIN) / offset;//計算當(dāng)前進度和目標進度方向,步數(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);//測量控件大小}int width = dialPoint.getMeasuredWidth();int height = dialPoint.getMeasuredHeight();dialPoint.post(() -> { dialPoint.setPivotX(width * 0.5f); dialPoint.setPivotY(height * 0.82666f); dialPoint.setRotation(rotation);}); }}

最終效果圖

Android實現(xiàn)儀表盤控件開發(fā)

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。

標簽: Android
相關(guān)文章:
主站蜘蛛池模板: 精品久久久久久久久久久久 | 欧美乱码精品一区二区三区 | 国产欧美精品一区二区色综合朱莉 | 免费a国产| 国产成人99久久亚洲综合精品 | 亚洲久在线 | 久久香焦 | 国产成人免费观看 | 99精品免费视频 | 久久国产欧美日韩精品 | www.yw193.com | 欧美久久久久久久 | 成人精品在线视频 | av在线伊人| 超碰免费在线 | 动漫www.被爆羞羞av44 | 国产乱人伦 | 精品国产一区二区三区久久久蜜月 | 国内精品视频免费观看 | 欧美午夜精品久久久久久浪潮 | 亚洲精品电影网在线观看 | 91黄在线观看 | 成人午夜免费网站 | re久久 | 成人免费视频在线观看 | 精品视频网 | 国产情品 | 九九综合九九 | 亚洲综合大片69999 | 日本在线你懂的 | 欧美日韩中文在线 | 成人a视频| 国内精品久久精品 | 欧美高清视频一区 | 午夜精品久久久久久不卡欧美一级 | 成人精品一区 | 色婷婷久久久亚洲一区二区三区 | 一级毛片在线播放 | 欧美精品在欧美一区二区少妇 | 日韩欧美一区二区在线播放 | 91热在线 |