1. 程式人生 > >自定義View來畫大轉盤進行抽獎的過程

自定義View來畫大轉盤進行抽獎的過程

package com.example.app_bingtu_work;

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;

import org.w3c.dom.Text;

public class BingTuCustomView extends View {

private Paint paint;
private RectF rectF;
private RectF rectF1;
float ZE_RO = 0;
private int[] colors = new int[]{
        Color.parseColor("#FF8247"),
        Color.parseColor("#ccff44"),
        Color.parseColor("#44ff44"),
        Color.parseColor("#ff44ff"),
        Color.parseColor("#FF00FF"),
        Color.parseColor("#00BFFF")
};

private String[] strings = new String[]{
        "趙    軍    輝","魏    志    遠","邵    文    龍","李    博    文","劉    洋    洋","魏    開    心"
};

public BingTuCustomView(Context context) {
    super(context);

    initData();
}

public BingTuCustomView(Context context, @Nullable AttributeSet attrs) {
    super(context, attrs);
    initData();
}

private void initData() {

    paint = new Paint();
    paint.setColor(Color.BLACK);
    paint.setStyle(Paint.Style.FILL);
    paint.setStrokeWidth(10);

    rectF = new RectF();

    rectF.top = 100;
    rectF.left = 100;
    rectF.right = 400;
    rectF.bottom = 400;

}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);

    int measuredWidth = getMeasuredWidth();

    rectF1 =new RectF(0,0,measuredWidth,measuredWidth);
}

@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
    super.onLayout(changed, left, top, right, bottom);
}

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    for(int i=0;i<6;i++)
    {
        canvas.drawArc(rectF,ZE_RO,60,true,paint);
        paint.setColor(colors[i]);
        Path path = new Path();
        paint.setTextSize(25);
        path.addArc(rectF1,(i-1)*60+60,60);
        canvas.drawTextOnPath(strings[i],path,50,130,paint);
        ZE_RO+=60;
    }
}

}

package com.example.app_bingtu_work;

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;

public class BingTuInsideView extends View {

private Paint paint;
private RectF rectF;

public BingTuInsideView(Context context) {
    super(context);

    initData();
}

public BingTuInsideView(Context context, @Nullable AttributeSet attrs) {
    super(context, attrs);
    initData();
}

private void initData() {
    paint = new Paint();
    paint.setColor(Color.RED);
    paint.setTextSize(20);
    paint.setStyle(Paint.Style.FILL);


    rectF = new RectF();

    rectF.top = 150;
    rectF.bottom = 250;
    rectF.left = 220;
    rectF.right = 280;


}

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    paint.setColor(Color.BLACK);
    canvas.drawArc(rectF,60,60,true,paint);
    paint.setColor(Color.RED);
    canvas.drawCircle(250,250,30,paint);
    paint.setColor(Color.WHITE);
    canvas.drawText("開始",230,260,paint);
}

}

package com.example.app_bingtu_work;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.animation.RotateAnimation;

import com.example.app_work_two.R;

public class BingTuActivity extends AppCompatActivity {

private BingTuCustomView bingTuCustomView;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.binttu_activity);

   bingTuCustomView = findViewById(R.id.custom);

   findViewById(R.id.inside_custom).setOnClickListener(new View.OnClickListener() {
       @Override
       public void onClick(View v) {
           float degrees = (float) (720 + Math.random() * 5000);

           RotateAnimation rotateAnimation = new RotateAnimation(0,degrees,250,250);
           rotateAnimation.setDuration(5000);
           rotateAnimation.setFillAfter(true);
           bingTuCustomView.startAnimation(rotateAnimation);
       }
   });
}

}