Android-two methods for customizing load progress, android-Load

Source: Internet
Author: User

Android-two methods for customizing load progress, android-Load

Blog Author: It 1zhai male
Reprinted please indicate address: http://www.cnblogs.com/ityizhainan/p/5914487.html

This article uses two methods to describe the progress of similar vehicle loads.

First

 

1. Add the for loop using the addview method of LinearLayout

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:orientation="horizontal" >    <LinearLayout         android:id="@+id/ll"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:orientation="horizontal">            </LinearLayout></LinearLayout>

1.2 sublayout view01.xml of LinearLayout

<?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:orientation="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 the green image with progress (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);} // fill in the gray image 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 two boundary image elements are involved.

The final result is as follows:

This method has both advantages and disadvantages. This method is easy to understand, consumes less memory, and runs fast. However, if you want to use different colors, you must cut images of different sizes and colors, with spacing between the left and right, it is troublesome. One of the following methods is the custom control method, which is good or bad.

2 How 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:orientation="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>

Com. example. progresstest is the package name of HezaiProgress, 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. linearLay Out. 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, the input is a color string in the form of "# ffffff" * @ param processColor */publi C 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 (cha R c, int I) {num ++; char B = 0; if (c> = '0' & c <= '9 ') {// j is used to determine which is in hexadecimal format if (I = j) {// the second digit of the right number in hexadecimal format B ++ = Integer. valueOf (c) * 16;} if (I = j + 1) {// The first B + = Integer in the hexadecimal system. valueOf (c) ;}// hexadecimal uppercase if (c >='a' & c <= 'F ') {if (I = j) {// ascii code minus 55, for example, the ASCII code of A is 65, after 55 is equal to 10 B + = (int) c-55) * 16;} if (I = j + 1) {B + = (int) c-55 ;}} // The lowercase letter in hexadecimal format if (c> = 'A' & c <= 'F') {if (I = j) {// ascii code minus 87, for example, the ASCII code of a is 97, after which 87 is equal to 10 B + = (in T) c-87) * 16;} if (I = j + 1) {B + = (int) c-87;} // if the count is an even number, then add 2 if (num % 2 = 0) {j = j + 2;} return B;} // get the maximum progress count public int getMaxCount () {return maxCount;} // set the maximum number of progress public void setMaxCount (int maxCount) {this. maxCount = maxCount;} // get the progress public int getProcess () {return process;} // set the progress public void setProcess (int process) {this. process = process;} // set the interval width public void setDivideWidth (int divideWi Dth) {this. divideWidth = divideWidth;} public HezaiProgress (Context context, AttributeSet attrs, int defStyle) {super (context, attrs, defStyle ); // TODO Auto-generated constructor stub} public HezaiProgress (Context context, AttributeSet attrs) {this (context, attrs, 0 ); // TODO Auto-generated constructor stub} public HezaiProgress (Context context) {this (context, null); // TODO Auto-generated co Nstructor stub} @ Override protected void onDraw (Canvas canvas) {// TODO Auto-generated method stub super. onDraw (canvas); setHezai (16, canvas);} // set the height of the progress bar public void setProcessHeight (int h) {if (h = 0) this. h = 32; else this. h = h;} // set the 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 is rounded up. if the total width of the progress bar is greater than the width of the specified control, only the integer progress if (process * (w + tmp) + tmp> width) is displayed) process = width/(w + divideWidth); // display the 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);} // displays the gray progress and the default 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 converts to px private int dipToPx (int dip) {float scale = getContext (). getResources (). getDisplayMetrics (). density; return (int) (dip * scale + 0.5f * (dip> = 0? 1:-1); // Add 0.5 to rounding} @ Override protected void onMeasure (int widthMeasureSpec, int heightMeasureSpec) {// TODO Auto-generated method stub super. onMeasure (widthMeasureSpec, heightMeasureSpec); 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) {width = widthSpecSize;} else {// if it is not an exact size, the set size or default size width = w;} // MeasureSpec is displayed. AT_MOST, maximum size, as long as it does not exceed the maximum size allowed by the parent control, MeasureSpec. UNSPECIFIED size if (heightSpecMode = MeasureSpec. AT_MOST | heightSpecMode = MeasureSpec. UNSPECIFIED) {// display set size or default size height = dipToPx (h);} else {height = heightSpecSize;} // set the actual size of the control setMeasuredDimension (width, height );}}

I made a detailed comment in the code, so I won't talk about it 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 (63); // sets the progress width of process. setProcessWidth (25); // set the interval width of process. setDivideWidth (10); // set the progress color process. setProcessColor ("# 008b8b ");}}

This is better than the first method. You can change the height, width, interval width, and progress color at will. You only need a line of code, because the preceding code has been encapsulated, but it seems memory consumption.

Here is the first image of this blog after the color is changed:

The Source Code address is enough. The steps are detailed and related icons are also provided.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.