Version: 1.0Date: 2014.7.292014.7.30Copyright: © kince Reproduced in the source
A key cleaning is a lot of launcher will have the function, and its effect is more beautiful. There may be a lot of implementations, one of the common is the use of picture drawable to complete, specifically, you can refer to this article: imitate the realization of 360 desktop crystal ball type of one-click cleaning effects. This article uses a custom view to accomplish the same effect, with higher performance and efficiency. Progresswheel believe that many people are not unfamiliar, I refer to some of the code. Interestingly, after reading its code, it is found that there is no use of the rectangular progress bar, because the project name of the reason I guess will never appear. Therefore, on the basis of the increase and modification, the formation of progressrectangle. In order to save time, the first version did not use the custom properties, this later add it, after all, some chicken. The code is as follows:
/*** */package com.kince.progressrectangle;import Android.content.context;import android.graphics.Canvas;import Android.graphics.color;import Android.graphics.paint;import Android.graphics.rectf;import Android.graphics.paint.style;import Android.os.handler;import Android.os.message;import Android.util.AttributeSet Import android.util.log;import android.view.view;/*** @author kince* @category imitation solo desktop Memory cleanup effect * @since 2014.7.30* @ Version 1.0.0* {@link}* */public class Progressrectangle extends View {//Sizes (with defaults) private int Layo ut_height = 0; private int layout_width = 0; Colors (with defaults) private int bgColor = color.transparent; private int progresscolor = 0xff339933; Paints Private Paint Progresspaint = new paint (); Private Paint Bgpaint = new paint (); Private Paint Titlepaint = new paint (); Private Paint Usepaint = new paint (); Rectangles private RECTF rectbgbounds = new RECTF (); Private RECTF RectprogresSbounds = new RECTF (); int progress = 100; Boolean isprogress; Private Handler Spinhandler = new Handler () {/** * This was the code that would increment the progress VA Riable and so * spin the wheel */@Override public void Handlemessage (Message msg) { Invalidate (); Super.handlemessage (msg); } }; /** * @param context */public progressrectangle (context context) {super (context); TODO auto-generated Constructor stub}/** * @param context * @param attrs */Public Progressre Ctangle (context context, AttributeSet Attrs) {Super (context, attrs); TODO auto-generated Constructor stub}/** * @param context * @param attrs * @param defstyleattr */Public Progressrectangle (context context, AttributeSet attrs, int defstyleattr) {Super (CO ntext, attrs, defstylEATTR); TODO auto-generated Constructor stub} @Override protected void onsizechanged (int w, int h, int oldw, int o LDH) {//TODO auto-generated Method Stub super.onsizechanged (W, H, OLDW, OLDH); Share the dimensions layout_width = w; LOG.I ("Layout_width", Layout_width + ""); Layout_height = h; LOG.I ("Layout_height", Layout_height + ""); Setupbounds (); Setuppaints (); Invalidate (); } private void Setuppaints () {//TODO auto-generated Method Stub Bgpaint.setcolor (BgColor); Bgpaint.setantialias (TRUE); Bgpaint.setstyle (Style.fill); Progresspaint.setcolor (Progresscolor); Progresspaint.setantialias (TRUE); Progresspaint.setstyle (Style.fill); Titlepaint.setcolor (Color.White); Titlepaint.settextsize (20); Titlepaint.setantialias (TRUE); Titlepaint.setstyle (Style.fill); Usepaint.setcolor (Color.White); Usepaint.setantialias (TRUE); Usepaint.settextsize (30); Usepaint.setstyle (Style.fill); } private void Setupbounds () {//TODO auto-generated method Stub int width = GetWidth ();//This.ge Tlayoutparams (). width; LOG.I ("width", Width + ""); int height = getheight (); This.getlayoutparams (). Height; LOG.I ("height", height + ""); Rectbgbounds = new RECTF (0, 0, width, height); } @Override protected void OnDraw (canvas canvas) {//TODO auto-generated method stub Super.ondr AW (canvas); Canvas.drawrect (Rectbgbounds, bgpaint); LOG.I ("Progress", Progress + ""); Rectprogressbounds = new RECTF (0, 0, Progress, layout_height); Canvas.drawrect (Rectprogressbounds, progresspaint); Canvas.drawtext ("Use memory", +, titlepaint); Canvas.drawtext (Progress + "M" + "/1024m", +,-usepaint); } /** * Increment the progress by 1 (OF) */public void incrementprogress () {isprogress = true; progress++; if (Progress > progress) = 100; SetText (Math.Round (((float) progress/360) * 100) + "%"); Spinhandler.sendemptymessage (0); }/** * Increment the progress by 1 (OF) */public void unincrementprogress () {isprogress = True progress--; if (Progress < 1) progress = 100; SetText (Math.Round (((float) progress/360) * 100) + "%"); Spinhandler.sendemptymessage (0); }/** * Set the progress to a specific value */public void setprogress (int i) {progress = i; Spinhandler.sendemptymessage (0); }} implementation of the idea is also very simple, that is, in the OnDraw () method to draw the background of the progress bar and progress, the parameters of the progress is passed in the value. The activity code is as follows:
Package Com.kince.progressrectangle;import Android.app.activity;import Android.os.bundle;import android.util.Log; Import Android.view.view;import Android.view.view.onclicklistener;import Android.widget.button;public class Recactivity extends Activity {Boolean running; int progress = 0; Progressrectangle Progressrectangle; @Override protected void OnCreate (Bundle savedinstancestate) {//TODO auto-generated method stub su Per.oncreate (savedinstancestate); Setcontentview (R.LAYOUT.ACTIVITY_REC); Progressrectangle= (Progressrectangle) Findviewbyid (R.id.progressbar); Final Runnable r = new Runnable () {public void run () {running = true; while (progress<100) {progressrectangle.incrementprogress (); progress++; try {Thread.sleeP (15); } catch (Interruptedexception e) {//TODO auto-generated catch block E.printstacktrace (); }} while (progress>0) {Progressrectangle. Unincrementprogress (); progress--; try {thread.sleep (15); } catch (Interruptedexception e) {//TODO auto-generated catch block E.printstacktrace (); }} running = false; } }; Button increment = (button) Findviewbyid (r.id.btn_increment); Increment.setonclicklistener (New Onclicklistener () {public void ONclick (View v) {if (!running) {progress = 0; Thread s = new Thread (r); S.start (); } } }); }}The effect is as follows:
In general, the goal is achieved by drawing a rectangle. Of course, in the actual use of the effect is still different, we welcome feedback, Exchange. <--csdn Download: http://download.csdn.net/detail/wangjinyu501/7694607 gitub Address: https://github.com/wangjinyu501/ Progressrectangle--