当前位置: 移动技术网 > IT编程>移动开发>Android > Android打造炫酷进度条效果

Android打造炫酷进度条效果

2019年07月24日  | 移动技术网IT编程  | 我要评论

保定租房网,0xfe0816,洛克王国秋秋

本文实例为大家分享了android炫酷进度条效果的具体代码,供大家参考,具体内容如下

学习:

horizontalprogressbarwithprogress的代码

import android.content.context;
import android.content.res.typedarray;
import android.graphics.canvas;
import android.graphics.paint;
import android.os.build;
import android.support.annotation.requiresapi;
import android.util.attributeset;
import android.util.typedvalue;
import android.widget.progressbar;

import trunk.doi.base.r;

/**
 * 作者:mr.lee on 2017-10-17 15:51
 * 邮箱:569932357@qq.com
 */

public class horizontalprogressbarwithprogress extends progressbar{

 private static final int default_text_size=10;//sp
 private static final int default_text_color=0xfffc00d1;
 private static final int default_color_unreach=0xffd3d6da;
 private static final int default_height_unreach=2;//dp
 private static final int default_color_reach=default_text_color;
 private static final int default_height_reach=2;
 private static final int default_text_offset=10;

 protected int mtextsize=sp2px(default_text_size);
 protected int mtextcolor=default_text_color;
 protected int munreachcolor=default_color_unreach;
 protected int munreachheigh=dp2px(default_height_unreach);
 protected int mreachheigh=dp2px(default_height_reach);
 protected int mreachcolor=default_color_reach;
 protected int mtextoffset=dp2px(default_text_offset);

 protected paint mpaint=new paint();
 protected int mrealwidth;

 public horizontalprogressbarwithprogress(context context) {
  super(context);
  init(null);
 }

 public horizontalprogressbarwithprogress(context context, attributeset attrs) {
  super(context, attrs);
  init(attrs);

 }

 public horizontalprogressbarwithprogress(context context, attributeset attrs, int defstyleattr) {
  super(context, attrs, defstyleattr);
  init(attrs);
 }

 @requiresapi(api = build.version_codes.lollipop)
 public horizontalprogressbarwithprogress(context context, attributeset attrs, int defstyleattr, int defstyleres) {
  super(context, attrs, defstyleattr, defstyleres);
  init(attrs);
 }
 private void init(attributeset attrs) {


  /**
   * 获取dimension的方法有几种,区别不大
   * 共同点是都会将dp,sp的单位转为px,px单位的保持不变
   *
   * getdimension() 返回float,
   * getdimensionpixelsize 返回int 小数部分四舍五入
   * getdimensionpixeloffset 返回int,但是会抹去小数部分
   */
  typedarray array=getcontext().obtainstyledattributes(attrs, r.styleable.horizontalprogressbarwithprogress);

  mtextsize= (int) array.getdimension(r.styleable.horizontalprogressbarwithprogress_progress_text_size,mtextsize);
  mtextcolor=array.getcolor(r.styleable.horizontalprogressbarwithprogress_progress_text_color,mtextcolor);
  munreachcolor=array.getcolor(r.styleable.horizontalprogressbarwithprogress_progress_unreach_color,munreachcolor);
  munreachheigh=(int) array.getdimension(r.styleable.horizontalprogressbarwithprogress_progress_unreach_height,munreachheigh);
  mreachheigh=(int) array.getdimension(r.styleable.horizontalprogressbarwithprogress_progress_reach_height,mreachheigh);
  mtextoffset=(int) array.getdimension(r.styleable.horizontalprogressbarwithprogress_progress_text_offset,mtextoffset);
  mreachcolor=array.getcolor(r.styleable.horizontalprogressbarwithprogress_progress_reach_color,mreachcolor);

  array.recycle();

  mpaint.settextsize(mtextsize);

 }

 @override
 protected synchronized void onmeasure(int widthmeasurespec, int heightmeasurespec) {
//  int widthmode=measurespec.getmode(widthmeasurespec);
  int width=measurespec.getsize(widthmeasurespec);
  int heigh=measureheight(heightmeasurespec);
  setmeasureddimension(width,heigh);
  mrealwidth=getmeasuredwidth()-getpaddingleft()-getpaddingright();
 }



