Android 进度条

来源:互联网 发布:物理软件高中 编辑:程序博客网 时间:2024/05/22 15:52
// Handler handler = new Handler() {
// //接收消息,用于更新UI界面
// @Override
// public void handleMessage(Message msg) {
// super.handleMessage(msg);
// int i = msg.what;
// tv_main_text.setText(i + "");
// }
// };
//
// class MyThread extends Thread {
// @Override
// public void run() {
// super.run();
// for (int i = 0; i <= 100; i++) {
// pb_progress_bar.setProgress(i);
// //在子线程中发送消息
// handler.sendEmptyMessage(i);
// try {
// Thread.sleep(100);
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
// }
// }
// }

intpro=0;
Runnablerunnable=newRunnable() {
public voidrun() {
if(flagPicture==true) {
// pb_progress_bar.setVisibility(View.VISIBLE);
// pro = pb_progress_bar.getProgress() + progress1 + progress2 + progress3 + progress4;
// pb_progress_bar.setProgress(pro);
if(progress1>0&&progress2==0&&progress3==0&&progress4==0) {
pro=pb_progress_bar.getProgress() +progress1;
pb_progress_bar.setProgress(100*progress1/progress);
}else if(progress2>0&&progress1>0&&progress3==0&&progress4==0) {
pro=progress1+progress2;
pb_progress_bar.setProgress(100* (progress1+progress2) /progress);
}else if(progress2>0&&progress1>0&&progress3>0&&progress4==0) {
pro=progress1+progress2+progress3;
pb_progress_bar.setProgress(100* (progress1+progress2+progress3) /progress);
}else if(progress2>0&&progress1>0&&progress3>0&&progress4>0) {
pro=progress1+progress2+progress3+progress4;
pb_progress_bar.setProgress(100* (progress1+progress2+progress3+progress4) /progress);
}
//如果进度小于100,,则延迟1000毫秒后重复执行runnable
if(pro<progress) {
handler.postDelayed(runnable,3000);
}else{
newHandler().postDelayed(newRunnable() {
@Override
public voidrun() {
if(num>= -13&&num<=13&&num!=0) {
if(flagDelay==true) {
AppContext.showToast("保存成功!");
Intent intent = newIntent();
intent.putExtra("longitude",longitude);
intent.putExtra("latitude",latitude);
// getActivity().setResult(123, intent);
callBackValue.sendLonLatValue(longitude,latitude);
getActivity().finish();
flagPicture=false;
flagDelay=false;
}
}
}
},4000);//延时1s执行

}
}
}
};
handler.post(runnable); //开始执行



mHandler.sendEmptyMessage(MSG_PROGRESS_UPDATE);
privateHandlermHandler=newHandler() {
public voidhandleMessage(android.os.Message msg) {
if(flagPicture==true) {
rl_scroll.setEnabled(false);
ly_progress.setEnabled(false);
ly_progress.setClickable(false);
intprogressRun =pb_progress_bar.getProgress();
intpro =0;
pro =progress1+progress2+progress3+progress4;
pb_progress_bar.setProgress(++progressRun);
if(progressRun >=100) {
mHandler.removeMessages(MSG_PROGRESS_UPDATE);
if(pro >=progress) {
if(num>= -13&&num<=13&&num!=0) {
if(flagDelay==true) {
AppContext.showToast("保存成功!");
Intent intent = newIntent();
intent.putExtra("longitude",longitude);
intent.putExtra("latitude",latitude);
// getActivity().setResult(123, intent);
callBackValue.sendLonLatValue(longitude,latitude);
getActivity().finish();
flagPicture=false;
flagDelay=false;
}
}
}
}
mHandler.sendEmptyMessageDelayed(MSG_PROGRESS_UPDATE,10);
}
}
};

Xml文件
<com.suncreate.afMapPointInfo.RoundProgressBarWidthNumber    android:id="@+id/pb_progress_bar"    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:layout_centerInParent="true"    android:padding="5dp"    zhy:progress_reached_bar_height="20dp"    zhy:progress_text_color="#ffF53B03"    zhy:progress_unreached_color="#00F7C6B7" />

