1. 程式人生 > >圓盤(抽獎)自定義View

圓盤(抽獎)自定義View

Java程式碼
package com.jia.lianxi4yuanandtoolbar;

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;

public class ChoujiangView extends View implements View.OnClickListener {

private int screenWidth;
private int screenHeight;
private int centerX;
private int centerY;
private Paint mpaint;
private int[] colors;
private String[] desc;
private boolean isRote;//設定是否旋轉
private RotateAnimation rotateAnimation;

public ChoujiangView(Context context) {
    this(context, null);
}

public ChoujiangView(Context context, AttributeSet attrs) {
    this(context, attrs, -1);
}

public ChoujiangView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    //獲取螢幕的寬高
    DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
    screenWidth = displayMetrics.widthPixels;
    screenHeight = displayMetrics.heightPixels;
    //獲取螢幕中心的座標
    centerX = screenWidth / 2;
    centerY = screenHeight / 2;
    //初始化畫筆
    initPaint();
    //設定6個圓弧的顏色
    colors = new int[]{Color.MAGENTA, Color.CYAN, Color.YELLOW, Color.GRAY, Color.GREEN, Color.BLUE};
    //設定6個圓弧的描述內容
    desc = new String[]{"香山", "長城", "百山", "公園", "睡覺", "逛吃"};
    //設定點選事件
    this.setOnClickListener(this);

}

//測量view大小
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    //設定大小
    setMeasuredDimension(1000, 1000);
}

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    //移動座標原點到螢幕中心
    canvas.translate(centerX, centerY);
    //繪製6個圓弧
    RectF rectF = new RectF(-300, -300, 300, 300);
    float start = 60;
    for (int i = 0; i < 6; i++) {
        mpaint.setColor(colors[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 r = new RectF(-200, -200, 200, 200);
    for (int i = 0; i < 6; i++) {
        Path path = new Path();
        path.addArc(r, start * i + 20, 60);
        canvas.drawTextOnPath(desc[i], path, 0, 0, mpaint);
    }

}

private void initPaint() {
    mpaint = new Paint();
    mpaint.setColor(Color.RED);
    mpaint.setStrokeWidth(20);
    mpaint.setStyle(Paint.Style.FILL);
    mpaint.setAntiAlias(true);
}

@Override
public void onClick(View v) {
    if (!isRote) {
        startAnima();
    }
}

private void startAnima() {
    isRote = true;
    double random = Math.random();
    rotateAnimation = new RotateAnimation(0, (float) (360 * random), centerX, centerY);
    rotateAnimation.setFillAfter(true);
    rotateAnimation.setDuration(500);
    rotateAnimation.setRepeatMode(Animation.RESTART);
    rotateAnimation.setInterpolator(new LinearInterpolator());
    rotateAnimation.setAnimationListener(new Animation.AnimationListener() {
        @Override
        public void onAnimationStart(Animation animation) {

        }

        @Override
        public void onAnimationEnd(Animation animation) {
            isRote = false;
        }

        @Override
        public void onAnimationRepeat(Animation animation) {

        }
    });
    startAnimation(rotateAnimation);
}

}

佈局檔案
<com.jia.lianxi4yuanandtoolbar.ChoujiangView
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”>
</com.jia.lianxi4yuanandtoolbar.ChoujiangView>