Android中怎么實現(xiàn)圓形漸變加載進度條,相信很多沒有經(jīng)驗的人對此束手無策,為此本文總結(jié)了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個問題。
成都創(chuàng)新互聯(lián)是一家集網(wǎng)站建設(shè),永德企業(yè)網(wǎng)站建設(shè),永德品牌網(wǎng)站建設(shè),網(wǎng)站定制,永德網(wǎng)站建設(shè)報價,網(wǎng)絡(luò)營銷,網(wǎng)絡(luò)優(yōu)化,永德網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強企業(yè)競爭力??沙浞譂M足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時我們時刻保持專業(yè)、時尚、前沿,時刻以成就客戶成長自我,堅持不斷學習、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實用型網(wǎng)站。package com.view;import android.content.Context;import android.content.res.TypedArray;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.Matrix;import android.graphics.Paint;import android.graphics.RectF;import android.graphics.SweepGradient;import android.util.AttributeSet;import android.view.View;import com.fx.R;public class CompletedView extends View { // 畫實心圓的畫筆 private Paint mCirclePaint; // 畫圓環(huán)的畫筆 private Paint mRingPaint; // 畫圓環(huán)的畫筆背景色 private Paint mRingPaintBg; // 畫字體的畫筆 private Paint mTextPaint; // 圓形顏色 private int mCircleColor; // 圓環(huán)顏色 private int mRingColor; // 圓環(huán)背景顏色 private int mRingBgColor; // 半徑 private float mRadius; // 圓環(huán)半徑 private float mRingRadius; // 圓環(huán)寬度 private float mStrokeWidth; // 圓心x坐標 private int mXCenter; // 圓心y坐標 private int mYCenter; // 字的長度 private float mTxtWidth; // 字的高度 private float mTxtHeight; // 總進度 private int mTotalProgress = 100; // 當前進度 private int mProgress; private String string; public CompletedView(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.TasksCompletedView, 0, 0); mRadius = typeArray.getDimension(R.styleable.TasksCompletedView_radius, 80); mStrokeWidth = typeArray.getDimension(R.styleable.TasksCompletedView_strokeWidth, 10); mCircleColor = typeArray.getColor(R.styleable.TasksCompletedView_circleColor, 0xFFFFFFFF); mRingColor = typeArray.getColor(R.styleable.TasksCompletedView_ringColor, 0xFFFFFFFF); mRingBgColor = typeArray.getColor(R.styleable.TasksCompletedView_ringBgColor, 0xFFFFFFFF); mRingRadius = mRadius + mStrokeWidth / 2; } RectF oval; //初始化畫筆 private void initVariable() { oval = new RectF(); //內(nèi)圓 mCirclePaint = new Paint(); mCirclePaint.setAntiAlias(true); mCirclePaint.setColor(mCircleColor); mCirclePaint.setStyle(Paint.Style.FILL); mCirclePaint.setStrokeCap(Paint.Cap.ROUND); //外圓弧背景 mRingPaintBg = new Paint(); mRingPaintBg.setAntiAlias(true); mRingPaintBg.setColor(mRingBgColor); mRingPaintBg.setStyle(Paint.Style.STROKE); mRingPaintBg.setStrokeWidth(mStrokeWidth); //外圓弧 mRingPaint = new Paint(); mRingPaint.setAntiAlias(true);// mRingPaint.setColor(mRingColor); mRingPaint.setStyle(Paint.Style.STROKE); mRingPaint.setStrokeWidth(mStrokeWidth); mRingPaint.setStrokeCap(Paint.Cap.ROUND);//設(shè)置線冒樣式,有圓 有方 //中間字 mTextPaint = new Paint(); mTextPaint.setAntiAlias(true); mTextPaint.setStyle(Paint.Style.FILL); mTextPaint.setColor(mRingColor); mTextPaint.setTextSize(mRadius / 2); Paint.FontMetrics fm = mTextPaint.getFontMetrics(); mTxtHeight = (int) Math.ceil(fm.descent - fm.ascent); } SweepGradient sweepGradient; //畫圖 @Override protected void onDraw(Canvas canvas) { mXCenter = getWidth() / 2; mYCenter = getHeight() / 2; //內(nèi)圓 canvas.drawCircle(mXCenter, mYCenter, mRadius, 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); canvas.drawArc(oval1, 0, 360, false, mRingPaintBg); //圓弧所在的橢圓對象、圓弧的起始角度、圓弧的角度、是否顯示半徑連線 //外圓弧 if (mProgress > 0 ) { oval.left = (mXCenter - mRingRadius); oval.top = (mYCenter - mRingRadius); oval.right = mRingRadius * 2 + (mXCenter - mRingRadius); oval.bottom = mRingRadius * 2 + (mYCenter - mRingRadius); if (sweepGradient==null) { int[] arcColors= new int[]{mRingColor,Color.parseColor("#b05e39"),mRingColor}; float[] arcPostion=new float[]{0.0f,0.5f,0.95f};// sweepGradient = new SweepGradient(mXCenter, mYCenter, mRingColor,Color.parseColor("#b05e39")); sweepGradient = new SweepGradient(mXCenter, mYCenter, arcColors,arcPostion); Matrix matrix = new Matrix(); matrix.setRotate(-90,mXCenter,mYCenter); sweepGradient.setLocalMatrix(matrix); mRingPaint.setShader(sweepGradient); } canvas.drawArc(oval, -90, ((float)mProgress / mTotalProgress) * 360, false, mRingPaint); // //字體 String txt = mProgress + "%"; mTxtWidth = mTextPaint.measureText(txt, 0, txt.length()); canvas.drawText(txt, mXCenter - mTxtWidth / 2, mYCenter + mTxtHeight / 4, mTextPaint); } } public void setText(String string){ } //設(shè)置進度 public void setProgress(int progress) { mProgress = progress; postInvalidate();//重繪 }}
看完上述內(nèi)容,你們掌握Android中怎么實現(xiàn)圓形漸變加載進度條的方法了嗎?如果還想學到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!
文章標題:Android中怎么實現(xiàn)圓形漸變加載進度條-創(chuàng)新互聯(lián)
標題鏈接:http://jinyejixie.com/article44/dcgjee.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供軟件開發(fā)、網(wǎng)站收錄、ChatGPT、外貿(mào)網(wǎng)站建設(shè)、App設(shè)計、網(wǎng)站維護
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容