android 简单的缓冲进度条

来源:互联网 发布:美国知乎quora 编辑:程序博客网 时间:2024/05/21 22:39
<pre name="code" class="java">package com.xxx.xxx.x;
import android.content.Context;import android.graphics.Canvas;import android.graphics.Paint;import android.graphics.Rect;import android.os.Handler;import android.os.SystemClock;import android.util.AttributeSet;import android.view.View;import android.view.animation.AlphaAnimation;import android.view.animation.Animation;public class WaitProgressBar extends View {private int progress;private int MAX = 1000;private boolean isRunning = true;private Paint mPaint = new Paint();private Rect rect = new Rect();private Handler mHandler = new Handler();@Overrideprotected void onDraw(Canvas canvas) {rect.set(0, 0, getWidth() * progress / MAX, getHeight());canvas.drawRect(rect, mPaint);}public <span style="font-family: Arial, Helvetica, sans-serif;">WaitProgressBar </span>(Context context, AttributeSet attrs) {super(context, attrs);mPaint.setColor(Color.RED);}//// ========================public==============================//public void stop() {isRunning = false;new Thread() {public void run() {while (progress <= 1000) {progress += 6;SystemClock.sleep(1);postInvalidate();}SystemClock.sleep(300);mHandler.post(new Runnable() {public void run() {AlphaAnimation aa = new AlphaAnimation(1f, 0f);aa.setDuration(300);aa.setAnimationListener(new SimpleAnimationListener() {public void onAnimationEnd(Animation animation) {setVisibility(GONE);}});startAnimation(aa);}});};}.start();}public void start() {isRunning = true;progress = 0;new Thread() {public void run() {while (isRunning) {if (progress < 851)progress++;SystemClock.sleep(6);postInvalidate();}};}.start();}}



使用直接调用 start() 和 stop() 方法就行




1 0