This article will use two methods to write a similar car load progress
Add a For loop with the LinearLayout AddView method
Ways to use custom controls
Take a screenshot first.
1. Add a For loop with the LinearLayout AddView method
1.1 Processtest01.xml File:
<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android=
"http://schemas.android.com/apk/" Res/android "
android:layout_width=" match_parent "
android:layout_height=" match_parent "
android:o" rientation= "Horizontal" >
<linearlayout
android:id= "@+id/ll" android:layout_width= "WRAP_"
Content "
android:layout_height=" wrap_content "
android:orientation=" Horizontal ">
</ Linearlayout>
</LinearLayout>
Sub-layout of 1.2 linearlayout view01.xml
<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android=
"http://schemas.android.com/apk/" Res/android "
android:layout_width=" wrap_content "
android:layout_height=" wrap_content "
android:o" rientation= "vertical" >
<imageview
android:id= "@+id/hezai_img"
android:layout_width= "12.5DP"
android:layout_height= "31.5DP"/>
</LinearLayout>
1.3 Processtest01.java
Package com.example.progresstest;
Import android.app.Activity;
Import Android.os.Bundle;
Import Android.view.LayoutInflater;
Import Android.view.View;
Import Android.widget.ImageView;
Import Android.widget.LinearLayout;
public class ProcessTest01 extends activity {private LinearLayout ll; @Override protected void OnCreate (Bundle savedinstancestate) {//TODO auto-generated method stub super.oncreate
(savedinstancestate);
Setcontentview (r.layout.processtest01);
ll = (linearlayout) Findviewbyid (R.ID.LL);
Setprocess (16);
private void setprocess (int green) {Layoutinflater inflater = This.getlayoutinflater ();
Fill in the green picture for (int i = 0; i < green; i++) {View v = inflater.inflate (r.layout.view01, NULL);
ImageView img = (imageview) V.findviewbyid (r.id.hezai_img);
Img.setimageresource (R.drawable.green);
Ll.addview (v); ///No progress fills the gray picture for (int i = 0; i < 24-green i++) {View v = iNflater.inflate (r.layout.view01, NULL);
ImageView img = (imageview) V.findviewbyid (r.id.hezai_img);
Img.setimageresource (R.drawable.gray);
Ll.addview (v);
}
}
}
1.4 This involves two picture elements with boundaries.
Finally get the screenshot
There are advantages and disadvantages to this method. This method is simple and easy to understand, and consumes less memory, fast and so on, but to different colors, it is necessary to intercept different sizes, different colors of the picture, and also to the left and right with spacing, more trouble. One of the following methods is the method of customizing the control, and there are both good and bad.
2 ways to use custom controls
2.1 Myprocesstest.xml File
<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android=
"http://schemas.android.com/apk/" Res/android "
android:layout_width=" match_parent "
android:layout_height=" match_parent "
android:o" rientation= "vertical" >
<com.example.progresstest.hezaiprogress
android:layout_margintop= "20DP"
android:layout_gravity= "center"
android:id= "@+id/process"
android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
>
</com.example.progresstest.HezaiProgress>
</LinearLayout>
The com.example.progresstest here is the package name where the hezaiprogress resides, and Hezaiprogress is the custom control code.
2.2 Hezaiprogress.java File
Package com.example.progresstest;
Import Android.content.Context;
Import Android.graphics.Canvas;
Import Android.graphics.Color;
Import Android.graphics.Paint;
Import Android.graphics.Rect;
Import Android.util.AttributeSet;
Import Android.util.Log;
Import Android.view.View;
Import Android.view.View.MeasureSpec;
Import Android.widget.ImageView;
Import Android.widget.LinearLayout;
Import Android.widget.LinearLayout.LayoutParams;
public class Hezaiprogress extends view{private int process = 10;
private int width,height;
private int h = 32;
private int w = 12;
private int dividewidth=5;
private int maxcount = 24;
private int processcolor=color.green;
private int j = 1;
private int num = 0;
public int Getprocesscolor () {return processcolor; /** * Custom color, input "#ffffff" in the form of a color String * @param processcolor/public void Setprocesscolor (string processcolor)
{Char c = processcolor.charat (0);
int r = 0;
int g = 0;
int b = 0;
The default color is green int RGB = Color.green;
if (c== ' # ') {for (int i = 1;i<processcolor.length (); i++) {c = Processcolor.charat (i);
if (i<3) {r + = Rorgorb (c,i);
else if (i<5) {g = Rorgorb (c,i);
}else{B = Rorgorb (c,i);
} RGB = Color.rgb (R, G, b);
} this.processcolor = RGB;
private int Rorgorb (char c,int i) {num++;
Char b = 0; if (c>= ' 0 ' &&c<= ' 9 ') {//j is used to determine which is hexadecimal if (i==j) {//16 the right number of the second bit B + = Integer.valueo
F (c) *16;
} if (i==j+1) {//16 the right number of the first bit B + = integer.valueof (c); }//16 uppercase form if (c>= ' A ' &&c<= ' F ') {if (i==j) {//ascii code minus 55, such as A's ASCII code 65, minus 55 equals 1
0 B + + ((int) c-55) *16;
} if (i==j+1) {b + = (int) c-55; }///16 Lowercase alphabetic form if (c>= ' a ' &&c<= ' F ') {if (i==j) {//ascii code minus 87, such as A's ASCII code is 97, minus 87 is equal to the B + + ((int) c-87) *16;
} if (i==j+1) {b + = (int) c-87;
}///If the count is even, give J plus 2 if (num%2==0) {j=j+2;
return b;
//Get maximum Progress number public int Getmaxcount () {return maxcount;
//Set maximum progress number public void Setmaxcount (int maxcount) {this.maxcount = Maxcount;
//Get Progress public int getprocess () {return process;
//Set Progress public void setprocess (int process) {this.process = process;
//Set interval width public void setdividewidth (int dividewidth) {this.dividewidth = Dividewidth;
Public hezaiprogress (context, AttributeSet attrs, int defstyle) {Super (context, attrs, Defstyle); TODO auto-generated Constructor stub} public hezaiprogress (context context, AttributeSet Attrs) {This (Contex
T, attrs,0);
TODO auto-generated Constructor stub} public hezaiprogress (context context) {this (context,null); TODO auto-generated Constructor stub} @OvErride protected void OnDraw (Canvas Canvas) {//TODO auto-generated Method Stub Super.ondraw (Canvas);
Sethezai (canvas);
//Set progress bar height public void setprocessheight (int h) {if (h==0) this.h = 32;
else this.h = h;
//Set progress bar width public void setprocesswidth (int w) {if (w==0) THIS.W = 12;
else THIS.W = W;
} private void Sethezai (int process,canvas Canvas) {Rect Rect;
Paint Paint;
int tmp = 2;
Progress rounding, if the total width of the progress bar is greater than the specified control width, displays only the whole number of progress if (process* (w+tmp) +tmp>width) process = width/(w+dividewidth);
Show progress for (int i = 0;i<process;i++) {rect = new rect (tmp, 2,W+TMP,H);
Paint = new paint ();
Tmp=tmp+w+dividewidth;
Paint.setcolor (Processcolor);
Canvas.drawrect (rect, paint);
//Show Gray progress, and default no progress status for (int i = 0; i < maxcount-process; i++) {rect = new rect (tmp, 2,W+TMP,H);
Paint = new paint ();
Tmp=tmp+w+dividewidth; Paint.setcolor (Color.rgb (211, 211, 211));
Canvas.drawrect (rect, paint); }//DP convert to px private int diptopx (int dip) {Float scale = GetContext (). Getresources (). Getdisplaymetrics ().
Y return (int) (DIP * scale + 0.5f * (Dip >= 0 1:-1))/Plus 0.5 is for rounding} @Override protected void onmeasure (int wi Dthmeasurespec, int heightmeasurespec) {//TODO auto-generated Method Stub super.onmeasure (Widthmeasurespec, Heig
HTMEASURESPEC);
int widthspecmode = Measurespec.getmode (Widthmeasurespec);
int heightspecmode = Measurespec.getmode (Heightmeasurespec);
int widthspecsize = measurespec.getsize (Widthmeasurespec);
int heightspecsize = measurespec.getsize (Heightmeasurespec); measurespec.exactly, exact size if (Widthspecmode = = measurespec.exactly | | widthspecmode = = measurespec.at_most) {W
Idth = widthspecsize;
else {//If it is not an exact size, display the set size or the default size width = w; //measurespec.at_most, maximum size, as long as it does not exceed the maximum allowable size of the parent control, Measurespec.unspecifieD Unspecified size if (Heightspecmode = Measurespec.at_most | | heightspecmode = = measurespec.unspecified) {//Show set size or default size
Height = diptopx (h);
else {height = heightspecsize;
//Set the actual size of the control setmeasureddimension (width, height);
}
}
Detailed comments are made in the code, not here.
2.3 Processtest.java
Package com.example.progresstest;
Import android.app.Activity;
Import Android.os.Bundle;
public class Processtest extends activity {
private hezaiprogress process;
@Override
protected void onCreate (Bundle savedinstancestate) {
//TODO auto-generated method stub
Super.oncreate (savedinstancestate);
Setcontentview (r.layout.myprocesstest);
Process = (hezaiprogress) Findviewbyid (r.id.process);
Set the progress height
process.setprocessheight ();
Set the progress width
process.setprocesswidth (m);
Set the interval width
process.setdividewidth (ten);
Set Progress color
process.setprocesscolor ("#008b8b");
}
Here is better than the first method can change the height, width, spacing width, progress color, and so on, and only need a line of code, because the front has been encapsulated, but it seems to be more memory consumption.
Here is a change in color after the screenshot, is the first picture of this blog:
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.