Android Custom progress bar Gradient Color View instance code _android

Source: Internet
Author: User

Recently in the company, the project is not very busy, and occasionally see a brother in Csdn help, help to a custom gradient progress bar, I looked at the progress bar, feel pretty, try to customize view to achieve a, nonsense do not say, first on the map!

This custom view, completely out of Android's own Progressview, and does not use a picture, so that it can better reduce the coupling of the program code!

Below I paste out the code, probably explained the realization thought!

Copy Code code as follows:

Package Com.spring.progressview;

Import Android.content.Context;
Import Android.graphics.Canvas;
Import Android.graphics.Color;
Import android.graphics.LinearGradient;
Import Android.graphics.Matrix;
Import Android.graphics.Paint;
Import Android.graphics.RectF;
Import Android.graphics.Shader;
Import Android.util.AttributeSet;
Import Android.view.View;

/***
* Custom Progress bar
* @author Spring Sky
* email:vipa1888@163.com
* Date Created: 2014-1-6 3:28:51
*/
public class Springprogressview extends View {

/** subsection Color * *
private static final int[] Section_colors = {color.green,color.yellow,color.red};
/** progress bar Maximum Value * *
private float Maxcount;
/** progress bar Current Value * *
private float Currentcount;
/** Brush * *
Private Paint Mpaint;
private int mwidth,mheight;

Public Springprogressview, AttributeSet Attrs,
int defstyleattr) {
Super (context, attrs, defstyleattr);
Initview (context);
}

Public Springprogressview (context context, AttributeSet Attrs) {
Super (context, attrs);
Initview (context);
}

Public Springprogressview {
Super (context);
Initview (context);
}

private void Initview (context context) {
}

@Override
protected void OnDraw (Canvas Canvas) {
Super.ondraw (canvas);
Mpaint = new Paint ();
Mpaint.setantialias (TRUE);
int round = MHEIGHT/2;
System.out.println ("max=" +maxcount + "current=" +currentcount);
Mpaint.setcolor (COLOR.RGB (71, 76, 80));
RECTF RECTBG = new RECTF (0, 0, mwidth, mheight);
Canvas.drawroundrect (RECTBG, round, round, mpaint);
Mpaint.setcolor (Color.Black);
RECTF RECTBLACKBG = new RECTF (2, 2, mWidth-2, mHeight-2);
Canvas.drawroundrect (RECTBLACKBG, round, round, mpaint);

float section = Currentcount/maxcount;
RECTF RECTPROGRESSBG = new RECTF (3, 3, (mWidth-3) *section, mHeight-3);
if (section <= 1.0f/3.0f) {
if (section!= 0.0f) {
Mpaint.setcolor (Section_colors[0]);
}else{
Mpaint.setcolor (color.transparent);
}
}else{
int count = (section <= 1.0f/3.0f*2)? 2:3;
int[] colors = new Int[count];
System.arraycopy (section_colors, 0, COLORS, 0, Count);
float[] positions = new Float[count];
if (count = = 2) {
Positions[0] = 0.0f;
POSITIONS[1] = 1.0f-positions[0];
}else{
Positions[0] = 0.0f;
POSITIONS[1] = (MAXCOUNT/3)/currentcount;
POSITIONS[2] = 1.0f-positions[0]*2;
}
Positions[positions.length-1] = 1.0f;
LinearGradient shader = new LinearGradient (3, 3, (mWidth-3) *section, mHeight-3, Colors,null, Shader.TileMode.MIRROR);
Mpaint.setshader (shader);
}
Canvas.drawroundrect (RECTPROGRESSBG, round, round, mpaint);
}

private int diptopx (int dip) {
Float scale = GetContext (). Getresources (). Getdisplaymetrics (). density;
return (int) (DIP * scale + 0.5f * (Dip >= 0? 1:-1));
}

/***
* Set the maximum progress value
* @param maxcount
*/
public void Setmaxcount (float maxcount) {
This.maxcount = Maxcount;
}

/***
* Set the current progress value
* @param currentcount
*/
public void Setcurrentcount (float currentcount) {
This.currentcount = currentcount > Maxcount? Maxcount:currentcount;
Invalidate ();
}

public float Getmaxcount () {
return maxcount;
}

public float Getcurrentcount () {
return currentcount;
}

@Override
protected void onmeasure (int widthmeasurespec, int heightmeasurespec) {
int widthspecmode = Measurespec.getmode (Widthmeasurespec);
int widthspecsize = measurespec.getsize (Widthmeasurespec);
int heightspecmode = Measurespec.getmode (Heightmeasurespec);
int heightspecsize = measurespec.getsize (Heightmeasurespec);
if (Widthspecmode = = measurespec.exactly | | widthspecmode = = measurespec.at_most) {
Mwidth = widthspecsize;
} else {
mwidth = 0;
}
if (Heightspecmode = = Measurespec.at_most | | heightspecmode = = measurespec.unspecified) {
Mheight = DIPTOPX (15);
} else {
Mheight = heightspecsize;
}
Setmeasureddimension (Mwidth, mheight);
}


}


The code above is the entire core code for the control.
Specific ideas:
1. Progress bar, in fact, is a maximum and minimum value of the proportional value, this ratio is the current value/maximum value;
2. Custom fillet problem, as long as it is used to the canvar of the Drawroundrect;
3. Fade: LinearGradient object Rendering, the proportion of specific rendering to their own calculation, the current my program to provide 3 color rendering, the specific rules are:
(1) When the progress bar occupies a maximum of one-third below, it provides a color
(2) When the maximum value of more than one-third words, to distinguish whether more than two-thirds, if more than three, otherwise use two colors, because three of the total progress of the color of the One-third, this is a junior high school data problems, their own slowly drawing!
4. How to put the progress bar on the top of a rounded background, this is to draw two rounded rectangles: the first as a background, the second as a progress bar of the entity, the specific second progress of the entity accounted for how long, is the current currentcount/maxcount* the length of the custom view;


The other, no technical difficulties, do this custom control, the most important thing is that the custom should be based on the people's effect map, to see the realization of ideas, specific code resume in the idea, otherwise will only be armchair! If you do not understand, you should draw more drawings, concrete step-by-step calculation, everlasting, you can "practice" out!

Below provides a demo download address: Http://xiazai.jb51.net/201401/yuanma/SpringProgressDemo (jb51.net). zip

Related Article

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.