怎么在android中自定義環(huán)形對(duì)比圖效果?針對(duì)這個(gè)問(wèn)題,這篇文章詳細(xì)介紹了相對(duì)應(yīng)的分析和解答,希望可以幫助更多想解決這個(gè)問(wèn)題的小伙伴找到更簡(jiǎn)單易行的方法。
創(chuàng)新互聯(lián)公司專注于企業(yè)全網(wǎng)營(yíng)銷推廣、網(wǎng)站重做改版、西吉網(wǎng)站定制設(shè)計(jì)、自適應(yīng)品牌網(wǎng)站建設(shè)、HTML5建站、商城建設(shè)、集團(tuán)公司官網(wǎng)建設(shè)、外貿(mào)網(wǎng)站制作、高端網(wǎng)站制作、響應(yīng)式網(wǎng)頁(yè)設(shè)計(jì)等建站業(yè)務(wù),價(jià)格優(yōu)惠性價(jià)比高,為西吉等各大城市提供網(wǎng)站開(kāi)發(fā)制作服務(wù)。
1.首先在res/values里創(chuàng)建一個(gè)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.然后為自定義對(duì)比圖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; /** * 弧線對(duì)比圖 * @param * @return * @author LH * @data 2016年1月25日 下午6:17:34 **/ public class AnswerChartView extends View { // 圓畫(huà)筆 private Paint mCirclePaint; // 圓環(huán)畫(huà)筆 private Paint mRingPaint; // 百分?jǐn)?shù)畫(huà)筆 private Paint mTextPaint; // 文本畫(huà)筆 private Paint mTextPaint2; // 里面圓顏色 private int mCircleColor; // 里面弧顏色 private int mInnerRingColor; // 外面弧顏色 private int mOutRingColor; // 空白的圓半徑 private float mRadius; // 里面的弧半徑 private float mRingRadius; // 最外弧半徑 private float mRingRadius2; // 圓環(huán)的寬度 private float mStrokeWidth; // 文本的中心x軸位置 private int mXCenter; // 文本的中心y軸位置 private int mYCenter; // 百分比文本的寬度 private float mTxtWidth; // 描述文本的寬度 private float mTxtWidth3; // 文本的高度 private float mTxtHeight; // 百分?jǐn)?shù)文本的大小 private float mTxtSize; // 總成績(jī) private int mTotalProgress = 100; // 個(gè)人的正確率 private double mInnerProgress; // 班級(jí)的正確率 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()); mTxtWidth3 = mTextPaint2.measureText(txt2, 0, txt2.length()); canvas.drawText(txt, mXCenter - mTxtWidth / 2, mYCenter+mTxtWidth / 8, mTextPaint); canvas.drawText(txt2 ,mXCenter - mTxtWidth3 / 2, mYCenter + mTxtWidth / 2, mTextPaint2); }else if(mInnerProgress==0){ String txt = mInnerProgress + "%"; String txt2 = "正確率"; mTxtWidth = mTextPaint.measureText(txt, 0, txt.length()); mTxtWidth3 = mTextPaint2.measureText(txt2, 0, txt2.length()); canvas.drawText(txt, mXCenter - mTxtWidth / 2, mYCenter+mTxtWidth / 8, mTextPaint); canvas.drawText(txt2 ,mXCenter - mTxtWidth3 / 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中設(shè)置內(nèi)環(huán)和外環(huán)的百分比和屬性
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;//個(gè)人的正確率 private double cAccuracy;//班級(jí)的正確率 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); } /** *進(jìn)度動(dòng)畫(huà)效果 * @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; } } }
關(guān)于怎么在android中自定義環(huán)形對(duì)比圖效果問(wèn)題的解答就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,如果你還有很多疑惑沒(méi)有解開(kāi),可以關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道了解更多相關(guān)知識(shí)。
分享標(biāo)題:怎么在android中自定義環(huán)形對(duì)比圖效果
本文來(lái)源:http://www.rwnh.cn/article6/gposig.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站導(dǎo)航、網(wǎng)頁(yè)設(shè)計(jì)公司、小程序開(kāi)發(fā)、網(wǎng)站內(nèi)鏈、搜索引擎優(yōu)化、品牌網(wǎng)站制作
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)