自定义的类

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

public class CustomView  extends View {
    private float circleWidth;
    private int defaultCircleColor;

    private int currentCircleColor;

    //画笔
    private Paint mPaint;

    //new 调用
    public CustomView(Context context) {
        this(context,null);
    }

    //加载布局
    public CustomView(Context context,  AttributeSet attrs) {
        this(context, attrs,-1);
    }

    //手动调用
    public CustomView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context,attrs);

    }

    private void init(Context context,AttributeSet attrs) {
        //初始化画笔
        mPaint=new Paint(Paint.ANTI_ALIAS_FLAG);
        //首先要获得我们自定义的属性  得到一个类似于集合的东西
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.Circle);
        //遍历并取出数据
        for (int i = 0; i < typedArray.getIndexCount(); i++) {
            //通过getIndex方法得到下标 索引
            int index = typedArray.getIndex(i);
            switch(index){
                case R.styleable.Circle_CircleWidth:
                    //得到宽度值  俩个参数分别是索引,和默认值 即:默认宽
                    circleWidth = typedArray.getDimension(index, 3);
                    break;
                case R.styleable.Circle_CircleColor:
                    defaultCircleColor = typedArray.getColor(index, Color.RED);
                    //将,默认的颜色赋予当前颜色
                    currentCircleColor=defaultCircleColor;
                    break;
                default:
                    break;
            }

        }
    }
    //定义圆的属性  圆心坐标  以及半径  当前角度
    private float circleX;
    private float circleY;
    private float radius=130;
    private float currentDegree=0;//默认为0

    //开始画
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //颜色
        mPaint.setColor(Color.RED);

        mPaint.setTextSize(20);
        //设置抗锯齿
        mPaint.setAntiAlias(true);

        //设置风格
        mPaint.setStyle(Paint.Style.STROKE);
        circleX=getWidth()/2;
        circleY=getHeight()/2;


        canvas.drawCircle(circleX,circleY,radius,mPaint);//画一个背景圆

        canvas.save();//用来在动画效果之后保存状态的   这里不写也可以

        //旋转画布 如果旋转的角度大的话,视觉上看着是旋转快的
        canvas.rotate(currentDegree,circleX,circleY);//三个参数 分别是旋转的角度和旋转的XY中心

        //画三角形
        //提供了一些api可以用来画线(画路径)
        Path path = new Path();
        //从哪开始画 从A开始画
        path.moveTo(circleX + radius, circleY);
        //从A点画一个直线到D点
        path.lineTo(circleX + radius - 20, circleY - 20);
        //从D点画一个直线到B点
        path.lineTo(circleX + radius, circleY + 20);
        //从B点画一个直线到C点
        path.lineTo(circleX + radius + 20, circleY - 20);
        //闭合  --  从C点画一个直线到A点
        path.close();

        //以上都是一个画三角形的操作

        mPaint.setStyle(Paint.Style.FILL);//重新设置画笔的样式 颜色  画出
        mPaint.setColor(Color.BLACK);   //设置颜色
        canvas.drawPath(path, mPaint);
        canvas.restore();   //作用  将坐标系恢复到原点的时候  在save()方法之后调用  通常和save()方法连用
        //旋转的角度一倍速的增加
        currentDegree+=1*currentSpeed;
        if (!isPause) {
            invalidate();   //设置是否擦除背景
        }
    }
    //定义旋转速度
    private int currentSpeed=1;
    //是否暂停
    private boolean isPause=false;
    //加速
    public void speed() {
        ++currentSpeed;
        if (currentSpeed >= 10) {
            currentSpeed = 10;
            Toast.makeText(getContext(), "我比闪电还快", Toast.LENGTH_SHORT).show();
        }
    }

    //减速
    public void slowDown() {
        --currentSpeed;
        if (currentSpeed <=1) {
            currentSpeed = 1;
            Toast.makeText(getContext(), "我比乌龟还慢", Toast.LENGTH_SHORT).show();

        }
    }
    //暂停
    public void pauseOrStart() {

        //如果是开始状态的话去重新绘制
        if (isPause) {
            isPause = !isPause;
            invalidate();
        } else {
            isPause = !isPause;
        }
    }

    //设置背景颜色
    public void setColor(int color) {
        if (currentCircleColor != color) {
            currentCircleColor = color;
        } else {
            currentCircleColor = defaultCircleColor;
        }
    }



}

MainActivity.java

import .Activity;
import android.content.Intent;
import android.graphics.Color;
import .AppCompatActivity;
import android.os.Bundle;
import android.view.Display;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    private TextView mTextView;
    private CustomView mCustomView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mCustomView = findViewById(R.id.cirle);

        Activity activity = new Activity();

    }

    //在setContentView回调完之后调用此方法
    @Override
    public void onContentChanged() {
        super.onContentChanged();
    }

    //实现onResultActivity 方法执行之后
    //当我们在activity的启动模式中设置为栈内唯一时,也就是android:launchMode=”singleTask”或android:launchMode=”signleTop”时,会用到这个方法。
    //不创建该Activity的新实例,则系统会调用原有实例的onNewIntent()方法来处理此intent.
    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
    }


    //实现Activity 的覆盖
    @Override
    public void setIntent(Intent newIntent) {
        super.setIntent(newIntent);
    }

    public void onClick(View view) {
        mCustomView.setColor(Color.BLUE);
    }

    public void add(View view) {
        mCustomView.speed();
    }

    public void slow(View view) {
        mCustomView.slowDown();
    }

    public void pauseOrStart(View view) {
        mCustomView.pauseOrStart();
    }


}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http:///apk/res/android"
    xmlns:app="http:///apk/res-auto"
    xmlns:tools="http:///tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/activity_main"
    tools:context=".MainActivity">


    <Button
        android:id="@+id/set_color_btn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:onClick="onClick"
        android:text="设置颜色" />

    <Button
        android:id="@+id/add"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/set_color_btn"
        android:layout_centerHorizontal="true"
        android:onClick="add"
        android:text="加速" />

    <Button
        android:id="@+id/slow"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/add"
        android:layout_centerHorizontal="true"
        android:onClick="slow"
        android:text="减速" />

    <Button
        android:id="@+id/pause_or_start"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/slow"
        android:layout_centerHorizontal="true"
        android:onClick="pauseOrStart"
        android:text="暂停/开始" />

    <com.liu.view1.CustomView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:CircleColor="#f00"
        app:CircleWidth="3dp"
        android:id="@+id/cirle"
        android:layout_centerInParent="true"
        />

</RelativeLayout>

实现思路

通过自定义的类 继承 任意的控件
通过canvas(画布) path (画笔)
调用path对象内部的方法 实现操作