CircleView(画外圆的类)
/**
* 这里是画转盘的
* @author hasee
*/
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
public class CircleView extends View {
//画笔
Paint mPaint;
int mCircleCount=6;
int mStartAngle=0;
//字符串
private String[] mStr = new String[]{"优惠券","十元话费","恭喜发财","恭喜发财","英雄皮肤","50M流量"};
int[] colors=new int[]{Color.YELLOW,Color.DKGRAY,Color.CYAN,Color.GRAY,Color.GREEN,Color.RED};
RectF rectF;
int textdegress=15;
public CircleView(Context context) {
super(context);
brush();
}
public CircleView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
brush();
}
//定义画笔
private void brush() {
//初始化画笔
mPaint = new Paint();
//画笔颜色
mPaint.setColor(Color.BLUE);
//画笔宽度
mPaint.setStrokeWidth(10);
//字体大小
mPaint.setTextSize(60);
//画笔样式
mPaint.setStyle(Paint.Style.FILL);
//Rect(尺寸)类
rectF = new RectF();
rectF.top = 100;
rectF.left = 100;
rectF.right = 600;
rectF.bottom = 600;
}
@Override
//开始画
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
for (int i=0;i<mCircleCount;i++){
mPaint.setColor(colors[i]);
Path path=new Path();
mPaint.setTextSize(24);
canvas.drawArc(rectF,mStartAngle,60,true,mPaint);
path.addArc(rectF,textdegress,60);
mPaint.setColor(Color.BLACK);
canvas.drawTextOnPath(mStr[i],path,30,80,mPaint);
mStartAngle +=60;
textdegress+=60;
}
}
}
ZZView(画内圆和指针的类)
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
/**
* 转盘中间开始按钮和指针
* @author hasee
*/
public class ZZView extends View {
Paint mPaint;
String mStr;
RectF rectF;
public ZZView(Context context) {
super(context);
brush();
}
public ZZView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
brush();
}
private void brush() {
mPaint=new Paint();
mPaint.setColor(Color.RED);
mPaint.setStrokeWidth(10);
mPaint.setTextSize(60);
mPaint.setStyle(Paint.Style.FILL);
rectF=new RectF();
rectF.top=100;
rectF.bottom = 350;
rectF.right = 450;
rectF.left = 250;
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//设置画笔颜色为黑色,
mPaint.setColor(Color.BLACK);
//画出指针,用一个扇形,然后盖住后面补分来简单表示
canvas.drawArc(rectF,60,60,true,mPaint);
//画一个红色的圆形,就是中间的大按钮
mPaint.setColor(Color.RED);
canvas.drawCircle(350,350,90,mPaint);
//添加按钮上的文字
mPaint.setColor(Color.BLACK);
canvas.drawText("start",290,370,mPaint);
}
}
MainActivity(在里面设置动画)
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.RotateAnimation;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private CircleView circleView;
private ZZView zzView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
circleView = findViewById(R.id.circleView);
initView();
}
private void initView() {
//点击事件
findViewById(R.id.zz).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//随机数旋转
float degrrees=(float)(720+Math.random()*1000);
//设置动画
RotateAnimation rotateAnimation=new RotateAnimation(0,-degrrees,350,350);
rotateAnimation.setDuration(5000);//动画时长
rotateAnimation.setFillAfter(true);//保持动画完之后的效果
circleView.setAnimation(rotateAnimation);
circleView.startAnimation(rotateAnimation);
}
});
}
}
布局
<RelativeLayout 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"
android:orientation="vertical"
tools:context=".MainActivity">
<com.bwie.asus.lottery.CircleView
android:id="@+id/circleView"
android:layout_width="wrap_content"
android:layout_height="match_parent"/>
<com.bwie.asus.lottery.ZZView
android:id="@+id/zz"
android:layout_width="wrap_content"
android:layout_height="match_parent"
/>
</RelativeLayout>