android画多种颜色的圆环带动画效果

首先在values中的attrs中创建自定义控件的属性


        
        
    

自定义控件继承view

package com.bruce.testgitdemo.test;

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.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewTreeObserver;

import com.ajie.bootstartdemo.R;

import java.util.ArrayList;


/**
 * 自定义圆环
 */
public class MyView extends View {
    private int color = Color.parseColor("#e8e8e8");   //画圆的颜色  默认红色
    private float cirle = 200;   //半径范围大小 默认200
    private Paint mPaint;
    private int widthSize,heightSize;    //屏幕宽高
    private int[] colors;   //颜色数组
    private ArrayList percents;   //百分比数组
    private float percent ;   //总共百分比
    private Boolean flag = true;    // 只执行一次
    private float[] step;   //保存每个圆弧的弧度
    private float[] percentnum ;   //保存每个圆弧的起始弧度
    private boolean isAnim = false;    //是否使用动画 默认不使用


    public MyView(Context context) {
        this(context, null);
    }

    public MyView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);

    }

    public MyView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.MyView, defStyleAttr, 0);
        int n = a.getIndexCount();
        for (int i = 0;i getPercents() {
        return percents;
    }

    public void setPercents(ArrayList percents) {
        this.percents = percents;
    }

    /**
     * 是否使用动画
     * @param isHaveAnim
     */
    public void setIsHaveAnim(Boolean isHaveAnim){
        if (isHaveAnim){
            this.getViewTreeObserver().addOnPreDrawListener(
                    new ViewTreeObserver.OnPreDrawListener() {
                        public boolean onPreDraw() {
                            new Thread(new CircleThread()).start();
                            getViewTreeObserver().removeOnPreDrawListener(this);
                            return false;
                        }
                    });
        }
        this.isAnim = isHaveAnim;
    }

    /**
     * 检查百分比是否超过100%
     */
    private float checkPercent(ArrayList percents){
        if (percents == null){
            Log.d("tag","总百分比为空");
            return 0;
        }
        for (int i = 0;i1) {
                    throw new Exception("百分比不能大于1");
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            step  = new float[percents.size()];
            percentnum = new float[percents.size()];
            for (int i = 0;i1) {
                throw new Exception("百分比不能大于1");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        //循环画圆环
        float nowPercent = 0;
        float nextPercent = 0;
        //绘制圆环  (无动画效果)
        for (int i = 0;i

控件的使用就很简单了

 MyView myView = (MyView) findViewById(R.id.myview);
        myView.setIsHaveAnim(false);
        myView.setColors(colors);
        myView.setPercents(getPercentValue());
 public ArrayList getPercentValue() {
        ArrayList list = new ArrayList<>();
//        for (int i = 0;i<5;i++){
//            list.add(0.15f);
//        }
        list.add(0.1f);
        list.add(0.2f);
        list.add(0.3f);
        list.add(0.1f);
        list.add(0.1f);

        return list;
    }

demo下载地址

你可能感兴趣的:(android进阶)