Android中自定義控件之液位指示器
由于安卓應(yīng)用很廣泛,在工業(yè)中也常有一些應(yīng)用,比如可以用安卓來去工業(yè)中的一些數(shù)據(jù)進(jìn)行實(shí)現(xiàn)的監(jiān)測(cè),顯示,同時(shí)可以做一些自動(dòng)化控制,當(dāng)然在這里,我不是做這些自動(dòng)化控制方面的研究,只是做一個(gè)控件,液位指示,其實(shí)就是繼承自progressbar,然后重新寫一測(cè)量與繪制,算是對(duì)自定義控件進(jìn)行一下復(fù)習(xí)。
我們要做的最終就是下面這個(gè)效果:
在這里,我們要做到對(duì)這個(gè)指示器的以下屬性可設(shè)置:
容器壁的厚度、容器壁的顏色、容器中液體的寬度、液體總高度、液體當(dāng)前高度的顏色顯示、液體未達(dá)到顏色顯示、當(dāng)前高度的文字指示、指示文字大小的顯示。
對(duì)以上屬性的可以設(shè)置,會(huì)使在實(shí)現(xiàn)應(yīng)用中讓顯示更加直觀人性化。下面就開始我們的指示器的制作。
1.先在項(xiàng)目的res目錄下建一個(gè)resouce文件,用來定義自定義的屬性,這些都會(huì)在下面給出的源碼中給出,新人可以參考下,老家伙你就繞道吧^^:
<?xml version="." encoding="utf-"?> <resources> <declare-styleable name="JianWWIndicateProgress"> <attr name="progress_height" format="dimension" /> <attr name="progress_width" format="dimension" /> <attr name="progress_unreachedcolor" format="color" /> <attr name="progress_reached_color" format="color" /> <attr name="progress_reached_height" format="integer" /> <attr name="progress_cheek_width" format="dimension" /> <attr name="progress_cheek_color" format="color" /> <attr name="progress_reached_textsize" format="dimension" /> <attr name="progress_reached_textcolor" format="color" /> </declare-styleable> </resources>
2.繼承progressbar,這里繼承他主要是為了能夠用progressbar的getProgress()方法得到當(dāng)前的progress,與setProgress()方法等progress中提供的一些方法,便于對(duì)數(shù)據(jù)的一些處理。
package com.jianww.firstview; import com.example.jwwcallback.R; import android.content.Context; import android.content.res.TypedArray; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Paint.Style; import android.graphics.Rect; import android.util.AttributeSet; import android.util.TypedValue; import android.widget.ProgressBar; public class JianWWIndicateProgress extends ProgressBar { private static final int unreached_color = Xaaff;// 未到達(dá)的顏色 private static final int reached_color = Xaaff;// 到達(dá)顏色 private static final int progress_height = ;// 容器中液位的默認(rèn)高度 private static final int progress_width = ;// 容器中液位的寬度 private static final int reached_height = ;// 默認(rèn)到達(dá)到達(dá)的高度 private static final int progress_cheek_width = ;// 容器的邊框?qū)挾? private static final int progress_cheek_color = xff;// 容器的邊框顏色 private static final int progress_reached_textsize = ;// 指示字體尺寸 private static final int progress_reached_textcolor = Xffff;// 指示字體顏色 protected int unReachedColor;// 未到達(dá)的顏色 protected int reachedColor;// 到達(dá)顏色 protected int progressHeight;// 容器中液位的總高度 protected int progressWidth;// 容器中液面的寬度 protected int reachedHeight;// 默認(rèn)液位到達(dá)的到達(dá)的高度 protected int cheekWidth;// 容器的邊框?qū)挾? protected int cheekColor;// 容器的邊框顏色 protected int reachedTextsize;// 指示字體尺寸 protected int reachedTextcolor;// 指示字體顏色 protected float widthZoom; protected float heightZoom; /** * dp px * */ protected int dppx(int dpVal) { return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dpVal, getResources().getDisplayMetrics()); } /** * sp px * */ protected int sppx(int spVal) { return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, spVal, getResources().getDisplayMetrics()); } private Paint paintCheek = new Paint(); private Paint paint = new Paint(); private float radio; private float progressNowHeight; public JianWWIndicateProgress(Context context) { this(context, null); } public JianWWIndicateProgress(Context context, AttributeSet asets) { this(context, asets, ); } public JianWWIndicateProgress(Context context, AttributeSet asets, int defStyle) { super(context, asets, defStyle); obtainStyledAttributes(asets); // paint.setTextSize(reachedTextsize); // paint.setColor(reachedTextcolor); } /** * 定義屬性 * * @param asets屬性 */ private void obtainStyledAttributes(AttributeSet asets) { final TypedArray typeArray = getContext().obtainStyledAttributes(asets, R.styleable.JianWWIndicateProgress); unReachedColor = typeArray.getColor(R.styleable.JianWWIndicateProgress_progress_unreachedcolor, unreached_color); reachedColor = typeArray.getColor(R.styleable.JianWWIndicateProgress_progress_reached_color, reached_color);// 到達(dá)顏色 progressHeight = (int) typeArray.getDimension(R.styleable.JianWWIndicateProgress_progress_height, progress_height); progressWidth = dppx( (int) typeArray.getDimension(R.styleable.JianWWIndicateProgress_progress_width, progress_width));// 容器的總寬度 reachedHeight = (int) typeArray.getDimension(R.styleable.JianWWIndicateProgress_progress_reached_height, reached_height);// 到達(dá)的高度 cheekWidth = (int) typeArray.getDimension(R.styleable.JianWWIndicateProgress_progress_cheek_width, progress_cheek_width);// 容器的邊框?qū)挾? cheekColor = typeArray.getColor(R.styleable.JianWWIndicateProgress_progress_cheek_color, progress_cheek_color); reachedTextsize = (int) typeArray.getDimension(R.styleable.JianWWIndicateProgress_progress_reached_textsize, progress_reached_textsize);// 指示字體尺寸 reachedTextcolor = typeArray.getColor(R.styleable.JianWWIndicateProgress_progress_reached_textcolor, progress_reached_textcolor);// 指示字體顏色 typeArray.recycle(); } /** * onMeasure是對(duì)繪出的控件將來占的空間進(jìn)行的安排, */ @Override protected synchronized void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); int totalWidth = measurdWidth(widthMeasureSpec); int totalHeight = measurdHeight(heightMeasureSpec); setMeasuredDimension(totalWidth, totalHeight); } /** * * @param widthMeasureSpec * @return 獲取寬度尺寸 */ private int measurdWidth(int widthMeasureSpec) { int result = ; int widthSpaceSize = MeasureSpec.getSize(widthMeasureSpec); int widthSpaceMode = MeasureSpec.getMode(widthMeasureSpec); if (widthSpaceMode == MeasureSpec.EXACTLY) { result = widthSpaceSize; widthZoom = widthSpaceSize/(progressWidth+*cheekWidth); progressWidth = (int) (progressWidth * widthZoom); } else if (widthSpaceMode == MeasureSpec.UNSPECIFIED) { result = Math.max(widthSpaceSize, getPaddingLeft() + getPaddingRight() + progressWidth + * cheekWidth); } else if (widthSpaceMode == MeasureSpec.AT_MOST) { result = Math.min(widthSpaceSize, getPaddingLeft() + getPaddingRight() + progressWidth + * cheekWidth); } return result; } /** * * @param heightMeasureSpec * @return獲取高度尺寸 */ private int measurdHeight(int heightMeasureSpec) { int result = ; int heightSpaceSize = MeasureSpec.getSize(heightMeasureSpec); int heightSpaceMode = MeasureSpec.getMode(heightMeasureSpec); if (heightSpaceMode == MeasureSpec.EXACTLY) { result = heightSpaceSize; heightZoom = heightSpaceSize/(progressHeight+*cheekWidth); progressHeight = (int) (progressHeight*heightZoom); } else if (heightSpaceMode == MeasureSpec.UNSPECIFIED) { result = Math.max(heightSpaceSize, getPaddingTop() + getPaddingBottom() + progressHeight + * cheekWidth); } else if (heightSpaceMode == MeasureSpec.AT_MOST) { result = Math.min(heightSpaceSize, getPaddingTop() + getPaddingBottom() + progressHeight + * cheekWidth); } return result; } /** * 繪制控件 */ @Override protected synchronized void onDraw(Canvas canvas) { super.onDraw(canvas); radio = getProgress() * .f / getMax(); progressNowHeight = (int) (progressHeight * radio); // 存繪畫前畫布狀態(tài) canvas.save(); // 把畫布移動(dòng)到要繪制的點(diǎn) canvas.translate(getPaddingLeft(), getPaddingRight()); // 繪制容器外殼 paintCheek.setColor(cheekColor);// 畫筆顏色 paintCheek.setAntiAlias(true);// 是否過度 paintCheek.setStyle(Style.STROKE);// 空心 paintCheek.setStrokeWidth(cheekWidth);// 邊框的寬度 canvas.drawRect(cheekWidth/, cheekWidth/, progressWidth + cheekWidth*/, progressHeight + cheekWidth*/, paintCheek); // 繪總液位 paint.setColor(unReachedColor); paint.setStyle(Style.FILL); canvas.drawRect(cheekWidth, cheekWidth, progressWidth+cheekWidth, progressHeight+cheekWidth, paint); // 繪當(dāng)前液位 paint.setStyle(Style.FILL); paint.setColor(reachedColor); canvas.drawRect(cheekWidth, cheekWidth + progressHeight - progressNowHeight, progressWidth + cheekWidth, progressHeight + cheekWidth, paint); // 繪液位指示 String text = getProgress() + "%"; paint.setTextSize(reachedTextsize); paint.setColor(reachedTextcolor); float textHeight = sppx(reachedTextsize)/; if(progressNowHeight >= progressHeight/){ canvas.drawText(text, cheekWidth + progressWidth / , cheekWidth + progressHeight - progressNowHeight+textHeight, paint); }else{ canvas.drawText(text, cheekWidth + progressWidth / , cheekWidth + progressHeight - progressNowHeight, paint); } canvas.restore(); } }
3.就是在布局中的引用了
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:jwwprogress="http://schemas.android.com/apk/res-auto" xmlns:app="http://schemas.android.com/apk/res/com.example.jwwcallback" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" > <com.jianww.firstview.JianWWIndicateProgress android:id="@+id/jp_progress" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:max="" app:progress_cheek_color="#ff" app:progress_cheek_width="dp" app:progress_height="dp" app:progress_reached_color="#ff" app:progress_reached_textcolor="#" app:progress_reached_textsize="sp" app:progress_unreachedcolor="#ff" app:progress_width="dp" /> </RelativeLayout>
4.就是在acitivity中的初始化與使用。
package com.example.jwwmain; import java.io.IOException; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import java.util.Random; import com.example.jwwcallback.R; import com.jianww.firstview.JianWWIndicateProgress; import android.app.Activity; import android.os.Bundle; import android.os.Handler; public class MainActivity extends Activity { private JianWWIndicateProgress jprogress; private int nowProgress; private Handler mHandler = new Handler() { public void handleMessage(android.os.Message msg) { int progress = jprogress.getProgress(); jprogress.setProgress(++progress); if (progress >= ) { jprogress.setProgress(); } mHandler.sendEmptyMessageDelayed(, ); }; }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); } private void initView() { jprogress = (JianWWIndicateProgress) findViewById(R.id.jp_progress); mHandler.sendEmptyMessage(); } }
關(guān)于Android中自定義控件之液位指示器的相關(guān)知識(shí)就給大家介紹到這里,希望對(duì)大家有所幫助!
- Android自定義控件(實(shí)現(xiàn)視圖樹繪制指示器)
- Android自定義ViewPager指示器
- Android實(shí)現(xiàn)仿網(wǎng)易新聞的頂部導(dǎo)航指示器
- Android應(yīng)用中仿今日頭條App制作ViewPager指示器
- Android應(yīng)用中使用ViewPager和ViewPager指示器來制作Tab標(biāo)簽
- Android之IphoneTreeView帶組指示器的ExpandableListView效果
- Android之帶group指示器的ExpandableListView(自寫)
- Android自定義View Flyme6的Viewpager指示器
相關(guān)文章
Flutter底部導(dǎo)航欄的實(shí)現(xiàn)方式
這篇文章主要為大家詳細(xì)介紹了Flutter底部導(dǎo)航欄的實(shí)現(xiàn)方式,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-02-02AndroidStudio Gradle基于友盟的多渠道打包方法
這篇文章主要介紹了AndroidStudio Gradle基于友盟的多渠道打包方法,需要的朋友可以參考下2017-09-09Android自定義頂部導(dǎo)航欄控件實(shí)例代碼
這篇文章主要介紹了Android自定義頂部導(dǎo)航欄控件實(shí)例代碼,需要的朋友可以參考下2017-12-12Android Studio用genymotion運(yùn)行后小圖標(biāo)無法顯示問題
這篇文章主要介紹了Android Studio用genymotion運(yùn)行后小圖標(biāo)無法顯示的問題,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-04-04Android讀取手機(jī)通訊錄聯(lián)系人到自己項(xiàng)目
這篇文章主要為大家詳細(xì)介紹了Android讀取手機(jī)通訊錄聯(lián)系人到自己項(xiàng)目,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-07-07Android6.0獲取GPS定位和獲取位置權(quán)限和位置信息的方法
今天小編就為大家分享一篇Android6.0獲取GPS定位和獲取位置權(quán)限和位置信息的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-07-07Android學(xué)習(xí)筆記——Menu介紹(一)
Android3.0(API level 11)開始,Android設(shè)備不再需要專門的菜單鍵。隨著這種變化,Android app應(yīng)該取消對(duì)傳統(tǒng)6項(xiàng)菜單的依賴。取而代之的是提供anction bar來提供基本的用戶功能2014-10-10Android畫板開發(fā)之添加背景和保存畫板內(nèi)容為圖片
這篇文章主要為大家詳細(xì)介紹了Android畫板開發(fā)之添加背景和保存畫板內(nèi)容為圖片,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-12-12