public class ZhuanPanView extends View implements View.OnClickListener {
private String[] contents = new String[]{"A","B","C","D","E","F"}; //文字
private int[] colors = new int[]{Color.parseColor("#8EE5EE"), Color.parseColor("#FFD700"),
Color.parseColor("#FFD39B"), Color.parseColor("#FF8247"),
Color.parseColor("#FF34B3"), Color.parseColor("#F0E68C")}; //颜色
private int mWidth;
private Paint mPaint;
private Context mContext;
private String mStr="Go";
public ZhuanPanView(Context context,AttributeSet attrs) {
super(context, attrs);
mContext = context;
mPaint = new Paint();
setOnClickListener(this);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
mPaint.setColor(Color.RED); //设置边缘的颜色
mPaint.setStyle(Paint.Style.STROKE); //描边 边缘
mPaint.setStrokeWidth(3); //设置边缘的宽度
mPaint.setAntiAlias(true); //设置边缘锯齿
canvas.drawCircle(mWidth/2,mWidth/2,mWidth/2,mPaint); //radius:半径
//画扇形 RectF
RectF rectF = new RectF(0, 0, mWidth, mWidth);
mPaint.setStyle(Paint.Style.FILL);
for (int i = 0; i < colors.length; i++) {
mPaint.setColor(colors[i]);
int startjd = i * 60;
canvas.drawArc(rectF, startjd, 60, true, mPaint);
}
//文字
mPaint.setColor(Color.BLACK); //设置文字颜色
mPaint.setTextSize(30); //设置文字大小
for (int i=0;i<contents.length;i++){
int startjd = i * 60;
Path path = new Path(); //Path 代表路径 想怎么画就怎么画
path.addArc(rectF,startjd,60); //
canvas.drawTextOnPath(contents[i],path,100,100,mPaint);
}
//在最中心画一个圆Go
drawCenterCircle(canvas);
}
//在最中心画一个圆 Go
private void drawCenterCircle(Canvas canvas) {
mPaint.setColor(Color.GREEN); //画中心圆 颜色
canvas.drawCircle(mWidth/2,mWidth/2,50,mPaint);
mPaint.setColor(Color.BLACK); //设置文字颜色
mPaint.setTextSize(24);
Rect rect = new Rect();
mPaint.getTextBounds(mStr,0,mStr.length(),rect);
int width = rect.width();
int height = rect.height();
canvas.drawText(mStr, mWidth / 2 - width / 2, mWidth / 2 + height / 2, mPaint);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
setMeasuredDimension(500, 500); //测量后的宽度和高度
mWidth = getMeasuredWidth();//得到测量过后的高和宽
}
//点击跳转
@Override
public void onClick(View v) {
Random random = new Random();
int jd = random.nextInt(3600);
RotateAnimation rotateAnimation = new RotateAnimation(0f, jd, mWidth / 2, mWidth / 2);
rotateAnimation.setDuration(3000);
rotateAnimation.setFillAfter(true); //停留到最后的状态
startAnimation(rotateAnimation);
}
}
自定义View——转盘
最新推荐文章于 2019-01-08 13:52:12 发布