/**
*
*/
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 仿solo桌面記憶體清理效果
* @since 2014.7.30
* @version 1.0.0
* {@link }
public class progressrectangle extends view {
// sizes (with defaults)
private int layout_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 is the code that will increment the progress variable 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 attrs
public progressrectangle(context context, attributeset attrs) {
super(context, attrs);
* @param defstyleattr
public progressrectangle(context context, attributeset attrs,
int defstyleattr) {
super(context, attrs, defstyleattr);
@override
protected void onsizechanged(int w, int h, int oldw, int oldh) {
// 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() {
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() {
int width = getwidth(); // this.getlayoutparams().width;
log.i("width", width + "");
int height = getheight(); // this.getlayoutparams().height;
log.i("height", height + "");
rectbgbounds = new rectf(0, 0, width, height);
protected void ondraw(canvas canvas) {
super.ondraw(canvas);
canvas.drawrect(rectbgbounds, bgpaint);
log.i("progress", progress + "");
rectprogressbounds = new rectf(0, 0, progress, layout_height);
canvas.drawrect(rectprogressbounds, progresspaint);
canvas.drawtext("使用記憶體", 25, 25, titlepaint);
canvas.drawtext(progress + "m" + "/1024m", 25, 60, usepaint);
* increment the progress by 1 (of 100)
public void incrementprogress() {
isprogress = true;
progress++;
if (progress > 200)
progress = 100;
// settext(math.round(((float) progress / 360) * 100) + "%");
spinhandler.sendemptymessage(0);
public void unincrementprogress() {
progress--;
if (progress < 1)
* set the progress to a specific value
public void setprogress(int i) {
progress = i;
}
實作思路也是很簡單的,就是在ondraw()方法裡面繪制進度條的背景以及進度,進度的參數是傳遞進來的數值。activity的代碼如下:
import android.app.activity;
import android.os.bundle;
import android.view.view.onclicklistener;
import android.widget.button;
public class recactivity extends activity {
boolean running;
int progress = 0;
progressrectangle progressrectangle;
protected void oncreate(bundle savedinstancestate) {
super.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--;
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();
}
});
效果如下:
總體來說,就是通過繪制矩形來達到目的。當然,在實際使用中的效果還是有所差異的,歡迎大家回報、交流。
<--
csdn下載下傳:http://download.csdn.net/detail/wangjinyu501/7694607
gitub位址:https://github.com/wangjinyu501/progressrectangle
-->