栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 移动开发 > Android

Android自定义View实现加载进度条效果

Android 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Android自定义View实现加载进度条效果

上一篇文章总结了下自定义View的几个步骤,如果还有不清楚的同学可以先去看看Android自定义View(一) ,这篇文章和大家分享一下自定义加载进度条,效果如下

下面就以水平的进度条为列进行讲解:

1.首先还是在attrs.xml文件中自定义我们需要的属性:



 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

2.获取我们的自定义属性:


private int mTextSize;

private int mTextColor;

private int mStartColor;

private int mEndColor;

private int mProgressWidth;

private int mRadius;

private int mBgColor;

private float mCurrentProgress;

private int mLoadSpeed;

private String mContent="0%";
private Rect mBounds;
private Paint mPaint;

public GradientProgressBar(Context context) {
 this(context, null);
}

public GradientProgressBar(Context context, AttributeSet attrs) {
 this(context, attrs, 0);
}

public GradientProgressBar(Context context, AttributeSet attrs, int defStyleAttr) {
 super(context, attrs, defStyleAttr);
 TypedArray array = context.getTheme().obtainStyledAttributes(attrs, R.styleable.GradientProgressBar, defStyleAttr, 0);
 int count = array.getIndexCount();
 for (int i = 0; i < count; i++) {
 int index = array.getIndex(i);
 switch (index) {
 case R.styleable.GradientProgressBar_textSize:
 
 mTextSize = array.getDimensionPixelSize(index, (int) TypedValue.applyDimension(
  TypedValue.COMPLEX_UNIT_SP, 16, getResources().getDisplayMetrics()));
 break;
 case R.styleable.GradientProgressBar_textColor:
 
 mTextColor = array.getColor(index, Color.BLACK);
 break;
 case R.styleable.GradientProgressBar_startColor:
 mStartColor = array.getColor(index, Color.BLACK);
 break;
 case R.styleable.GradientProgressBar_endColor:
 mEndColor = array.getColor(index, Color.BLACK);
 break;
 case R.styleable.GradientProgressBar_bgColor:
 mBgColor = array.getColor(index, Color.BLACK);
 break;
 case R.styleable.GradientProgressBar_rectRadius:
 mRadius = array.getDimensionPixelSize(index, (int) TypedValue.applyDimension(
  TypedValue.COMPLEX_UNIT_DIP, 10, getResources().getDisplayMetrics()
 ));
 break;
 case R.styleable.GradientProgressBar_lineWidth:
 mProgressWidth=array.getDimensionPixelSize(index,(int)TypedValue.applyDimension(
  TypedValue.COMPLEX_UNIT_DIP,200,getResources().getDisplayMetrics()));
 break;
 case R.styleable.GradientProgressBar_loadSpeed:
 mLoadSpeed=array.getInt(index,10);
 break;
 }
 }
 array.recycle();
 init();
}

init()方法做如下操作

private void init(){
 mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
 mPaint.setAntiAlias(true);
 mBounds = new Rect();
 new Thread(new Runnable() {
 @Override
 public void run() {
 while (mCurrentProgress < mProgressWidth) {
 mCurrentProgress = mCurrentProgress + 1;
 mContent = Math.round((mCurrentProgress / mProgressWidth) * 100) + "%";
 try {
  postInvalidate();
  Thread.sleep(mLoadSpeed);
 } catch (Exception e) {
  e.printStackTrace();
 }
 }
 }
 }).start();
}

3.重写onDraw()方法

@Override
protected void onDraw(Canvas canvas) {
 super.onDraw(canvas);

 
 mPaint.setColor(mBgColor);
 mPaint.setStyle(Paint.Style.FILL);

 
 canvas.drawRoundRect(0, 0, mProgressWidth, getHeight(), mRadius, mRadius, mPaint);

 
 LinearGradient gradient = new LinearGradient(0, getHeight() / 2, mProgressWidth, getHeight() / 2,
 mStartColor, mEndColor, Shader.TileMode.MIRROR);
 mPaint.setShader(gradient);
 
 canvas.drawRoundRect(0, 0, mCurrentProgress, getHeight(), mRadius, mRadius, mPaint);

 mPaint.reset();
 mPaint.setAntiAlias(true);
 mPaint.setColor(mTextColor);
 mPaint.setTextSize(mTextSize);

 
 mPaint.getTextBounds(mContent, 0, mContent.length(), mBounds);
 canvas.drawText(mContent, getWidth() / 2 - mBounds.width() / 2, getHeight() / 2 + mBounds.height() / 2, mPaint);
}

 好了,这样就完成了我们水平渐变加载进度条,下面贴出圆形进度条的源码:

