안 드 로 이 드 는 원형 그 라 데 이 션 로드 진행 바 를 실현 합 니 다.

5815 단어 Android진도 표
최근 디자인 은 원형 진도 바 의 점진 적 인 수 요 를 요구 합 니 다.
1.원형 진도 줄 그리 기
2.그 라 데 이 션 해결


최종 구현 효과 코드

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;
 //       
 private Paint mRingPaint;
 //          
 private Paint mRingPaintBg;
 //       
 private Paint mTextPaint;
 //     
 private int mCircleColor;
 //     
 private int mRingColor;
 //       
 private int mRingBgColor;
 //   
 private float mRadius;
 //     
 private float mRingRadius;
 //     
 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();
  //  
  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);//      ,     


  //   
  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;

  //  
  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){

 }

 //    
 public void setProgress(int progress) {
  mProgress = progress;
  postInvalidate();//  
 }
}
이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.

좋은 웹페이지 즐겨찾기