package com.basic.gaugeprogress.view;

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.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.basic.gaugeprogress.R;

/**
 * 仪表盘进度条.
 */
public class GaugeProgressBar extends View {
    private int mWidth;
    private int mHeight;
    private boolean mIsInit = false;
    private int mProgressWidth;
    private int mProgressColor;
    private int mProgressTextColor;
    private int mProgressTextSize;
    private int mProgress;


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

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

    public GaugeProgressBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray attrType = context.obtainStyledAttributes(attrs, R.styleable
                .custom_progress_attrs);
        mProgressWidth = attrType.getInt(R.attr.progress_width, 15);
        mProgressColor = attrType.getColor(R.attr.progress_color, Color.BLUE);
        mProgressTextColor = attrType.getColor(R.attr.progress_text_color, Color.WHITE);
        mProgressTextSize = attrType.getInt(R.attr.pregress_text_size, 20);//这里应该是sp，将其转换为px
        attrType.recycle();
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (!mIsInit) {
            initView();
            mIsInit = true;
        }

        int temp = Math.min(mWidth, mHeight);
        int paddingTop = Math.abs((mHeight - temp) / 2);
        int paddingLeft = Math.abs((mWidth - temp) / 2);
        float top = 0 + paddingTop;
        float buttom = top + temp;
        float left = 0 + paddingLeft;
        float right = left + paddingLeft;

        Paint paint = new Paint();
        paint.setColor(mProgressColor);
        paint.setAntiAlias(true);
        paint.setStrokeWidth(mProgressWidth);

        RectF oval = new RectF(left, top, right, buttom);
        canvas.drawArc(oval, 135, 45, false, paint); //顺时针方向开始画，以3点钟方向作为起点.
    }

    private void initView() {
        mHeight = getHeight();
        mWidth = getWidth();
    }

}

@@ -251,16 +251,56 @@
 android.
+graphics.SweepGradient;%0D%0Aimport android.
 util.Att
@@ -335,73 +335,8 @@
 iew.
-View;%0D%0Aimport android.view.ViewGroup;%0D%0Aimport android.widget.Text
 View
@@ -377,16 +377,47 @@
 s.R;%0D%0A%0D%0A
+import java.util.ArrayList;%0D%0A%0D%0A
 /**%0D%0A * 
@@ -730,24 +730,128 @@
 rogress;%0D%0A%0D%0A
+    private static final int TOTAL_PROGRESS = 100;%0D%0A    private static final int CIRCLE_ANGLE = 360;%0D%0A%0D%0A
 %0D%0A    public
@@ -2420,10 +2420,11 @@
 35, 
-45
+270
 , fa
@@ -2468,103 +2468,1315 @@
     
-%7D%0D%0A%0D%0A    private void initView() %7B%0D%0A        mHeight = getHeight();%0D%0A        mWidth = getWidth()
+    //%E8%AE%A1%E7%AE%97%E9%95%BF%E5%BA%A6%0D%0A%0D%0A        float sweepAngle = ((float)mProgress / TOTAL_PROGRESS) * CIRCLE_ANGLE;%0D%0A%0D%0A        if(sweepAngle %3C 90 && sweepAngle %3E 0)%7B%0D%0A              paint.setColor(Color.GREEN);%0D%0A        %7D else %7B%0D%0A            ArrayList%3CInteger%3E list = new ArrayList%3C%3E();%0D%0A            if(sweepAngle %3C= 180)%7B%0D%0A                list.add(Color.GREEN);%0D%0A                list.add(Color.YELLOW);%0D%0A            %7D  else %7B%0D%0A                list.add(Color.GREEN);%0D%0A                list.add(Color.YELLOW);%0D%0A                list.add(Color.RED);%0D%0A            %7D%0D%0A            int%5B%5D colors = new int%5Blist.size()%5D;%0D%0A            for (int i = 0; i%3C list.size();i++)%7B%0D%0A                 colors%5Bi%5D = list.get(i);%0D%0A            %7D%0D%0A            SweepGradient gradient = new SweepGradient(0, 0, colors, null);%0D%0A            paint.setShader(gradient);%0D%0A        %7D%0D%0A%0D%0A        canvas.drawArc(oval, 135, sweepAngle, false, paint);%0D%0A    %7D%0D%0A%0D%0A    private void initView() %7B%0D%0A        mHeight = getHeight();%0D%0A        mWidth = getWidth();%0D%0A    %7D%0D%0A%0D%0A    public void setProgress(int progress) %7B%0D%0A        if (progress %3C 0) %7B%0D%0A            return;%0D%0A        %7D%0D%0A        if (progress %3E TOTAL_PROGRESS) %7B%0D%0A            while (progress %3E TOTAL_PROGRESS) %7B%0D%0A                progress = progress %25 TOTAL_PROGRESS;%0D%0A            %7D%0D%0A        %7D%0D%0A%0D%0A        mProgress = progress
 ;%0D%0A 

add gradient