实现效果:
布局文件
<?xml version="1.0" encoding="utf-8"?>
<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.ww.wangwei.choujiangview.ChoujiangView
android:layout_width="match_parent"
android:layout_height="match_parent" />
</android.support.constraint.ConstraintLayout>
新建一个class继承View:
public class ChoujiangView extends View implements View.OnClickListener{
private int screenWidth;
private int screenHight;
private int centerX;
private int centerY;
private Paint paint;
private int[] colors;
private String[] desc = new String[]{"性感", "丰满", "知性", "聪明", "贤惠", "优秀"};
private boolean isRote;//是否在旋转
public ChoujiangView(Context context) {
this(context, null);
}
public ChoujiangView(Context context, AttributeSet attrs) {
this(context, attrs, -1);
}
public ChoujiangView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
//获取屏幕宽高信息
DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
screenWidth = displayMetrics.widthPixels;
screenHight = displayMetrics.heightPixels;
//获取屏幕中心坐标
centerX = screenWidth / 2;
centerY = screenHight / 2;
//初始化画笔
initPaint();
//颜色
colors = new int[]{Color.RED, Color.GRAY, Color.YELLOW, Color.BLUE, Color.GREEN, Color.LTGRAY};
this.setOnClickListener(this);
}
//测量
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
setMeasuredDimension(100, 100);
}
//绘图
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.translate(centerX, centerY);
//绘制6个圆弧
RectF rect = new RectF(-300, -300, 300, 300);
float start = 60;
for (int i = 0; i < 6; i++) {
paint.setColor(colors[i]);
canvas.drawArc(rect, start * i, 60, true, paint);
}
//绘制中心的圆
paint.setColor(Color.RED);
canvas.drawCircle(0, 0, 100, paint);
paint.setColor(Color.WHITE);
paint.setTextSize(40);
//获取文字宽度和高度
Rect rectText = new Rect();
paint.getTextBounds("start", 0, 5, rectText);
int width = rectText.width();
int height = rectText.height();
canvas.drawText("start", -width / 2, height / 2, paint);
//绘制描述信息
RectF rectF = new RectF(-200, -200, 200, 200);
for (int i = 0; i < 6; i++) {
paint.setColor(Color.WHITE);
Path path = new Path();
path.addArc(rectF, start * i+15 , 60);
canvas.drawTextOnPath(desc[i], path, 0, 0, paint);
}
}
//初始化画笔
private void initPaint() {
paint = new Paint();
paint.setColor(Color.RED);
paint.setStrokeWidth(20);
paint.setAntiAlias(true);
paint.setStyle(Paint.Style.FILL);
}
@Override
public void onClick(View v) {
if (!isRote){
startAnima();
}
}
private void startAnima() {
isRote = true;
RotateAnimation rotateAnimation;
double random = Math.random();
rotateAnimation = new RotateAnimation(0, (float) (720*random),centerX,centerY);
rotateAnimation.setDuration(800);
rotateAnimation.setFillAfter(true);
//设置重复的次数
rotateAnimation.setInterpolator(new LinearInterpolator());
//设置重复的模式
rotateAnimation.setRepeatMode(Animation.RESTART);
//给动画添加监听
rotateAnimation.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
isRote = false;
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
startAnimation(rotateAnimation);
}
private void setRoundDom(){
double random = Math.random();
RotateAnimation rotateAnimation2 = new RotateAnimation(0, (float) (360*random),centerX,centerY);
rotateAnimation2.setDuration(100);
rotateAnimation2.setFillAfter(true);
startAnimation(rotateAnimation2);
}
}