FanView
public class FanView extends View implements View.OnClickListener {
private Paint mFanPaint,mTextPaint;//扇形画笔和文字画笔
private ArrayList<Fan> mFanList = new ArrayList<>();
private RotateAnimation mRotateAnimation;//控件旋转动画
private boolean isStartAnim;//是否已经启动了动画
public FanView(Context context) {
super(context);
init();
}
public FanView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public FanView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
private void init(){
mFanPaint = new Paint();
mFanPaint.setStyle(Paint.Style.FILL);//设置画笔为全填充
mTextPaint = new Paint();
mTextPaint.setTextSize(60);//设置文字大小
setOnClickListener(this);//添加点击事件
}
/**
* 添加扇形
*/
public void addFan(Fan fan){
mFanList.add(fan);//添加扇形
invalidate();//刷新控件,回调onDraw方法
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//*****定义矩形区域用于画环形:圆,椭圆等
RectF rectF = new RectF();
rectF.left = 0;
rectF.top=0;
rectF.right=800;
rectF.bottom = 800;
//*****定义矩形区域用于画环形:圆,椭圆等
for (int i = 0; i <mFanList.size() ; i++) {
Fan fan = mFanList.get(i);
//*********画扇形
mFanPaint.setColor(fan.getColor());
canvas.drawArc(rectF,fan.getStartAngle(),
360/mFanList.size(),true,mFanPaint);//画扇形
//********画扇形
//********把文字画到扇形上
Path path = new Path();
path.addArc(rectF,fan.getStartAngle(), 360/mFanList.size());//定义文字区域,需要传入开始角度和结束角度
mTextPaint.setColor(Color.BLACK);
//沿路径path画文字,第一个参数是文字,第二个参数是路径,
// 第三个参数是水平距离,也就是扇形的起点边界
//第四个参数是纵向距离
canvas.drawTextOnPath(fan.getName(),path,200,60,mTextPaint);
//********把文字画到扇形上
}
mFanPaint.setColor(Color.BLACK);
canvas.drawCircle(400,400,150,mFanPaint);//画中间圆
mTextPaint.setColor(Color.WHITE);
canvas.drawText("Start",400-mTextPaint.measureText("Start")/2,
400+mTextPaint.getTextSize()/2,mTextPaint);//画中间文字
}
@Override
public void onClick(View v) {
if (isStartAnim){
this.clearAnimation();
mRotateAnimation = new RotateAnimation(0,(int)(360*Math.random()),400,400);
mRotateAnimation.setDuration(100);
mRotateAnimation.setRepeatCount(0);//重复次数
mRotateAnimation.setFillEnabled(true);//动画执行保留目前的状态
mRotateAnimation.setFillAfter(true);//动画执行结束保留目前的状态
startAnimation(mRotateAnimation);
isStartAnim =false;
}else{
mRotateAnimation = new RotateAnimation(0,360,400,400);
mRotateAnimation.setDuration(1000);
mRotateAnimation.setRepeatCount(-1);//重复次数
mRotateAnimation.setInterpolator(new LinearInterpolator());//差速器:匀速执行
startAnimation(mRotateAnimation);
isStartAnim = true;
}
}
}
MainActivity
public class MainActivity extends AppCompatActivity {
FanView mFanView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_car);
mFanView = findViewById(R.id.fan_view);
int[] colorArray = new int[]{Color.BLUE,Color.RED,Color.GRAY,
Color.YELLOW,Color.RED,Color.GREEN};
for (int i = 0; i <6 ; i++) {
Fan fan = new Fan();
fan.setStartAngle(i*60);
fan.setName("扇形"+i);
fan.setColor(colorArray[i]);
mFanView.addFan(fan);
}
}
}
bean类
package com.baidu.yuanpan.bean;
public class Fan {
int startAngle;
String name;
int color;
public Fan() {
this.startAngle = startAngle;
this.name = name;
this.color = color;
}
public int getStartAngle() {
return startAngle;
}
public void setStartAngle(int startAngle) {
this.startAngle = startAngle;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getColor() {
return color;
}
public void setColor(int color) {
this.color = color;
}
@Override
public String toString() {
return "Fan{" +
"startAngle=" + startAngle +
", name='" + name + '\'' +
", color=" + color +
'}';
}
}
布局加载
<android.support.constraint.ConstraintLayout xmlns:android=“http://schemas.android.com/apk/res/android”
xmlns:app=“http://schemas.android.com/apk/res-auto”
xmlns:tools=“http://schemas.android.com/tools”
android:layout_width=“match_parent”
android:layout_height=“match_parent”
tools:context=".MainActivity">
<com.baidu.yuanpan.View.FanView
android:id="@+id/fanView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</android.support.constraint.ConstraintLayout>