public class ChoujiangView extends View implements View.OnClickListener {
private int screenWidth;//屏幕宽度
private int screenHeight;//屏幕高度
private int centerX;
private int centerY;
private ArrayList<Integer> yan = new ArrayList<>();
private TypedArray typedArray;
private boolean isRote;
private Paint mPaint;//画笔工具
private int[] colors;
private int color1;
private int color2;
private int color3;
private int color4;
private int color5;
private int color6;
private String[] desc = new String[]{"网络工程师", "安卓工程师", "UI工程师", "高级翻译员", "我", "他"};
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;
screenHeight = displayMetrics.heightPixels;
//获取屏幕中心坐标
centerX = screenWidth/2;
centerY = screenHeight/2;
//初始化画笔
initpaint();
colors = new int[]{Color.RED, Color.GRAY, Color.YELLOW, Color.BLUE, Color.GREEN, Color.DKGRAY, Color.WHITE};
//给自己添加事件
this.setOnClickListener(this);
isCumsore(context,attrs);
}
private void isCumsore(Context context, AttributeSet attrs) {
typedArray = context.obtainStyledAttributes(attrs,R.styleable.ChoujiangView);
color1 = typedArray.getColor(R.styleable.ChoujiangView_Color1, Color.YELLOW);
color2 = typedArray.getColor(R.styleable.ChoujiangView_Color2, Color.DKGRAY);
color3 = typedArray.getColor(R.styleable.ChoujiangView_Color3, Color.LTGRAY);
color4 = typedArray.getColor(R.styleable.ChoujiangView_Color4, Color.MAGENTA);
color5 = typedArray.getColor(R.styleable.ChoujiangView_Color5, Color.CYAN);
color6 = typedArray.getColor(R.styleable.ChoujiangView_Color6, Color.BLUE);
yan.add(color1);
yan.add(color2);
yan.add(color3);
yan.add(color4);
yan.add(color5);
yan.add(color6);
}
//测量View大小
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//super.onMeasure(widthMeasureSpec, heightMeasureSpec);
setMeasuredDimension(widthMeasureSpec,heightMeasureSpec);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//移动画布的坐标原点
canvas.translate(centerX,centerY);
//绘制6个圆弧
RectF rect = new RectF(-200,-200,200,200);
float start=60;
for (int i = 0; i <6 ; i++) {
mPaint.setColor(colors[i]);
canvas.drawArc(rect,start*i,60,true,mPaint);
}
//绘制中心的圆
mPaint.setColor(Color.RED);
canvas.drawCircle(0,0,50,mPaint);
mPaint.setColor(Color.WHITE);
mPaint.setTextSize(30);
//获取文字宽度个高度
Rect rect1 = new Rect();
mPaint.getTextBounds("start",0,5,rect1);
int width = rect1.width();
int height = rect1.height();
canvas.drawText("start",-width/2,height/2,mPaint);
//绘制描述信息
RectF rectF = new RectF(-100, -100, 100, 100);
for (int i = 0; i <6 ; i++) {
mPaint.setColor(Color.WHITE);
Path path = new Path();
path.addArc(rectF, (float) (start*i+15),60);
canvas.drawTextOnPath(desc[i],path,0,0,mPaint);
}
}
private void initpaint() {
mPaint = new Paint();
mPaint.setColor(Color.RED);
mPaint.setStrokeWidth(20);
mPaint.setStyle(Paint.Style.FILL);
mPaint.setAntiAlias(true);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()){
case MotionEvent.ACTION_DOWN:
float rawX = event.getRawX();
float rawY = event.getRawY();
if (rawX<=centerX+100&&rawX>=centerX-100&&rawY<=centerY+150&&rawY>=centerY+20){
if (!isRote){
start();
}else {
clearAnimation();
}
}
break;
}
return super.onTouchEvent(event);
}
private void start(){
isRote=true;
RotateAnimation rotateAnimation;
double random = Math.random();
rotateAnimation=new RotateAnimation(0, (float) (720*random),centerX,centerY);
rotateAnimation.setDuration(800);
//rotateAnimation.setRepeatCount(-1);
rotateAnimation.setFillAfter(true);
rotateAnimation.setInterpolator(new LinearInterpolator());
//设置重复模式
rotateAnimation.setRepeatMode(Animation.RESTART);
rotateAnimation.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
if (listen!=null){
listen.onClick();
}
}
@Override
public void onAnimationEnd(Animation animation) {
isRote=false;
if (listen!=null){
listen.onclick2();
}
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
startAnimation(rotateAnimation);
}
@Override
public void onClick(View v) {
/*if (!isRote){
start();
}*/
}
ClickListen listen;
public void setListen(ClickListen listen) {
this.listen = listen;
}
public interface ClickListen{
void onClick();
void onclick2();
}