public class RoundProgressBar extends View {

 
 private int mTextSize;
 private int mTextColor;
 private int mCircleWidth;
 private int mBgColor;
 private int mCurrentColor;
 private int mLoadSpeed;
 private float mCurrentProgress;

 private String mContent = "0%";
 private Rect mBounds;
 private Paint mPaint;

 public RoundProgressBar(Context context) {
 this(context, null);
 }

 public RoundProgressBar(Context context, AttributeSet attrs) {
 this(context, attrs, 0);
 }

 public RoundProgressBar(Context context, AttributeSet attrs, int defStyleAttr) {
 super(context, attrs, defStyleAttr);
 TypedArray array = context.getTheme().obtainStyledAttributes(attrs, R.styleable.RoundProgressBar, defStyleAttr, 0);
 int count = array.getIndexCount();
 for (int i = 0; i < count; i++) {
 int index = array.getIndex(i);
 switch (index) {
 case R.styleable.RoundProgressBar_textSizeRound:
  
  mTextSize = array.getDimensionPixelSize(index, (int) TypedValue.applyDimension(
  TypedValue.COMPLEX_UNIT_SP, 16, getResources().getDisplayMetrics()));
  break;
 case R.styleable.RoundProgressBar_textColorRound:
  
  mTextColor = array.getColor(index, Color.BLACK);
  break;
 case R.styleable.RoundProgressBar_bgColorRound:
  mBgColor = array.getColor(index, Color.BLACK);
  break;
 case R.styleable.RoundProgressBar_circleWidthRound:
  mCircleWidth = array.getDimensionPixelSize(index, (int) TypedValue.applyDimension(
  TypedValue.COMPLEX_UNIT_DIP, 1, getResources().getDisplayMetrics()
  ));
  break;
 case R.styleable.RoundProgressBar_currentColorRound:
  mCurrentColor = array.getColor(index, Color.BLACK);
  break;
 case R.styleable.RoundProgressBar_loadSpeedRound:
  mLoadSpeed=array.getInt(index,10);
  break;
 }
 }
 array.recycle();
 init();
 }

 private void init() {
 mBounds = new Rect();
 mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
 mPaint.setAntiAlias(true);
 new Thread(new Runnable() {
 @Override
 public void run() {
 while (mCurrentProgress < 360) {
  mCurrentProgress = mCurrentProgress + 1;
  mContent = Math.round((mCurrentProgress / 360) * 100) + "%";
  postInvalidate();
  try {
  Thread.sleep(mLoadSpeed);
  } catch (Exception e) {
  e.printStackTrace();
  }
 }
 }
 }).start();
 }

 @Override
 protected void onDraw(Canvas canvas) {
 super.onDraw(canvas);

 
 mPaint.setColor(mBgColor);
 mPaint.setStyle(Paint.Style.STROKE);
 mPaint.setStrokeWidth(mCircleWidth);

 
 int xPoint = getWidth() / 2;//获取圆心x的坐标
 int radius = xPoint - mCircleWidth;//获取圆心的半径
 canvas.drawCircle(xPoint, xPoint, radius, mPaint);//用于定义的圆弧的形状和大小的界限

 
 mPaint.setColor(mCurrentColor);
 RectF oval = new RectF(xPoint - radius, xPoint - radius, radius + xPoint, radius + xPoint);
 canvas.drawArc(oval, -90, mCurrentProgress, false, mPaint);

 
 mPaint.reset();
 mPaint.setAntiAlias(true);
 mPaint.setColor(mTextColor);
 mPaint.setTextSize(mTextSize);
 mPaint.getTextBounds(mContent, 0, mContent.length(), mBounds);
 canvas.drawText(mContent, xPoint - mBounds.width() / 2, xPoint + mBounds.height() / 2, mPaint);
 }
}

4.在xml文件中申明我们的自定义View



 

 

好了,这样就完成了我们的水平加载进度条,和圆形加载进度条效果了,是不是感觉还可以啊。

源码下载地址

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/156171.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号