抽獎大轉盤
阿新 • • 發佈:2018-12-01
1.佈局
<?xml version="1.0" encoding="utf-8"?> <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" tools:context=".MainActivity"> <com.bw.ymy.demo6.Custor android:id="@+id/cuuuuuuuuu" android:layout_width="wrap_content" android:layout_height="match_parent" /> <com.bw.ymy.demo6.Custorbegin android:id="@+id/begin" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="250dp" android:layout_marginLeft="280dp"/> </RelativeLayout>
2.主頁面
package com.bw.ymy.demo6; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.view.animation.RotateAnimation; public class MainActivity extends AppCompatActivity { private Custor cuuuu; private Custorbegin begin; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); cuuuu= findViewById(R.id.cuuuuuuuuu); begin=findViewById(R.id.begin); begin.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //隨機旋轉 float degrees = (float)(720 + Math.random() * 1000); //旋轉 RotateAnimation rotateAnimation = new RotateAnimation(0, degrees, 400, 400); rotateAnimation.setDuration(5000); rotateAnimation.setFillAfter(true); //加入動畫 cuuuu.setAnimation(rotateAnimation); //開始動畫 cuuuu.startAnimation(rotateAnimation); } }); } }
3.外面的大圓盤
package com.bw.ymy.demo6; 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 Custor extends View { Paint mpaint; int degress=0; int text=15; RectF rectF; public Custor(Context context) { super(context); } public Custor(Context context, @Nullable AttributeSet attrs) { super(context, attrs); init(); } private void init() { mpaint=new Paint(); mpaint.setStrokeWidth(10); mpaint.setStyle(Paint.Style.FILL); rectF=new RectF(); rectF.right=700; rectF.left=100; rectF.bottom=700; rectF.top=100; } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); // String[] strings=new String[]{"一","二","三","四","五","六"}; int[] colors=new int[]{Color.RED,Color.YELLOW,Color.GRAY,Color.GREEN,Color.BLUE,Color.WHITE}; for (int i=0;i<6;i++) { mpaint.setColor(colors[i]); Path path=new Path(); mpaint.setTextSize(20); canvas.drawArc(rectF,degress,60,true,mpaint); path.addArc(rectF,text,60); mpaint.setColor(Color.BLACK); canvas.drawTextOnPath(strings[i],path,60,60,mpaint); degress+=60; text+=60; } } }
4.裡面的小圓盤
package com.bw.ymy.demo6;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Build;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.util.AttributeSet;
import android.view.View;
public class Custorbegin extends View {
//畫筆
Paint paint;
public Custorbegin(Context context) {
super(context);
}
public Custorbegin(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
init();
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
setMeasuredDimension(120,150);
}
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//顏色
paint.setColor(Color.BLACK);
canvas.drawArc(0,-150,120,120,60,60,true,paint);
paint.setColor(Color.RED);
canvas.drawCircle(60,90,50,paint);
paint.setColor(Color.GREEN);
canvas.drawText("開始",30,100,paint);
}
private void init() {
paint=new Paint();
paint.setColor(Color.RED);
paint.setStyle(Paint.Style.FILL);
paint.setStrokeWidth(10);
paint.setTextSize(30);
}
}