android自定义环形对比图效果
作者:第4个苹果 时间:2023-06-15 19:49:19
本文实例为大家分享了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;
}
}
}
来源:https://blog.csdn.net/u012483116/article/details/50635268
标签:android,对比图
0
投稿
猜你喜欢
Android编程自定义对话框(Dialog)位置及大小的方法
2021-10-01 19:01:51
C#中ListView控件实现窗体代码
2023-02-04 02:55:06
WinForm相对路径的陷阱
2022-01-20 10:25:18
如何搭建新的WPF项目框架
2023-09-28 08:18:05
java http token请求代码实例
2022-09-28 18:23:19
Android手机卫士之获取联系人信息显示与回显
2021-08-03 21:33:07
android实现文件读写功能
2022-03-10 16:31:23
如何安装java的运行环境IDEA
2022-09-20 10:42:38
C#实现DVD借出归还管理系统
2021-07-02 06:55:38
Android使用VideoView播放本地视频和网络视频的方法
2023-06-28 11:17:32
Java虚拟机内存结构及编码实战分享
2023-11-29 13:47:47
android文件上传示例分享(android图片上传)
2022-10-07 21:14:11
利用java实现二叉搜索树
2023-10-25 13:25:07
C#接口(Interface)用法分析
2022-10-18 10:59:17
Java SpringBoot实现AOP
2023-05-31 05:49:30
Mybatis plus通用字段自动填充的示例
2022-10-27 02:05:27
SpringMVC实现表单验证功能详解
2023-09-24 09:07:28
基于Java代码实现支付充值的通用流程
2021-09-04 00:48:45
Netty分布式pipeline管道传播outBound事件源码解析
2022-10-17 23:43:06
Java数据结构之线索化二叉树的实现
2022-03-16 15:25:34