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

android自定义环形对比图效果

程序员文章站 2022-06-19 19:48:06
本文实例为大家分享了android自定义环形对比图的具体代码,供大家参考,具体内容如下 1.首先在res/values里创建一个attr.xml的文件。...

本文实例为大家分享了android自定义环形对比图的具体代码,供大家参考,具体内容如下

android自定义环形对比图效果

1.首先在res/values里创建一个attr.xml的文件。

<?xml version="1.0" encoding="utf-8"?>
<resources>
 
 <declare-styleable name="answerchartview">
  <attr name="radius" format="dimension"/>
  <attr name="strokewidth" format="dimension"/>
  <attr name="circlecolor" format="color"/>
  <attr name="innerringcolor" format="color"/>
  <attr name="outringcolor" format="color"/>
  <attr name="textsize" format="dimension"/>
 </declare-styleable>
 
</resources>

2.然后为自定义对比图view

package com.jsle.ebag.answer.view;
 
 
import com.jsle.ebag.answer.r;
 
 
import android.content.context;
import android.content.res.typedarray;
import android.graphics.canvas;
import android.graphics.paint;
import android.graphics.rectf;
import android.graphics.paint.fontmetrics;
import android.util.attributeset;
import android.view.view;
 
 
/**
 * 弧线对比图
 * @param
 * @return
 * @author lh
 * @data 2016年1月25日 下午6:17:34
 **/
