package com.lichao.bwei.com.lichao20181104s;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import org.w3c.dom.Text;
/**
* date:2018/11/4
* author:李超(li)
* function:
*/
public class Zhuanpan extends View implements View.OnClickListener {
private boolean iRote;
private int mX;
private int mY;
private Paint mPaint;
private int[] mColor;
private String[] mText;
public Zhuanpan(Context context) {
this(context,null);
}
public Zhuanpan(Context context,AttributeSet attrs) {
this(context, attrs,-1);
}
public Zhuanpan(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
//找到中心点
DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
int widthPixels = displayMetrics.widthPixels;
int heightPixels = displayMetrics.heightPixels;
mX = widthPixels / 2;
mY = heightPixels / 2;
mPaint = new Paint();
mPaint.setColor(Color.RED);
mPaint.setStyle(Paint.Style.FILL);
mPaint.setStrokeWidth(20);
mPaint.setAntiAlias(true);
mText = new String[]{"100","150","140","130","120","110"};
mColor = new int[]{Color.BLACK,Color.BLUE,Color.GREEN,Color.RED,Color.BLUE,Color.RED};
this.setOnClickListener(this);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.translate(mX,mY);
//画弧
RectF rectF = new RectF(-240,-240,240,240);
int start = 60;
for (int i = 0; i < 6; i++) {
mPaint.setColor(mColor[i]);
canvas.drawArc(rectF,start*i,60,true,mPaint);
}
//画中心圆
mPaint.setColor(Color.RED);
canvas.drawCircle(0,0,100,mPaint);
//画中心圆内字
mPaint.setColor(Color.WHITE);
mPaint.setTextSize(40);
Rect rect = new Rect();
mPaint.getTextBounds("start",0,5,rect);
int width = rect.width();
int height = rect.height();
canvas.drawText("start",-width/2,height/2,mPaint);
//字体设置
RectF rectF1 = new RectF(-150, -150, 150, 150);
for (int i = 0; i < 6; i++) {
mPaint.setColor(Color.WHITE);
Path path = new Path();
path.addArc(rectF1,start*i+15,60);
canvas.drawTextOnPath(mText[i],path,0,0,mPaint);
}
}
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
setMeasuredDimension(100,100);
}
@Override
public void onClick(View v) {
if (!iRote){
setanima();
}
}
private void setanima() {
iRote = true;
double random = Math.random();
RotateAnimation rotateAnimation = new RotateAnimation(0, (float) (720 * random), mX, mY);
rotateAnimation.setDuration(8000);
rotateAnimation.setFillAfter(true);
rotateAnimation.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
iRote = false;
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
startAnimation(rotateAnimation);
}
}
自定义控件转盘
猜你喜欢
转载自blog.csdn.net/weixin_41722852/article/details/83716176
今日推荐
周排行