java代码 圆形进度条
package com.suncreate.afMapPointInfo;import android.content.Context;import android.content.res.TypedArray;import android.graphics.Canvas;import android.graphics.Paint.Cap;import android.graphics.Paint.Style;import android.graphics.RectF;import android.util.AttributeSet;import com.suncreate.afMapPointInfo.R;public class RoundProgressBarWidthNumber extends      HorizontalProgressBarWithNumber{   /**    * mRadius of view    */   private int mRadius = dp2px(30);   private int mMaxPaintWidth;   public RoundProgressBarWidthNumber(Context context)   {      this(context, null);   }   public RoundProgressBarWidthNumber(Context context, AttributeSet attrs)   {      super(context, attrs);      mReachedProgressBarHeight = (int) (mUnReachedProgressBarHeight * 2.5f);      TypedArray ta = context.obtainStyledAttributes(attrs,            R.styleable.RoundProgressBarWidthNumber);      mRadius = (int) ta.getDimension(            R.styleable.RoundProgressBarWidthNumber_radius, mRadius);      ta.recycle();      mPaint.setStyle(Style.STROKE);      mPaint.setAntiAlias(true);      mPaint.setDither(true);      mPaint.setStrokeCap(Cap.ROUND);   }   /**    * 这里默认在布局中padding值要么不设置,要么全部设置    */   @Override   protected synchronized void onMeasure(int widthMeasureSpec,         int heightMeasureSpec)   {      mMaxPaintWidth = Math.max(mReachedProgressBarHeight,            mUnReachedProgressBarHeight);      int expect = mRadius * 2 + mMaxPaintWidth + getPaddingLeft()            + getPaddingRight();      int width = resolveSize(expect, widthMeasureSpec);      int height = resolveSize(expect, heightMeasureSpec);      int realWidth = Math.min(width, height);      mRadius = (realWidth - getPaddingLeft() - getPaddingRight() - mMaxPaintWidth) / 2;      setMeasuredDimension(realWidth, realWidth);   }   @Override   protected synchronized void onDraw(Canvas canvas)   {      String text = getProgress() + "%";      float textWidth = mPaint.measureText(text);      float textHeight = (mPaint.descent() + mPaint.ascent()) / 2;      canvas.save();      canvas.translate(getPaddingLeft() + mMaxPaintWidth / 2, getPaddingTop()            + mMaxPaintWidth / 2);      mPaint.setStyle(Style.STROKE);      // draw unreaded bar      mPaint.setColor(mUnReachedBarColor);      mPaint.setStrokeWidth(mUnReachedProgressBarHeight);      canvas.drawCircle(mRadius, mRadius, mRadius, mPaint);      // draw reached bar      mPaint.setColor(mReachedBarColor);      mPaint.setStrokeWidth(mReachedProgressBarHeight);      float sweepAngle = getProgress() * 1.0f / getMax() * 360;      canvas.drawArc(new RectF(0, 0, mRadius * 2, mRadius * 2), 0,            sweepAngle, false, mPaint);      // draw text      mPaint.setStyle(Style.FILL);      canvas.drawText(text, mRadius - textWidth / 2, mRadius - textHeight,            mPaint);      canvas.restore();   }}

package com.suncreate.afMapPointInfo;import android.content.Context;import android.content.res.TypedArray;import android.graphics.Canvas;import android.graphics.Paint.Cap;import android.graphics.Paint.Style;import android.graphics.RectF;import android.util.AttributeSet;import com.suncreate.afMapPointInfo.R;public class RoundProgressBarWidthNumber extends      HorizontalProgressBarWithNumber{   /**    * mRadius of view    */   private int mRadius = dp2px(30);   private int mMaxPaintWidth;   public RoundProgressBarWidthNumber(Context context)   {      this(context, null);   }   public RoundProgressBarWidthNumber(Context context, AttributeSet attrs)   {      super(context, attrs);      mReachedProgressBarHeight = (int) (mUnReachedProgressBarHeight * 2.5f);      TypedArray ta = context.obtainStyledAttributes(attrs,            R.styleable.RoundProgressBarWidthNumber);      mRadius = (int) ta.getDimension(            R.styleable.RoundProgressBarWidthNumber_radius, mRadius);      ta.recycle();      mPaint.setStyle(Style.STROKE);      mPaint.setAntiAlias(true);      mPaint.setDither(true);      mPaint.setStrokeCap(Cap.ROUND);   }   /**    * 这里默认在布局中padding值要么不设置,要么全部设置    */   @Override   protected synchronized void onMeasure(int widthMeasureSpec,         int heightMeasureSpec)   {      mMaxPaintWidth = Math.max(mReachedProgressBarHeight,            mUnReachedProgressBarHeight);      int expect = mRadius * 2 + mMaxPaintWidth + getPaddingLeft()            + getPaddingRight();      int width = resolveSize(expect, widthMeasureSpec);      int height = resolveSize(expect, heightMeasureSpec);      int realWidth = Math.min(width, height);      mRadius = (realWidth - getPaddingLeft() - getPaddingRight() - mMaxPaintWidth) / 2;      setMeasuredDimension(realWidth, realWidth);   }   @Override   protected synchronized void onDraw(Canvas canvas)   {      String text = getProgress() + "%";      float textWidth = mPaint.measureText(text);      float textHeight = (mPaint.descent() + mPaint.ascent()) / 2;      canvas.save();      canvas.translate(getPaddingLeft() + mMaxPaintWidth / 2, getPaddingTop()            + mMaxPaintWidth / 2);      mPaint.setStyle(Style.STROKE);      // draw unreaded bar      mPaint.setColor(mUnReachedBarColor);      mPaint.setStrokeWidth(mUnReachedProgressBarHeight);      canvas.drawCircle(mRadius, mRadius, mRadius, mPaint);      // draw reached bar      mPaint.setColor(mReachedBarColor);      mPaint.setStrokeWidth(mReachedProgressBarHeight);      float sweepAngle = getProgress() * 1.0f / getMax() * 360;      canvas.drawArc(new RectF(0, 0, mRadius * 2, mRadius * 2), 0,            sweepAngle, false, mPaint);      // draw text      mPaint.setStyle(Style.FILL);      canvas.drawText(text, mRadius - textWidth / 2, mRadius - textHeight,            mPaint);      canvas.restore();   }}
java代码 水平进度条
package com.suncreate.afMapPointInfo;import android.content.Context;import android.content.res.TypedArray;import android.graphics.Canvas;import android.graphics.Paint;import android.util.AttributeSet;import android.util.Log;import android.util.TypedValue;import android.widget.ProgressBar;import com.suncreate.afMapPointInfo.R;public class HorizontalProgressBarWithNumber extends ProgressBar{   private static final int DEFAULT_TEXT_SIZE = 10;   private static final int DEFAULT_TEXT_COLOR = 0XFFFC00D1;   private static final int DEFAULT_COLOR_UNREACHED_COLOR = 0xFFd3d6da;   private static final int DEFAULT_HEIGHT_REACHED_PROGRESS_BAR = 2;   private static final int DEFAULT_HEIGHT_UNREACHED_PROGRESS_BAR = 2;   private static final int DEFAULT_SIZE_TEXT_OFFSET = 10;   /**    * painter of all drawing things    */   protected Paint mPaint = new Paint();   /**    * color of progress number    */   protected int mTextColor = DEFAULT_TEXT_COLOR;   /**    * size of text (sp)    */   protected int mTextSize = sp2px(DEFAULT_TEXT_SIZE);   /**    * offset of draw progress    */   protected int mTextOffset = dp2px(DEFAULT_SIZE_TEXT_OFFSET);   /**    * height of reached progress bar    */   protected int mReachedProgressBarHeight = dp2px(DEFAULT_HEIGHT_REACHED_PROGRESS_BAR);   /**    * color of reached bar    */   protected int mReachedBarColor = DEFAULT_TEXT_COLOR;   /**    * color of unreached bar    */   protected int mUnReachedBarColor = DEFAULT_COLOR_UNREACHED_COLOR;   /**    * height of unreached progress bar    */   protected int mUnReachedProgressBarHeight = dp2px(DEFAULT_HEIGHT_UNREACHED_PROGRESS_BAR);   /**    * view width except padding    */   protected int mRealWidth;   protected boolean mIfDrawText = true;   protected static final int VISIBLE = 0;   public HorizontalProgressBarWithNumber(Context context, AttributeSet attrs)   {      this(context, attrs, 0);   }   public HorizontalProgressBarWithNumber(Context context, AttributeSet attrs,         int defStyle)   {      super(context, attrs, defStyle);      obtainStyledAttributes(attrs);      mPaint.setTextSize(mTextSize);      mPaint.setColor(mTextColor);   }   @Override   protected synchronized void onMeasure(int widthMeasureSpec,         int heightMeasureSpec)   {      int width = MeasureSpec.getSize(widthMeasureSpec);      int height = measureHeight(heightMeasureSpec);      setMeasuredDimension(width, height);      mRealWidth = getMeasuredWidth() - getPaddingRight() - getPaddingLeft();   }   private int measureHeight(int measureSpec)   {      int result = 0;      int specMode = MeasureSpec.getMode(measureSpec);      int specSize = MeasureSpec.getSize(measureSpec);      if (specMode == MeasureSpec.EXACTLY)      {         result = specSize;      } else      {         float textHeight = (mPaint.descent() - mPaint.ascent());         result = (int) (getPaddingTop() + getPaddingBottom() + Math.max(               Math.max(mReachedProgressBarHeight,                     mUnReachedProgressBarHeight), Math.abs(textHeight)));         if (specMode == MeasureSpec.AT_MOST)         {            result = Math.min(result, specSize);         }      }      return result;   }   /**    * get the styled attributes    *     * @param attrs    */   private void obtainStyledAttributes(AttributeSet attrs)   {      // init values from custom attributes      final TypedArray attributes = getContext().obtainStyledAttributes(            attrs, R.styleable.HorizontalProgressBarWithNumber);      mTextColor = attributes            .getColor(                  R.styleable.HorizontalProgressBarWithNumber_progress_text_color,                  DEFAULT_TEXT_COLOR);      mTextSize = (int) attributes.getDimension(            R.styleable.HorizontalProgressBarWithNumber_progress_text_size,            mTextSize);      mReachedBarColor = attributes            .getColor(                  R.styleable.HorizontalProgressBarWithNumber_progress_reached_color,                  mTextColor);      mUnReachedBarColor = attributes            .getColor(                  R.styleable.HorizontalProgressBarWithNumber_progress_unreached_color,                  DEFAULT_COLOR_UNREACHED_COLOR);      mReachedProgressBarHeight = (int) attributes            .getDimension(                  R.styleable.HorizontalProgressBarWithNumber_progress_reached_bar_height,                  mReachedProgressBarHeight);      mUnReachedProgressBarHeight = (int) attributes            .getDimension(                  R.styleable.HorizontalProgressBarWithNumber_progress_unreached_bar_height,                  mUnReachedProgressBarHeight);      mTextOffset = (int) attributes            .getDimension(                  R.styleable.HorizontalProgressBarWithNumber_progress_text_offset,                  mTextOffset);      int textVisible = attributes            .getInt(R.styleable.HorizontalProgressBarWithNumber_progress_text_visibility,                  VISIBLE);      if (textVisible != VISIBLE)      {         mIfDrawText = false;      }      attributes.recycle();   }   @Override   protected synchronized void onDraw(Canvas canvas)   {      canvas.save();      canvas.translate(getPaddingLeft(), getHeight() / 2);      boolean noNeedBg = false;      float radio = getProgress() * 1.0f / getMax();      float progressPosX = (int) (mRealWidth * radio);      String text = getProgress() + "%";      // mPaint.getTextBounds(text, 0, text.length(), mTextBound);      float textWidth = mPaint.measureText(text);      float textHeight = (mPaint.descent() + mPaint.ascent()) / 2;      if (progressPosX + textWidth > mRealWidth)      {         progressPosX = mRealWidth - textWidth;         noNeedBg = true;      }      // draw reached bar      float endX = progressPosX - mTextOffset / 2;      if (endX > 0)      {         mPaint.setColor(mReachedBarColor);         mPaint.setStrokeWidth(mReachedProgressBarHeight);         canvas.drawLine(0, 0, endX, 0, mPaint);      }      // draw progress bar      // measure text bound      if (mIfDrawText)      {         mPaint.setColor(mTextColor);         canvas.drawText(text, progressPosX, -textHeight, mPaint);      }      // draw unreached bar      if (!noNeedBg)      {         float start = progressPosX + mTextOffset / 2 + textWidth;         mPaint.setColor(mUnReachedBarColor);         mPaint.setStrokeWidth(mUnReachedProgressBarHeight);         canvas.drawLine(start, 0, mRealWidth, 0, mPaint);      }      canvas.restore();   }   /**    * dp 2 px    *     * @param dpVal    */   protected int dp2px(int dpVal)   {      return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,            dpVal, getResources().getDisplayMetrics());   }   /**    * sp 2 px    *     * @param spVal    * @return    */   protected int sp2px(int spVal)   {      return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,            spVal, getResources().getDisplayMetrics());   }}