public class answerchartview extends view {
 
 
 // 圆画笔
 private paint mcirclepaint;
 // 圆环画笔
 private paint mringpaint;
 // 百分数画笔
 private paint mtextpaint;
 // 文本画笔
 private paint mtextpaint2;
 // 里面圆颜色
 private int mcirclecolor;
 // 里面弧颜色
 private int minnerringcolor;
 // 外面弧颜色
 private int moutringcolor;
 // 空白的圆半径
 private float mradius;
 // 里面的弧半径
 private float mringradius;
 // 最外弧半径
 private float mringradius2;
 // 圆环的宽度
 private float mstrokewidth;
 // 文本的中心x轴位置
 private int mxcenter;
 // 文本的中心y轴位置
 private int mycenter;
 // 百分比文本的宽度
 private float mtxtwidth;
 // 描述文本的宽度
 private float mtxtwidth2;
 // 文本的高度
 private float mtxtheight;
 // 百分数文本的大小
 private float mtxtsize;
 // 总成绩
 private int mtotalprogress = 100;
 // 个人的正确率
 private double minnerprogress;
 // 班级的正确率
 private double moutprogress;
 public answerchartview(context context, attributeset attrs) {
 super(context, attrs);
 initattrs(context, attrs);
 initvariable();
 }
 
 
 private void initattrs(context context, attributeset attrs) {
 typedarray typearray = context.gettheme().obtainstyledattributes(attrs,
 r.styleable.answerchartview, 0, 0);
 mradius = typearray.getdimension(r.styleable.answerchartview_radius, 80);
 mtxtsize=typearray.getdimension(r.styleable.answerchartview_textsize, 20);
 mstrokewidth = typearray.getdimension(r.styleable.answerchartview_strokewidth, 10);
 mcirclecolor = typearray.getcolor(r.styleable.answerchartview_circlecolor, 0xffffffff);
 moutringcolor = typearray.getcolor(r.styleable.answerchartview_innerringcolor, 0xffffffff);
 minnerringcolor = typearray.getcolor(r.styleable.answerchartview_outringcolor, 0xffffffff);
 mringradius = mradius + mstrokewidth / 2;
 mringradius2 = mradius + mstrokewidth/2*3;
 }
 
 
 private void initvariable() {
 mcirclepaint = new paint();
 mcirclepaint.setantialias(true);
 mcirclepaint.setstyle(paint.style.fill);
 
 
 mringpaint = new paint();
 mringpaint.setantialias(true);
 mringpaint.setcolor(minnerringcolor);
 mringpaint.setstyle(paint.style.stroke);
 mringpaint.setstrokewidth(mstrokewidth);
 
 
 mtextpaint = new paint();
 mtextpaint.setantialias(true);
 mtextpaint.setstyle(paint.style.fill);
 mtextpaint.setargb(255, 32, 207, 152);
 mtextpaint.settextsize(mtxtsize);
 
 mtextpaint2 = new paint();
 mtextpaint2.setantialias(true);
 mtextpaint2.setstyle(paint.style.fill);
 mtextpaint2.setargb(255, 0, 0, 0);
 mtextpaint2.settextsize(20);
 fontmetrics fm = mtextpaint.getfontmetrics();
 mtxtheight = (int) math.ceil(fm.descent - fm.ascent);
 
 
 }
 
 
 @override
 protected void ondraw(canvas canvas) {
 
 
 mxcenter = getwidth() / 2;
 mycenter = getheight() / 2;
 
 
 mcirclepaint.setcolor(getresources().getcolor(r.color.gray));
 canvas.drawcircle(mxcenter,mycenter, mradius + mstrokewidth*2, mcirclepaint);
 
 rectf oval1 = new rectf();
 oval1.left = (mxcenter - mringradius);
 oval1.top = (mycenter - mringradius);
 oval1.right = mringradius * 2 + (mxcenter - mringradius);
 oval1.bottom = mringradius * 2 + (mycenter - mringradius);
 mringpaint.setcolor(moutringcolor);
 canvas.drawarc(oval1, -90, ((float)moutprogress / mtotalprogress) * 360, false, mringpaint);
 
 mcirclepaint.setcolor(mcirclecolor);
 canvas.drawcircle(mxcenter, mycenter, mradius, mcirclepaint);
 if (minnerprogress > 0 ) {
 rectf oval = new rectf();
 oval.left = (mxcenter - mringradius2);
 oval.top = (mycenter - mringradius2);
 oval.right = mringradius2 * 2 + (mxcenter - mringradius2);
 oval.bottom = mringradius2 * 2 + (mycenter - mringradius2);
 mringpaint.setcolor(minnerringcolor);
 canvas.drawarc(oval, -90, ((float)minnerprogress / mtotalprogress) * 360, false, mringpaint); //
 // canvas.drawcircle(mxcenter, mycenter, mradius + mstrokewidth / 2, mringpaint);
 string txt = minnerprogress + "%";
 string txt2 = "正确率";
 mtxtwidth = mtextpaint.measuretext(txt, 0, txt.length());
 mtxtwidth2 = mtextpaint2.measuretext(txt2, 0, txt2.length());
 canvas.drawtext(txt, mxcenter - mtxtwidth / 2, mycenter+mtxtwidth / 8, mtextpaint);
 canvas.drawtext(txt2 ,mxcenter - mtxtwidth2 / 2, mycenter + mtxtwidth / 2, mtextpaint2);
 }else if(minnerprogress==0){
 string txt = minnerprogress + "%";
 string txt2 = "正确率";
 mtxtwidth = mtextpaint.measuretext(txt, 0, txt.length());
 mtxtwidth2 = mtextpaint2.measuretext(txt2, 0, txt2.length());
 canvas.drawtext(txt, mxcenter - mtxtwidth / 2, mycenter+mtxtwidth / 8, mtextpaint);
 canvas.drawtext(txt2 ,mxcenter - mtxtwidth2 / 2, mycenter + mtxtwidth / 2, mtextpaint2);
 }
 }
 public void setoutprogress(double progress){
 moutprogress=progress;
 }
 public void setinnerprogress(double progress) {
 minnerprogress = progress;
 // invalidate();
 postinvalidate();
 }
 
 
}

3.使用自定义view

