Android自定義view畫圓,圓上面有箭頭,可控制速度,可控制顏色變化

Android自定義view畫圓,圓上面有箭頭,可控制速度,可控制顏色變化。

下面貼出具體類的使用方法:

package com.example.mycustomcirclearrowviewdemo.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.Path;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Toast;

import com.example.mycustomcirclearrowviewdemo.R;

/**
 * Created by hasee on 2017/9/4.
 */

public class MyCustomCircleArrowView extends View {

    //從xml中獲取的顏色
    private int circleBoundColor;
    private float circleBoundWidth;

    //當前畫筆畫圓的顏色
    private int CurrenCircleBoundColor;

    private Paint paint;

    public MyCustomCircleArrowView(Context context) {
        super(context);
        initVeiw(context);
    }


    public MyCustomCircleArrowView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initVeiw(context);
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.MyCustomCircleArrowView);
        for (int i = 0; i < typedArray.getIndexCount(); i++) {

            //就是我們自定義的屬性的資源id
            int attr = typedArray.getIndex(i);
            switch (attr) {
                case R.styleable.MyCustomCircleArrowView_circlr_bound_color:
                    circleBoundColor = typedArray.getColor(attr, Color.RED);
                    CurrenCircleBoundColor = circleBoundColor;
                    break;
                case R.styleable.MyCustomCircleArrowView_circlr_bound_width:
                    circleBoundWidth = typedArray.getDimension(attr, 3);
                    break;
            }
        }
    }

    private void initVeiw(Context context) {
        paint = new Paint();
    }

    public void setColor(int color) {
        if (CurrenCircleBoundColor != color) {
            CurrenCircleBoundColor = color;
        } else {
            CurrenCircleBoundColor = circleBoundColor;
        }
    }


    //圓心
    private float pivotX;
    private float pivotY;
    private float radius = 130;

    private float currentDegree = 0;

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        paint.setAntiAlias(true);
        paint.setColor(CurrenCircleBoundColor);
        paint.setStrokeWidth(circleBoundWidth);
        paint.setStyle(Paint.Style.STROKE);
        pivotX = getWidth() / 2;
        pivotY = getHeight() / 2;
        canvas.drawCircle(pivotX, pivotY, radius, paint);

        canvas.save();

        //旋轉畫佈 , 如果旋轉的的度數大的話,視覺上看著是旋轉快的
        canvas.rotate(currentDegree, pivotX, pivotY);

        //提供瞭一些api可以用來畫線(畫路徑)
        Path path = new Path();

        //從哪開始畫 從A開始畫
        path.moveTo(pivotX + radius, pivotY);

        //從A點畫一個直線到D點
        path.lineTo(pivotX + radius - 20, pivotY - 20);

        //從D點畫一個直線到B點
        path.lineTo(pivotX + radius, pivotY + 20);

        //從B點畫一個直線到C點
        path.lineTo(pivotX + radius + 20, pivotY - 20);

        //閉合  --  從C點畫一個直線到A點
        path.close();
        paint.setStyle(Paint.Style.FILL);
        paint.setColor(Color.BLACK);
        canvas.drawPath(path, paint);
        canvas.restore();

        //旋轉的度數一個一個度數增加,  如果乘以一個速度的話,按一個速度速度增加
        currentDegree += 1 * currentSpeed;

        if (!isPause) {
            invalidate();
        }
    }

    private int currentSpeed = 1;
    private boolean isPause = false;

    public int speed() {
        ++currentSpeed;
        if (currentSpeed >= 10) {
            currentSpeed = 10;
            Toast.makeText(getContext(), "我比閃電還快", Toast.LENGTH_SHORT).show();

        }
        return currentSpeed;
    }

    public void slowDown() {
        --currentSpeed;
        if (currentSpeed <=1) {
            currentSpeed = 1;
        }
    }

    public void pauseOrStart() {

        //如果是開始狀態的話去重新繪制
        if (isPause) {
            isPause = !isPause;
            invalidate();
        } else {
            isPause = !isPause;
        }
    }
}

這個類在xml文件裡面聲明,就可以使用

You May Also Like