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

Android 仿支付宝中的余额宝收益进度条

程序员文章站 2024-02-26 10:27:46
一、 看效果 二、上代码 package com.framework.widget; import android.app.activity; impo...

一、 看效果

Android 仿支付宝中的余额宝收益进度条

二、上代码

package com.framework.widget;
import android.app.activity;
import android.content.context;
import android.content.res.typedarray;
import android.graphics.canvas;
import android.graphics.color;
import android.graphics.paint;
import android.graphics.rect;
import android.graphics.rectf;
import android.util.attributeset;
import android.util.displaymetrics;
import android.view.view;
import com.r;
/**
 * @author dahai
 * @classname: (仿支付宝) 收益进度条
 * @description: ${todo}
 * @date ${date} ${time}
 * @email 202491024@qq.com
 * @since $android
进度条

 */
public class profitprogerssbar extends view {
 //背景色
 private static final int default_back_color = color.parsecolor("#ffffff");
 //字的颜色
 private static final int default_text_color = color.parsecolor("#ffffff");
 //进度条背景颜色
 private static final int default_progress_color = color.parsecolor("#abacaf");
 //进度条默认的高度
 private static final float default_progress_height =120f;
 //文字的大小
 private static final float default_text_size = 50;
 /**
  * 收益进度条左右两边margin大小
  */
 private static final int margin_size = 20;
 private context context;
 /**
  * 背景颜色的画笔
  */
 private paint backgroundpaint;
 /**
  * 收益进度颜色的画笔
  */
 private paint progresspaint;
 /**
  * 画文字的画笔
  */
 private paint textpaint;
 /**
  * 背景的宽度
  */
 private int view_background_width;
 /**
  * 背景的高度
  */
 private float view_background_height = default_progress_height;
 /**
  * 日期
  */
 private string date = "2016/12/07";
 /**
  * 描叙(百分比/元)
  */
 private string desc = "2.1234";
 /**
  * 要显示的长度的百分比
  */
 private int progress = 70;
 //进度条颜色
 private int progress_color = default_progress_color;
 //背景色
 private int progress_back_color = default_back_color;
 //字的颜色
 private int text_color = default_text_color;
 //字的大小
 private float text_size = default_text_size;
 public profitprogerssbar(context context) {
  super(context);
  initview(context);
 }
 public profitprogerssbar(context context, attributeset attrs) {
  super(context, attrs);
  initview(context);
 }
 public profitprogerssbar(context context, attributeset attrs, int defstyleattr) {
  super(context, attrs, defstyleattr);
  initview(context);
 }
 private void initview(context context) {
  this.context = context;
  typedarray typedarray = this.context.obtainstyledattributes(r.styleable.profitprogerssbar);
  progress_back_color = typedarray.getcolor(r.styleable.profitprogerssbar_progress_backg_color,default_back_color);
  text_color = typedarray.getcolor(r.styleable.profitprogerssbar_progress_text_color,default_text_color);
  text_size = typedarray.getdimension(r.styleable.profitprogerssbar_progress_text_size,default_text_size);
  backgroundpaint = new paint();
  backgroundpaint.setstrokewidth(10);
  backgroundpaint.setcolor(progress_back_color);
  backgroundpaint.setdither(true);
  backgroundpaint.setantialias(true);
  progresspaint = new paint();
  progresspaint.setstrokewidth(10);
  progresspaint.setdither(true);
  progresspaint.setantialias(true);
  textpaint = new paint();
  textpaint.setstrokewidth(10);
  textpaint.setdither(true);
  textpaint.setantialias(true);
  textpaint.settextsize(text_size);
  displaymetrics d = new displaymetrics();
  ((activity) context).getwindowmanager().getdefaultdisplay().getmetrics(d);
  view_background_width = d.widthpixels;
 }
 /**
  * 初始化 进度条
  * @param date
  * @param desc
  * @param progress
  * @param progresscolor
  */
 public void init(string date,string desc,int progress,int progresscolor){
  this.date = date;
  this.desc = desc;
  this.progress = progress;
  this.progress_color = progresscolor;
 }
 @override
 protected void ondraw(canvas canvas) {
  super.ondraw(canvas);
  view_background_height = this.getmeasuredheight();
  rectf r = new rectf();
  r.left = 0;
  r.top = 0;
  r.right = view_background_width;
  r.bottom = view_background_height;////------------------------
  canvas.drawrect(r, backgroundpaint);
  rectf r1 = new rectf();
  r1.left = 0;
  r1.top = 0;
  r1.right = view_background_width * progress / 100;
  r1.bottom = view_background_height;////------------------------
  progresspaint.setcolor(progress_color);
  canvas.drawrect(r1, progresspaint);
  textpaint.setcolor(text_color);
  rect r2 = new rect();
  textpaint.gettextbounds(date,0,date.length(),r2);
  canvas.drawtext(date, margin_size, (view_background_height-r2.top)/2, textpaint);//日期
  rect r3 = new rect();
  textpaint.gettextbounds(desc,0,desc.length(),r3);
  if(progress>95&&progress<100){
   canvas.drawtext(desc, r1.right-textpaint.measuretext(desc)-margin_size-30,(view_background_height-r3.top)/2, textpaint);
  }else if(progress>=100) {
   canvas.drawtext(desc, r1.right-textpaint.measuretext(desc)-margin_size-45,(view_background_height-r3.top)/2, textpaint);
  }else {
   canvas.drawtext(desc, r1.right - textpaint.measuretext(desc) - margin_size, (view_background_height - r3.top) / 2, textpaint);
  }
  invalidate();
 }
}

三、

<com.framework.widget.profitprogerssbar
 android:layout_width="match_parent"
 android:layout_height="35dp"
 android:layout_marginleft="10dp"
 android:layout_marginright="10dp"
 android:layout_margintop="10dp"
 app:progress_back_color="@color/white"
 app:progress_text_color="@color/white"
 app:progress_text_size="14dp"
 android:id="@+id/profitprogerssbar"
 />
<declare-styleable name="profitprogerssbar">
 <attr name="progress_backg_color" format="color"/>
 <attr name="progress_text_color" format="color"/>
 <attr name="progress_text_size" format="dimension"/>
</declare-styleable>

以上所述是小编给大家介绍的android 仿支付宝中的余额宝收益进度条,希望对大家有所帮助