<com.jsle.ebag.answer.view.answerchartview
   android:id="@+id/tasks_view"
   android:layout_width="160dp"
   android:layout_height="160dp"
   android:layout_centerhorizontal="true"
   tc:circlecolor="@color/circle_color"
   tc:innerringcolor="@color/dark_yellow"
   tc:outringcolor="@color/green"
   tc:radius="60dip"
   tc:strokewidth="6dip"
   tc:textsize="32sp" />

4.最后可已在answerchartactivity中设置内环和外环的百分比和属性

package com.jsle.ebag.answer.activity;
 
import java.text.decimalformat;
 
import com.jsle.ebag.answer.r;
import com.jsle.ebag.answer.r.layout;
import com.jsle.ebag.answer.view.answerchartview;
 
import android.app.activity;
import android.content.intent;
import android.os.bundle;
import android.view.menu;
import android.view.menuitem;
import android.view.view;
import android.view.view.onclicklistener;
import android.widget.button;
import android.widget.linearlayout;
import android.widget.textview;
 
public class answerchartactivity extends baseactivity implements onclicklistener {
 private answerchartview mtasksview;
 private double maccuracy;//个人的正确率
 private double caccuracy;//班级的正确率
 private double mcurrentprogress;
 private linearlayout btn_black;
 private textview tv_title,tv_subjectcount,tv_submit,tv_accuracy;
 private string title;
 private double maccuracy,caccuracy;
 private int subjectcount,submit;
 @override
 protected int getid() {
 // todo auto-generated method stub
 return r.id.activity_id_answerchart;
 }
 @override
 protected string gettag() {
 // todo auto-generated method stub
 return "answerchart_acitivity";
 }
 @override
 protected void oncreate(bundle savedinstancestate) {
 super.oncreate(savedinstancestate);
 setcontentview(r.layout.activity_answer_chart);
 btn_black=(linearlayout) findviewbyid(r.id.btn_black);
 tv_title=(textview) findviewbyid(r.id.tv_title);
 tv_subjectcount=(textview) findviewbyid(r.id.tv_subjectcount);
 tv_submit=(textview) findviewbyid(r.id.tv_submit);
 tv_accuracy=(textview) findviewbyid(r.id.tv_accuracy);
 btn_black.setonclicklistener(this);
 getdata();
 initvariable();
 initview();
 new thread(new progressrunable()).start();
 }
 private void getdata() {
 // todo auto-generated method stub
 intent intent = getintent();
 title=intent.getstringextra("title");
 maccuracy = intent.getdoubleextra("maccuracy", 0);
 caccuracy = intent.getdoubleextra("caccuracy", 0);
 subjectcount=intent.getintextra("subjectcount", 0);
 submit=intent.getintextra("submit", 0);
 }
 private void initvariable() {
 tv_title.settext(title);
 tv_subjectcount.settext("共"+subjectcount+"道题");
 tv_submit.settext(submit+"");
 tv_accuracy.settext(caccuracy+"%");
 maccuracy =maccuracy;
 caccuracy=caccuracy;
 mcurrentprogress = 0;
 }
 private void initview() {
 mtasksview = (answerchartview) findviewbyid(r.id.tasks_view);
 mtasksview.setoutprogress(caccuracy);
 }
 /**
 *进度动画效果
 * @author lh
 * @data 2016年1月29日 下午3:43:31
 **/
 class progressrunable implements runnable {
 
 @override
 public void run() {
 while (mcurrentprogress < maccuracy) {
 mcurrentprogress += 1;
 if(mcurrentprogress>maccuracy){
  mcurrentprogress=maccuracy;
 }
 mtasksview.setinnerprogress(mcurrentprogress);
 try {
  thread.sleep(15);
 } catch (exception e) {
  e.printstacktrace();
 }
 }
 }
 
 }
 
 @override
 public void onclick(view v) {
 // todo auto-generated method stub
 switch (v.getid()) {
 case r.id.btn_black:
 finish();
 break;
 
 default:
 break;
 }
 }
}

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