 private int measureheight(int heightmeasurespec) {

  int result=0;
  int mode=measurespec.getmode(heightmeasurespec);
  int size=measurespec.getsize(heightmeasurespec);

  if(mode==measurespec.exactly){
   result=size;
  }else{
   int textheigh= (int) (mpaint.descent()-mpaint.ascent());
   result=getpaddingtop()+getpaddingbottom()+math.max(math.max(mreachheigh,munreachheigh),math.abs(textheigh));
   if(mode==measurespec.at_most){
    result=math.min(result,size);
   }
  }
  return result;

 }

 @override
 protected synchronized void ondraw(canvas canvas) {


  canvas.save();
  canvas.translate(getpaddingleft(),getheight()/2);

  boolean noneedunreach=false;
  string text=getprogress()+"%";
  int textwidth= (int) mpaint.measuretext(text);
  float radio =getprogress()*1.0f/getmax();
  float progressx=radio*mrealwidth;
  if(progressx+textwidth>mrealwidth){
   progressx=mrealwidth-textwidth;
   noneedunreach=true;
  }

  float endx=progressx-mtextoffset/2;
  if(endx>0){
   mpaint.setcolor(mreachcolor);
   mpaint.setstrokewidth(mreachheigh);
   canvas.drawline(0,0,endx,0,mpaint);
  }

  //draw text
  mpaint.setcolor(mtextcolor);
  int y = (int) (-(mpaint.descent()+mpaint.ascent())/2);
  canvas.drawtext(text,progressx,y,mpaint);


  //draw unreach bar
  if(!noneedunreach){
   float startx=progressx+ mtextoffset/2+textwidth;
   mpaint.setcolor(munreachcolor);
   mpaint.setstrokewidth(munreachheigh);
   canvas.drawline(startx,0,mrealwidth,0,mpaint);
  }
  canvas.restore();


 }

 protected int dp2px(int dpval){
  return (int) typedvalue.applydimension(typedvalue.complex_unit_dip,dpval,getresources().getdisplaymetrics());
 }
 protected int sp2px(int spval){
  return (int) typedvalue.applydimension(typedvalue.complex_unit_sp,spval,getresources().getdisplaymetrics());
 }

}

roundprogressbarwithprogress的代码

import android.content.context;
import android.content.res.typedarray;
import android.graphics.canvas;
import android.graphics.paint;
import android.graphics.rectf;
import android.os.build;
import android.support.annotation.requiresapi;
import android.util.attributeset;

import trunk.doi.base.r;

/**
 * 作者:mr.lee on 2017-10-18 10:48
 * 邮箱:569932357@qq.com
 */

public class roundprogressbarwithprogress extends horizontalprogressbarwithprogress {


 private int mradius=dp2px(30);
 private int mmaxpaintwidth;

 public roundprogressbarwithprogress(context context) {
  super(context);
  init(null);
 }

 public roundprogressbarwithprogress(context context, attributeset attrs) {
  super(context, attrs);
  init(attrs);
 }

 public roundprogressbarwithprogress(context context, attributeset attrs, int defstyleattr) {
  super(context, attrs, defstyleattr);
  init(attrs);
 }

 @requiresapi(api = build.version_codes.lollipop)
 public roundprogressbarwithprogress(context context, attributeset attrs, int defstyleattr, int defstyleres) {
  super(context, attrs, defstyleattr, defstyleres);
  init(attrs);
 }


 private void init(attributeset attrs){

  mreachheigh= (int) (munreachheigh*2.5f);

  typedarray array=getcontext().obtainstyledattributes(attrs, r.styleable.roundprogressbarwithprogress);
  mradius=(int) array.getdimension(r.styleable.roundprogressbarwithprogress_radius,mradius);
  array.recycle();

  mpaint.setstyle(paint.style.stroke);
  mpaint.setantialias(true);
  mpaint.setdither(true);
  mpaint.setstrokecap(paint.cap.round);

 }

