一键清理是很多Launcher都会带有的功能,其效果也比较美观。实现方式也许有很多中,其中常见的是使用图片drawable来完成的,具体可以参考这篇文章: 模仿实现360桌面水晶球式的一键清理特效。本文另辟蹊径,使用自定义View来完成同样的效果,性能、效率更高。
ProgressWheel相信很多人并不陌生,我参考了其中一些代码。有意思的是,看完它的代码,发现其中隐藏了没有使用的矩形进度条,因为项目名字的原因我估计也永远不会出现了吧。所以就在其基础之上增增改改,形成了ProgressRectangle。为了节省时间,第一版本并没有使用自定义的属性,这个以后再添加吧,毕竟有些鸡肋。代码如下:
[html] view plain copy
- /**
- *
- */
- 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)
- layout_height = 0;
- layout_width = 0;
- // Colors (with defaults)
- bgColor = Color.TRANSPARENT;
- progressColor = 0xFF339933;
- // Paints
- progressPaint = new
- bgPaint = new
- titlePaint = new
- usePaint = new
- // Rectangles
- rectBgBounds = new
- rectProgressBounds = new
- progress = 100;
- boolean isProgress;
- spinHandler = new
- /**
- * 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 context
- * @param attrs
- */
- public ProgressRectangle(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(context, attrs, defStyleAttr);
- // TODO Auto-generated constructor stub
- }
- @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() {
- // 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
- width = getWidth(); // this.getLayoutParams().width;
- Log.i("width", width + "");
- height = getHeight(); // this.getLayoutParams().height;
- Log.i("height", height + "");
- rectBgBounds = new
- }
- @Override
- protected void onDraw(Canvas canvas) {
- // TODO Auto-generated method stub
- super.onDraw(canvas);
- canvas.drawRect(rectBgBounds, bgPaint);
- Log.i("progress", progress + "");
- rectProgressBounds = new
- 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++;
- >
- progress = 100;
- // setText(Math.round(((float) progress / 360) * 100) + "%");
- spinHandler.sendEmptyMessage(0);
- }
- /**
- * Increment the progress by 1 (of 100)
- */
- public void unIncrementProgress() {
- isProgress = true;
- 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);
- }
- }
实现思路也是很简单的,就是在onDraw()方法里面绘制进度条的背景以及进度,进度的参数是传递进来的数值。Activity的代码如下:
[html] view plain copy
- 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;
- progress = 0;
- ProgressRectangle progressRectangle;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- // TODO Auto-generated method stub
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_rec);
- progressRectangle=(ProgressRectangle) findViewById(R.id.progressBar);
- r = new
- public void run() {
- running = true;
- <100) {
- progressRectangle.incrementProgress();
- progress++;
- try {
- Thread.sleep(15);
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- >0) {
- progressRectangle.unIncrementProgress();
- progress--;
- try {
- Thread.sleep(15);
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- running = false;
- }
- };
- increment
- increment.setOnClickListener(new OnClickListener() {
- public void onClick(View v) {
- if(!running) {
- progress = 0;
- s = new
- s.start();
- }
- }
- });
- }
- }