First, directly look at the effect
Second, directly on the code
1. Customizing the control Section
Package com.susan.project.myapplication;
Import android.app.Activity;
Import Android.content.Context;
Import Android.graphics.Bitmap;
Import Android.graphics.BitmapFactory;
Import Android.graphics.Canvas;
Import Android.graphics.Color;
Import android.graphics.LinearGradient;
Import Android.graphics.Paint;
Import Android.graphics.RectF;
Import Android.graphics.Shader;
Import Android.util.AttributeSet;
Import Android.util.DisplayMetrics;
Import Android.util.Log;
Import Android.view.View; /** * @author Dahai * @ClassName: ${type_name} * @Description: ${todo} * @date ${date} ${time} * @email 202491024@qq.com * @since $android Gradient progress bar/public class Progressseek extends View {/** * progress bar width */private int view_width;/** * Width of canvas *
/private int view_base_width;
/** * Control Width * * private int view_edge_width;
/** * Progress/private int progress;
Private Canvas Cachecanvas;
/** * Background color of the brush * * Private Paint backgroundpaint;
/** * The brush of the progress bar * * Private Paint progresspaint; /** * Progress end of the map * * Private Bitmap bitMap
private int bitmapwidth;
private int bitmapheight;
private context;
Gradients start private static final int default_start_color = Color.parsecolor ("#34DAB5");
Gradient end private static final int default_end_color = Color.parsecolor ("#27A5FE");
/** * Cache Picture * * Private Bitmap Cachebitmap; Public Progressseek (context) {super (context); Initview (context); AttributeSet attrs) {Super (context, attrs); Initview (context);} public Progressseek (context, AttributeSet attrs , int defstyleattr) {Super (context, attrs, defstyleattr); Initview (context);} private void Initview (context context) {T
His.context = context;
Bitmap = Bitmapfactory.decoderesource (Context.getresources (), r.mipmap.thumb);
Bitmapwidth = Bitmap.getwidth ();
Bitmapheight = Bitmap.getheight ();
Backgroundpaint = new Paint ();
Backgroundpaint.setstrokewidth (Bitmapwidth);
Backgroundpaint.setcolor (Color.parsecolor ("#cccccc"));
Backgroundpaint.setdither (TRUE); Backgroundpaint.setanTialias (TRUE);
Progresspaint = new Paint ();
Progresspaint.setstrokewidth (Bitmapwidth);
Progresspaint.setdither (TRUE);
Progresspaint.setantialias (TRUE);
Displaymetrics d = new Displaymetrics ();
(activity). Getwindowmanager (). Getdefaultdisplay (). Getmetrics (d);
View_base_width = D.widthpixels; public void init (int progress) {this.progress = progress; if (view_width = = 0) {//First up/* displaymetrics d = new Displa
Ymetrics ();
(activity). Getwindowmanager (). Getdefaultdisplay (). Getmetrics (d);
View_width = d.widthpixels*progress/100;*/View_width = view_base_width * PROGRESS/100; else {view_width = View_edge_width * progress/100} if (Cachebitmap!= null) {if (!cachebitmap.isrecycled ()) {Cach
Ebitmap.recycle ();
Cachebitmap = null;
} Cachecanvas = null;
} Cachebitmap = Bitmap.createbitmap (View_base_width, Bitmapheight * 2, Bitmap.Config.ARGB_8888); if (Cachecanvas = = null) {Cachecanvas = new Canvas (); Cachecanvas.setbitmap (Cachebitmap);}/** * Draw background/RECTF r = newRECTF ();
R.left = 0;
R.top = Bitmapheight;
R.right = View_base_width;
R.bottom = Bitmapwidth + 10;
Cachecanvas.drawroundrect (R, 5f, 5f, Backgroundpaint); if (Progress > 0) {lineargradient lg = new LinearGradient (0, 0, view_width, Bitmapwidth, Default_start_color, Default_
End_color, Shader.TileMode.CLAMP);
Progresspaint.setshader (LG);
RECTF r1 = new RECTF ();
R.left = 0;
R.top = Bitmapheight;
R.right = View_width;
R.bottom = Bitmapwidth + 10;
Cachecanvas.drawroundrect (R, 5f, 5f, Progresspaint);
Cachecanvas.drawbitmap (Bitmap, view_width-bitmapwidth+8, BITMAPHEIGHT/2 + 6, new Paint ());
} invalidate ();
} @Override protected void OnDraw (Canvas Canvas) {Super.ondraw (Canvas);
Paint bmppaint = new Paint (); Draw Cachebitmap to the View component if (Cachebitmap!= null) {Canvas.drawbitmap (cachebitmap, 0, 0, bmppaint);} view_edge_width = th
Is.getwidth ();
LOG.E ("Check out the width of the control:", View_edge_width + "");
Init (progress); }
}
3. layout file Section
<?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.susan.project.myapplication.progressseek android:id= "@+id/progress" android:layout_width= "Match_ Parent "android:layout_height=" 80DP "> </com.susan.project.myapplication.ProgressSeek> <
Com.susan.project.myapplication.ProgressSeek android:id= "@+id/progress1" android:layout_width= "Match_parent" android:layout_height= "80DP" > </com.susan.project.myapplication.ProgressSeek> <
Com.susan.project.myapplication.ProgressSeek android:id= "@+id/progress2" android:layout_width= "Match_parent" android:layout_height= "80DP" > </com.susan.project.myapplication.ProgressSeek> <
Com.susan.project.myapplication.ProgressSeek android:id= "@+id/progress3" android:layout_width= "Match_parent" android:layout_height= "80DP" > </com.susan.project.myApplication. Progressseek> </LinearLayout>
4.Activity part
Package com.susan.project.myapplication;
Import android.app.Activity;
Import Android.os.Bundle;
public class Mainactivity extends activity {
private progressseek progress;
Private Progressseek Progress1;
Private Progressseek Progress2;
Private Progressseek PROGRESS3;
@Override
protected void onCreate (Bundle savedinstancestate) {
super.oncreate (savedinstancestate);
Setcontentview (r.layout.activity_main);
Progress = (Progressseek) Findviewbyid (r.id.progress);
Progress.init (0);
Progress1 = (Progressseek) Findviewbyid (R.ID.PROGRESS1);
Progress1.init (2);
Progress2 = (Progressseek) Findviewbyid (R.ID.PROGRESS2);
Progress2.init (m);
PROGRESS3 = (Progressseek) Findviewbyid (R.ID.PROGRESS3);
Progress3.init (MB);
}
The above is a small set to introduce the Android custom with water droplets of the progress bar style (with gradient effect), I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!