文章详情

短信预约-IT技能 免费直播动态提醒

请输入下面的图形验证码

提交验证

短信预约提醒成功

Android中制作进度框和环形进度条的简单实例分享

2022-06-06 08:44

关注

进度框


import android.content.Context; 
import android.graphics.Canvas; 
import android.graphics.Color; 
import android.graphics.Paint; 
import android.util.AttributeSet; 
import android.view.View; 
import java.util.Random; 
public class ObliqueProgressbar extends View { 
  private Paint mPaint; 
  private float mProgress; 
  public ObliqueProgressbar(Context context) { 
    this(context, null); 
  } 
  public ObliqueProgressbar(Context context, AttributeSet attrs) { 
    this(context, attrs, 0); 
  } 
  public ObliqueProgressbar(Context context, AttributeSet attrs, int defStyleAttr) { 
    super(context, attrs, defStyleAttr); 
    mPaint = new Paint(); 
    mPaint.setAntiAlias(true); 
  } 
  @Override 
  protected void onDraw(Canvas canvas) { 
    super.onDraw(canvas); 
    if (mProgress == 0) return; 
    //碎片雨 
    mPaint.setColor(Color.parseColor("#a96ecb")); 
    mPaint.setStrokeWidth(3); 
    Random random = new Random(); 
    int sx, sy; 
    for (int i = 0; i < 200; i++) { 
      sx = random.nextInt(getWidth() + 10); 
      sy = random.nextInt(getHeight() + 10); 
//      canvas.drawLine(sx, sy, sx+random.nextInt(5), sy+random.nextInt(5), mPaint); 
      canvas.drawCircle(sx, sy, random.nextInt(5) + 1, mPaint); 
    } 
    //进度 
    mPaint.setColor(Color.parseColor("#6AFFFFFF")); 
    mPaint.setStrokeWidth(15); 
    float x = mProgress * getWidth(); 
    for (int i = 0; i < x; i += 30) { 
      canvas.drawLine(i - 30, -10, i + 30, getHeight() + 10, mPaint); 
    } 
  } 
  public void setProgress(float progress) { 
    this.mProgress = progress; 
    invalidate(); 
  } 
} 

环形进度条

先来看一下效果:

下面直接上代码了:


ckage com.stone.circleprogressbar.view; 
import android.content.Context; 
import android.content.res.TypedArray; 
import android.graphics.Bitmap; 
import android.graphics.Canvas; 
import android.graphics.Color; 
import android.graphics.LinearGradient; 
import android.graphics.Paint; 
import android.graphics.RectF; 
import android.graphics.Shader; 
import android.util.AttributeSet; 
import android.view.View; 
import com.stone.circleprogressbar.R; 
public class CircleProgressbar extends View { 
 private float mProgress; 
 private int mBarColor; 
 private int mTextColor; 
 private float mTextSize; 
 public CircleProgressbar(Context context) { 
  this(context, null); 
 } 
 public CircleProgressbar(Context context, AttributeSet attrs) { 
  this(context, attrs, 0); 
 } 
 public CircleProgressbar(Context context, AttributeSet attrs, int defStyleAttr) { 
  super(context, attrs, defStyleAttr); 
  TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.CircleProgressbar); 
  mBarColor = array.getColor(R.styleable.CircleProgressbar_barColor, Color.GRAY); 
  mTextColor = array.getColor(R.styleable.CircleProgressbar_textColor, Color.GRAY); 
  mProgress = array.getFloat(R.styleable.CircleProgressbar_progress, 0); 
 } 
 @Override 
 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
  super.onMeasure(widthMeasureSpec, heightMeasureSpec); 
 } 
 public void setProgress(float count) { 
  mProgress = count; 
  invalidate(); 
 } 
 @Override 
 protected void onDraw(Canvas canvas) { 
  int w = getWidth(); 
  int h = getHeight(); 
  int strokeWidth = 20; 
  int radius = w / 2 - strokeWidth / 2;//大圆 半径 
  Bitmap barBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888); 
  Paint paint = new Paint(); 
  paint.setAntiAlias(true); //抗锯齿 
  paint.setStrokeWidth(strokeWidth); //描边宽 
  paint.setDither(true); //防抖动 
   
  Canvas circleCanvas = new Canvas(barBitmap); 
  RectF rect = new RectF(0, 0, w, h); 
  paint.setColor(Color.argb(0x11, 0xcc, 0xcc, 0xcc)); 
