android 一键清理动画,Android 一键清理动画片

/**

*

*/

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 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

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);

}

@Override

protected void onDraw(Canvas canvas) {

// TODO Auto-generated method stub

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);

}

/**

* Increment the progress by 1 (of 100)

*/

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);

}

}

实现思路也是很简单的,就是在onDraw()方法里面绘制进度条的背景以及进度,进度的参数是传递进来的数值。Activity的代码如下: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

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--;

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();

}

}

});

}

}  效果如下:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值