欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

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

程序员文章站 2024-02-28 23:24:10
进度框 import android.content.context; import android.graphics.canvas; import an...

进度框

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


环形进度条

先来看一下效果:

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

下面直接上代码了:

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); 
 } 
 
 /** 
  * 计算并设置最适合的textsize 
  * 
  * @param paint 
  * @param max 最大宽度 
  * @param offset 偏移 
  */ 
 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()即可。