Android studio中怎么實(shí)現(xiàn)一個(gè)圓形進(jìn)度條,相信很多沒(méi)有經(jīng)驗(yàn)的人對(duì)此束手無(wú)策,為此本文總結(jié)了問(wèn)題出現(xiàn)的原因和解決方法,通過(guò)這篇文章希望你能解決這個(gè)問(wèn)題。
目前成都創(chuàng)新互聯(lián)公司已為成百上千的企業(yè)提供了網(wǎng)站建設(shè)、域名、虛擬主機(jī)、網(wǎng)站托管、企業(yè)網(wǎng)站設(shè)計(jì)、寧陜網(wǎng)站維護(hù)等服務(wù),公司將堅(jiān)持客戶導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長(zhǎng),共同發(fā)展。
MainActivity
import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; public class MainActivity extends AppCompatActivity implements View.OnClickListener{ private Gua mGua1; private Button play; private Button resele; private Button dao; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mGua1 = (Gua) findViewById(R.id.Circle); mGua1.setTargetPercent(0); play=(Button)findViewById(R.id.play); resele=(Button)findViewById(R.id.resele); dao=(Button)findViewById(R.id.dao); play.setOnClickListener(this); resele.setOnClickListener(this); dao.setOnClickListener(this); } @Override public void onClick(View view) { switch (view.getId()){ case R.id.play: //設(shè)置目標(biāo)百分比為100 mGua1.setTargetPercent(100); mGua1.invalidate(); break; case R.id.resele: //設(shè)置目標(biāo)百分比為0 mGua1.setTargetPercent(0); mGua1.invalidate(); break; case R.id.dao: //設(shè)置目標(biāo)百分比為100 mGua1.setTargetPercent(0); mGua1.invalidate(); break; } } }
Gua
import android.content.Context; import android.content.res.TypedArray; import android.graphics.Canvas; import android.graphics.Paint; import android.graphics.Paint.Align; import android.graphics.RectF; import android.util.AttributeSet; import android.view.View; public class Gua extends View{ private Paint mCirclePaint; private Paint mTextPaint; private Paint mArcPaint; private int mCircleX; private int mCircleY; private float mCurrentAngle; private RectF mArcRectF; private float mStartSweepValue; private float mTargetPercent; private float mCurrentPercent; private int mRadius; private int mCircleBackground; private int mRingColor; private int mTextSize; private int mTextColor; public Gua(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); init(context); } public Gua(Context context, AttributeSet attrs) { super(context, attrs); //自定義屬性 values/attr TypedArray typedArray = context.obtainStyledAttributes(attrs,R.styleable.PercentageRing); //中間圓的背景顏色 默認(rèn)為淺紫色 mCircleBackground = typedArray.getColor(R.styleable.PercentageRing_circleBackground, 0xffafb4db); //外圓環(huán)的顏色 默認(rèn)為深紫色 mRingColor = typedArray.getColor(R.styleable.PercentageRing_ringColor, 0xff6950a1); //中間圓的半徑 默認(rèn)為60 mRadius = typedArray.getInt(R.styleable.PercentageRing_radius, 60); //字體顏色 默認(rèn)為白色 mTextColor = typedArray.getColor(R.styleable.PercentageRing_textColor, 0xffffffff); //最后一定要調(diào)用這個(gè) 釋放掉TypedArray typedArray.recycle(); //初始化數(shù)據(jù) init(context); } public Gua(Context context) { super(context); init(context); } private void init(Context context){ //圓環(huán)開(kāi)始角度 -90° 正北方向 mStartSweepValue = -90; //當(dāng)前角度 mCurrentAngle = 0; //當(dāng)前百分比 mCurrentPercent = 0; //設(shè)置中心園的畫(huà)筆 mCirclePaint = new Paint(); mCirclePaint.setAntiAlias(true); mCirclePaint.setColor(mCircleBackground); mCirclePaint.setStyle(Paint.Style.FILL); //設(shè)置文字的畫(huà)筆 mTextPaint = new Paint(); mTextPaint.setColor(mTextColor); mTextPaint.setAntiAlias(true); mTextPaint.setStyle(Paint.Style.FILL); mTextPaint.setStrokeWidth((float) (0.025*mRadius)); mTextPaint.setTextSize(mRadius/2); mTextPaint.setTextAlign(Align.CENTER); //設(shè)置外圓環(huán)的畫(huà)筆 mArcPaint = new Paint(); mArcPaint.setAntiAlias(true); mArcPaint.setColor(mRingColor); mArcPaint.setStyle(Paint.Style.STROKE); mArcPaint.setStrokeWidth((float) (0.075*mRadius)); //獲得文字的字號(hào) 因?yàn)橐O(shè)置文字在圓的中心位置 mTextSize = (int) mTextPaint.getTextSize(); } //主要是測(cè)量wrap_content時(shí)候的寬和高,因?yàn)閷捀咭粯?,只需要測(cè)量一次寬即可,高等于寬 @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { setMeasuredDimension(measure(widthMeasureSpec), measure(widthMeasureSpec)); //設(shè)置圓心坐標(biāo) mCircleX = getMeasuredWidth()/2; mCircleY = getMeasuredHeight()/2; //如果半徑大于圓心橫坐標(biāo),需要手動(dòng)縮小半徑的值,否則就畫(huà)到外面去了 if (mRadius>mCircleX) { //設(shè)置半徑大小為圓心橫坐標(biāo)到原點(diǎn)的距離 mRadius = mCircleX; mRadius = (int) (mCircleX-0.075*mRadius); //因?yàn)榘霃礁淖兞?,所以要重新設(shè)置一下字體寬度 mTextPaint.setStrokeWidth((float) (0.025*mRadius)); //重新設(shè)置字號(hào) mTextPaint.setTextSize(mRadius/2); //重新設(shè)置外圓環(huán)寬度 mArcPaint.setStrokeWidth((float) (0.075*mRadius)); //重新獲得字號(hào)大小 mTextSize = (int) mTextPaint.getTextSize(); } //畫(huà)中心園的外接矩形,用來(lái)畫(huà)圓環(huán)用 mArcRectF = new RectF(mCircleX-mRadius, mCircleY-mRadius, mCircleX+mRadius, mCircleY+mRadius); } //當(dāng)wrap_content的時(shí)候,view的大小根據(jù)半徑大小改變,但最大不會(huì)超過(guò)屏幕 private int measure(int measureSpec){ int result=0; int specMode = MeasureSpec.getMode(measureSpec); int specSize = MeasureSpec.getSize(measureSpec); if (specMode == MeasureSpec.EXACTLY) { result = specSize; }else { result =(int) (1.075*mRadius*2); if (specMode == MeasureSpec.AT_MOST) { result = Math.min(result, specSize); } } return result; } //開(kāi)始畫(huà)中間圓、文字和外圓環(huán) @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); //畫(huà)中間圓 canvas.drawCircle(mCircleX, mCircleY, mRadius, mCirclePaint); //畫(huà)圓環(huán) canvas.drawArc(mArcRectF, mStartSweepValue ,mCurrentAngle, false, mArcPaint); //畫(huà)文字 canvas.drawText(String.valueOf(mCurrentPercent)+"%", mCircleX, mCircleY+mTextSize/4, mTextPaint); //判斷當(dāng)前百分比是否小于設(shè)置目標(biāo)的百分比 if (mCurrentPercentmTargetPercent){ //當(dāng)前百分比-1 mCurrentPercent-=1; //當(dāng)前角度+360 mCurrentAngle-=3.6; //每10ms重畫(huà)一次 postInvalidateDelayed(10); } } //設(shè)置目標(biāo)的百分比 public void setTargetPercent(int percent){ this.mTargetPercent = percent; } }
Xml文件
values文件中的attrs
看完上述內(nèi)容,你們掌握Android studio中怎么實(shí)現(xiàn)一個(gè)圓形進(jìn)度條的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!