 @override
 protected synchronized void onmeasure(int widthmeasurespec, int heightmeasurespec) {

  mmaxpaintwidth=math.max(mreachheigh,munreachheigh);
  //默认4个padding一致
  int except=mradius*2+mmaxpaintwidth+getpaddingleft()+getpaddingright();
  int width=resolvesize(except,widthmeasurespec);
  int height=resolvesize(except,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.ascent()+mpaint.descent())/2;

  canvas.save();
  canvas.translate(getpaddingleft(),getpaddingtop());

  mpaint.setstyle(paint.style.stroke);
  // draw unreachbar
  mpaint.setcolor(munreachcolor);
  mpaint.setstrokewidth(munreachheigh);
  canvas.drawcircle(mradius,mradius,mradius,mpaint);
  //draw reach bar
  mpaint.setcolor(mreachcolor);
  mpaint.setstrokewidth(mreachheigh);
  float sweepangle=getprogress()*1.0f/getmax()*360;
  canvas.drawarc(new rectf(0,0,mradius*2,mradius*2),0,sweepangle,false,mpaint);
  //draw text
  mpaint.setcolor(mtextcolor);
  mpaint.setstyle(paint.style.fill);
  canvas.drawtext(text,mradius-textwidth/2,mradius-textheight,mpaint);

  canvas.restore();

 }
}

activity_view_mv代码

<?xml version="1.0" encoding="utf-8"?>
<relativelayout
  xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:app="http://schemas.android.com/apk/res-auto"
 android:id="@+id/rl_view"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
 >


  <trunk.doi.base.ui.activity.test.horizontalprogressbarwithprogress
   android:id="@+id/progress_bar"
   style="?android:attr/progressbarstylehorizontal"
   android:layout_margintop="50dp"
   android:padding="5dp"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:max="100"
   android:progress="50"
   app:progress_unreach_color="@color/pink"
   app:progress_text_color="@color/yellow"
   app:progress_reach_color="@color/red"
   />

  <android.support.v7.widget.appcompatseekbar
   android:id="@+id/seekbar"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:padding="5dp"
   android:layout_margintop="100dp"
   />

  <trunk.doi.base.ui.activity.test.roundprogressbarwithprogress
   android:id="@+id/progress_bar2"
   style="?android:attr/progressbarstylehorizontal"
   android:layout_margintop="150dp"
   android:padding="5dp"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:max="100"
   android:progress="0"
   app:progress_unreach_color="@color/pink"
   app:progress_text_color="@color/yellow"
   app:progress_reach_color="@color/red"
   app:progress_reach_height="3dp"
   app:progress_unreach_height="1dp"
   app:radius="200dp"
   />

</relativelayout>

viewmvactivity代码

import android.os.bundle;
import android.support.annotation.nullable;
import android.support.v7.widget.appcompatseekbar;
import android.view.motionevent;
import android.view.view;
import android.widget.relativelayout;
import android.widget.seekbar;

import butterknife.bindview;
import trunk.doi.base.r;
import trunk.doi.base.base.baseactivity;

public class viewmvactivity extends baseactivity {

 //没有集成butterknife的findviewbyid()

 @bindview(r.id.progress_bar)
 horizontalprogressbarwithprogress progress_bar;
 @bindview(r.id.progress_bar2)
 roundprogressbarwithprogress progress_bar2;
 @bindview(r.id.seekbar)
 appcompatseekbar seekbar;

 private float mtouchstarty;
 private static final float touch_move_max_y=600;

 @override
 protected int initlayoutid() {
  return r.layout.activity_view_mv;
 }

 @override
 protected void initview(@nullable bundle savedinstancestate) {

 }

 @override
 protected void setlistener() {

  seekbar.setonseekbarchangelistener(new seekbar.onseekbarchangelistener() {
   @override
   public void onprogresschanged(seekbar seekbar, int progress, boolean fromuser) {
    progress_bar.setprogress(progress);
    progress_bar2.setprogress(progress);
   }

   @override
   public void onstarttrackingtouch(seekbar seekbar) {

   }

   @override
   public void onstoptrackingtouch(seekbar seekbar) {

   }
  });


 }

 @override
 protected void initdata() {

 }


}

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

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网