Since Android is widely used, in the industry also have a number of applications, such as the use of Android to the industry in some data to achieve monitoring, display, and can do some automation control, of course, here, I do not do these automation control aspects of research, just do a control, level instructions, In fact, is inherited from ProgressBar, and then write a measurement and drawing, is a custom control for a review.
What we're going to do is end up with the following effect:
Here we are going to do the following properties on this indicator can be set:
The thickness of the container wall, the color of the container wall, the width of the liquid in the container, the total height of the liquid, the color display of the current height of the liquid, the liquid does not reach the color display, the current height of the text indication, indicating the text size display.
The above properties can be set, so that in the implementation of the application to make the display more intuitive human. The following is the beginning of our indicator production.
1. First in the project's res directory to build a resouce file , used to define custom attributes, these will be given in the following source code given, the new can refer to the old guy you take the detour ^ ^:
<?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" 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>
2. Inheriting ProgressBar, which inherited him primarily in order to be able to get the current progress with the ProgressBar getprogress () method, and Setprogress () Methods such as progress provide some methods to facilitate the processing of data.
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;//not reached color Priva Te static final int reached_color = xaaff;//to reach color private static final int progress_height =//The default height of the liquid level in a container private static Final int progress_width =//The width of the liquid level in the container private static final int reached_height =//default arrival height private static final int Progress_cheek_width =//container border width private static final int progress_cheek_color = xff;//container border color private static final int Progress_reached_textsize =//indicates font size private static final int progress_reached_textcolor = xffff;//indicates font color protected int unreachedcolor;//The unreachable color protected int reachedcolor;//to the total height of the liquid level in protected int progressheight;//container protected The width of the liquid level in the Int progresswidth;//container protected int reachedheight;//The reached height of the default liquid level protected the border width of the Int cheekwidth;//container protected in The border color of the T cheekcolor;//container protected int reachedtextsize;//indicates the font size protected int reachedtextcolor;//indicates the font color protected float W
Idthzoom;
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, SPVA
L, Getresources (). Getdisplaymetrics ());
Private Paint Paintcheek = new Paint ();
Private Paint Paint = new Paint ();
private float Radio;
private float progressnowheight; Public Jianwwindicateprogress {This (context, null);} public jianwwindicateprogress AttributeSet asets) {THe (context, asets,); Public jianwwindicateprogress (context, AttributeSet asets, int defstyle) {Super (context, asets, defstyle); OBTA
Instyledattributes (asets);
Paint.settextsize (reachedtextsize);
Paint.setcolor (Reachedtextcolor); /** * Define Properties * * @param asets property/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); Reach Color progressheight = (int) typearray.getdimension (r.styleable.jianwwindicateprogress_progress_height, Progress_
height); Progresswidth = dppx (int) typearray.getdimension (r.styleable.jianwwindicateprogress_progress_width, Progress_ width));//container's total widths reachedheight = (int) typearray.getdimension (r.styleable.jianwwindicateproGress_progress_reached_height, reached_height);//reach height cheekwidth = (int) typearray.getdimension ( R.styleable.jianwwindicateprogress_progress_cheek_width, progress_cheek_width);//container border Width 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)//Indicates the font size Reachedtextcolor = Typearray.getcolor (r.styleable.jianwwindicateprogress_
Progress_reached_textcolor, Progress_reached_textcolor);//indicates the font color typearray.recycle (); /** * Onmeasure is an arrangement for the future space of the drawn control, */@Override protected synchronized void onmeasure (int widthmeasurespec, int heightme
Asurespec) {super.onmeasure (Widthmeasurespec, heightmeasurespec); int totalwidth = Measurdwidth (WidthMeasureSpec);
int totalheight = Measurdheight (Heightmeasurespec);
Setmeasureddimension (totalwidth, totalheight); /** * * @param widthmeasurespec * @return Get width size/private int measurdwidth (int widthmeasurespec) {int. result =; int widthspacesize = Measurespec.getsize (Widthmeasurespec);
NT 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 () + Getpaddingrig
HT () + progresswidth + * cheekwidth); else if (Widthspacemode = = measurespec.at_most) {result = Math.min (Widthspacesize, Getpaddingleft () + getpaddingright ()
+ progresswidth + * cheekwidth);
return result; /** * * @param heightmeasurespec * @return Get the height dimension */private int measurdheight (int heightmeasurespec) {int result = I
NT 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 () + Getpaddingbo
Ttom () + progressheight + * cheekwidth); else if (Heightspacemode = = measurespec.at_most) {result = Math.min (Heightspacesize, getpaddingtop () + Getpaddingbottom
() + progressheight + * cheekwidth);
return result; /** * Draw Control/@Override protected synchronized void OnDraw (Canvas Canvas) {Super.ondraw (Canvas); radio = Getprogress ()
*. F/getmax ();
Progressnowheight = (int) (Progressheight * radio);
Canvas.save () of the canvas before painting;
Move the canvas to the point canvas.translate (Getpaddingleft (), Getpaddingright ()) to be drawn; Draw container Shell Paintcheek.setcolor (cheekcolor);//Brush color Paintcheek.setantialias (TRUE);//whether excessive paintcheek.setstyle ( Style.stroke);//Hollow Paintcheek.setstrokewidth (Cheekwidth);//Border Width canvas.drawrect (cheekwidth/, cheekwidth/, Progresswidth + cheekwidth*/, ProgresSheight + cheekwidth*/, Paintcheek);
Paint Total liquid level paint.setcolor (Unreachedcolor);
Paint.setstyle (Style.fill);
Canvas.drawrect (Cheekwidth, Cheekwidth, Progresswidth+cheekwidth, progressheight+cheekwidth, paint);
Paint Current liquid level paint.setstyle (Style.fill);
Paint.setcolor (Reachedcolor); Canvas.drawrect (cheekwidth, Cheekwidth + progressheight-progressnowheight, Progresswidth + cheekWidth, progressHeight
+ cheekwidth, paint);
Draw liquid level indicator String Text = getprogress () + "%";
Paint.settextsize (reachedtextsize);
Paint.setcolor (Reachedtextcolor);
float textHeight = sppx (reachedtextsize)/; if (progressnowheight >= progressheight/) {canvas.drawtext (text, Cheekwidth + progresswidth/, CheekWidth + progressHe
Ight-progressnowheight+textheight, paint); }else{canvas.drawtext (text, Cheekwidth + progresswidth/, Cheekwidth + progressheight-progressnowheight, paint); can
Vas.restore (); }
}
3. Is the reference in the layout
<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"/>
4. Is the initialization and use in the 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 Ha Ndler 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 (); }
}
About the knowledge of the liquid level indicator of the custom control in Android I would like to introduce to you here, I hope to help you!