//  circleCanvas.drawRect(rect, paint); //没啥用 只是看外边框的 
   
  paint.setColor(Color.CYAN); 
  paint.setShader(new LinearGradient(0, 0, w, h, Color.RED, Color.GREEN, Shader.TileMode.CLAMP)); 
  circleCanvas.drawCircle(w / 2, h / 2, radius - strokeWidth / 2, paint); 
  paint.setShader(null); 
   
  paint.setColor(mBarColor); 
  paint.setStyle(Paint.Style.STROKE); 
  circleCanvas.drawCircle(w / 2, h / 2, radius, paint); 
   
  paint.setShader(new LinearGradient(0, 0, w, h, 
    new int[]{Color.GREEN, Color.MAGENTA, Color.CYAN, Color.RED}, 
    new float[]{0.2f, 0.5f, 0.7f, 1.0f}, Shader.TileMode.CLAMP)); 
  float cur = mProgress * 360 * 0.01f; 
  circleCanvas.drawArc(new RectF(strokeWidth / 2, strokeWidth / 2, w - strokeWidth / 2, h - strokeWidth / 2), 
    360 - 45, cur, false, paint); 
  paint.setShader(null); 
   
  paint.setColor(mTextColor); 
  if (mTextSize == 0) { 
   calcTextSize(paint, w, strokeWidth); 
  } else { 
   paint.setTextSize(mTextSize); 
  } 
  paint.setTextAlign(Paint.Align.LEFT);//default 
  String percent = mProgress + "%"; 
  paint.setStyle(Paint.Style.FILL); 
  circleCanvas.drawText(percent, w / 2 - paint.measureText(percent) / 2, h / 2 + paint.getTextSize() / 2, paint); 
  canvas.drawBitmap(barBitmap, 0, 0, paint); 
 } 
  
 private void calcTextSize(Paint paint, int max, int offset) { 
  float width = paint.measureText("99.99%"); 
  while (width < max * 3 / 5) { 
   paint.setTextSize(paint.getTextSize() + 5); 
   width = paint.measureText("92.88%") + offset / 2; 
  } 
  mTextSize = paint.getTextSize(); 
 } 
} 

设置进度刷新显示  调用  setProgress()即可。

您可能感兴趣的文章:Android实现环形进度条Android自定义环形LoadingView效果Android自定义View实现环形进度条的思路与实例Android实现计步进度的环形ProgressAndroid实现环形进度条的实例Android实现环形进度条代码Android应用中炫酷的横向和环形进度条的实例分享Android环形进度条(安卓默认形式)实例代码android自定义环形对比图效果


阅读原文内容投诉

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

软考中级精品资料免费领

  • 历年真题答案解析
  • 备考技巧名师总结
  • 高频考点精准押题
  • 2024年上半年信息系统项目管理师第二批次真题及答案解析(完整版)

    难度     807人已做
    查看
  • 【考后总结】2024年5月26日信息系统项目管理师第2批次考情分析

    难度     351人已做
    查看
  • 【考后总结】2024年5月25日信息系统项目管理师第1批次考情分析

    难度     314人已做
    查看
  • 2024年上半年软考高项第一、二批次真题考点汇总(完整版)

    难度     433人已做
    查看
  • 2024年上半年系统架构设计师考试综合知识真题

    难度     221人已做
    查看

相关文章

发现更多好内容

猜你喜欢

AI推送时光机
位置:首页-资讯-移动开发
咦!没有更多了?去看看其它编程学习网 内容吧
首页课程
资料下载
问答资讯