Androidカスタムアニメーション付きの半円環の進捗効果

8192 ワード

本論文の例では、Androidの半円環型進捗効果の具体的なコードを共有しています。

package com.newair.ondrawtext;

import android.animation.ValueAnimator;
import android.annotation.TargetApi;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Typeface;
import android.os.Build;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.OvershootInterpolator;

/**
 * Created by ouhimehime on 16/6/15.
 * --------     -------
 */
public class CustomView extends View {


  //  
  private Paint paint;
  private RectF oval;


  //    
  private int roundColor;
  //    
  private int progressColor;
  //    
  private boolean textIsShow;
  //    
  private float textSize = 14;
  //    
  private int textColor;
  //    
  private int max = 1000;
  //    
  private int progress = 300;
  //    
  private int roundWidth = 30;

  private int viewWidth; //  --      

  private float nowPro = 0;//    

  private ValueAnimator animator;

  public CustomView(Context context) {
    super(context);
  }

  public CustomView(Context context, AttributeSet attrs) {
    super(context, attrs);
    initAttrs(attrs, context);
  }

  public CustomView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    initAttrs(attrs, context);
  }

  @TargetApi(Build.VERSION_CODES.LOLLIPOP)
  public CustomView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
    super(context, attrs, defStyleAttr, defStyleRes);
    initAttrs(attrs, context);
  }


  private void initAttrs(AttributeSet attr, Context context) {
    TypedArray array = context.obtainStyledAttributes(attr, R.styleable.CustomView);


    roundColor = array.getColor(R.styleable.CustomView_roundColor, Color.BLACK);//    
    progressColor = array.getColor(R.styleable.CustomView_progressColor, Color.RED);//    
    textIsShow = array.getBoolean(R.styleable.CustomView_textIsShow, false);//  
    textSize = array.getDimension(R.styleable.CustomView_textSize, 14);//    
    textColor = array.getColor(R.styleable.CustomView_textColor, Color.BLACK);//    
    roundWidth = array.getInt(R.styleable.CustomView_roundWidth, 30);//    

    array.recycle();

    //  
    animator = ValueAnimator.ofFloat(0, progress);
    animator.setDuration(1800);
    animator.setInterpolator(new OvershootInterpolator());
    animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
      @Override
      public void onAnimationUpdate(ValueAnimator animation) {
        nowPro = (float) animation.getAnimatedValue();
        postInvalidate();
      }
    });
    animator.start();

  }

  @Override
  protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

    final int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
    final int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);

    if (widthSpecMode == MeasureSpec.AT_MOST) {//       
      setMeasuredDimension(widthMeasureSpec, (widthSpecSize / 2) + (int) (Math.cos(20) * (widthSpecSize / 2)));
    } else if (widthMeasureSpec == MeasureSpec.EXACTLY) {//      
      setMeasuredDimension(widthMeasureSpec, (widthSpecSize / 2) + (int) (Math.cos(20) * (widthSpecSize / 2)));
    } else {
      setMeasuredDimension(widthMeasureSpec, (viewWidth / 2) + (int) (Math.cos(20) * (viewWidth / 2)));
    }
  }

  @Override
  protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    super.onSizeChanged(w, h, oldw, oldh);

    viewWidth = w;//                 

    //        
    oval = new RectF();
    oval.left = roundWidth + getPaddingLeft();
    oval.top = roundWidth + getPaddingTop();
    oval.right = viewWidth - roundWidth - getPaddingRight();
    oval.bottom = viewWidth - roundWidth - getPaddingBottom();

  }


  @Override
  protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    Paint paint = new Paint();
    paint.setAntiAlias(true);            //        
    paint.setColor(roundColor);           //      
    paint.setStrokeWidth(roundWidth);        //  
    paint.setStyle(Paint.Style.STROKE);       //  
    canvas.drawArc(oval, 160, 220, false, paint);  //    

    //    
    paint.setColor(progressColor);
    paint.setStrokeWidth(roundWidth + 1);
    canvas.drawArc(oval, 160, 220 * nowPro / max, false, paint); //    


    if (textIsShow) {
      paint.setColor(textColor);
      paint.setStrokeWidth(0);
      paint.setTypeface(Typeface.DEFAULT);
      paint.setTextSize(textSize * 2);
      float textWidth = paint.measureText((int) ((nowPro / (float) max) * 100) + "%");
      canvas.drawText((int) ((nowPro / (float) max) * 100) + "%", viewWidth / 2 - textWidth / 2, viewWidth / 2, paint);
    }

  }


  private int getDefaultHeight() {
    return 0;
  }

  private int getDefaultWidth() {
    return 0;
  }


  public int getRoundColor() {
    return roundColor;
  }

  public void setRoundColor(int roundColor) {
    this.roundColor = roundColor;
  }

  public int getProgressColor() {
    return progressColor;
  }

  public void setProgressColor(int progressColor) {
    this.progressColor = progressColor;
  }

  public boolean getText() {
    return textIsShow;
  }

  public void setText(boolean text) {
    this.textIsShow = text;
  }

  public float getTextSize() {
    return textSize;
  }

  public void setTextSize(float textSize) {
    this.textSize = textSize;
  }

  public int getTextColor() {
    return textColor;
  }

  public void setTextColor(int textColor) {
    this.textColor = textColor;
  }

  public int getMax() {
    return max;
  }

  public void setMax(int max) {
    this.max = max;
  }

  public int getProgress() {
    return progress;
  }

  public void setProgress(int progress) {
    this.progress = progress;
  }
}
カスタム属性

<declare-styleable name="CustomView">
    <attr name="roundColor" format="color" />
    <attr name="progressColor" format="color" />
    <attr name="textIsShow" format="boolean" />
    <attr name="textSize" format="dimension" />
    <attr name="textColor" format="color" />
    <attr name="roundWidth" format="integer" />
  </declare-styleable>

使い方

<com.newair.ondrawtext.CustomView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:visibility="visible"
    app:progressColor="@android:color/holo_orange_dark"
    app:roundColor="@android:color/holo_blue_dark"
    app:roundWidth="45"
    app:textColor="@android:color/black"
    app:textIsShow="true"
    app:textSize="14sp" />

以上が本文の全部です。皆さんの勉強に役に立つように、私たちを応援してください。