android自定义抽奖,android 自定义View轮盘抽奖

这个博客展示了如何在Android中创建一个自定义视图`ChoujiangView`,实现抽奖转盘的功能。视图包括绘制多个颜色的扇形区域、中心圆以及文字描述。还包含了旋转动画的初始化和控制,以及点击事件监听来开始和结束旋转。
摘要由CSDN通过智能技术生成

package com.bawie.customerview;

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 screenWith;//屏幕宽度

private int screenHeigh;//屏幕高度

private Paint mPaint;//画笔工具

private int centerX;

private int centerY;

private int[] colors;

private RotateAnimation rotateAnimation;

private String[] desc = new String[]{"性感", "丰满", "知性", "聪明", "贤惠", "优秀"};

private boolean isRote;//是否在旋转状态

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();

screenWith = displayMetrics.widthPixels;

screenHeigh = displayMetrics.heightPixels;

//获取屏幕中心坐标

centerX = screenWith / 2;

centerY = screenHeigh / 2;

//初始化画笔

initPaint();

colors = new int[]{Color.RED, Color.GRAY, Color.YELLOW, Color.BLUE, Color.GREEN, Color.DKGRAY, Color.WHITE};

//初始化旋转动画

initAnimation();

//给自己添加点击事件

this.setOnClickListener(this);

}

//测量View大小

@Override

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

super.onMeasure(600, 600);

}

//绘图

@Override

protected void onDraw(Canvas canvas) {

super.onDraw(canvas);

//移动画布的坐标原点

canvas.translate(centerX, centerY);

//绘制6个圆弧

RectF rect = new RectF(-300, -300, 300, 300);

float start = 60;

for (int i = 0; i < 6; i++) {

mPaint.setColor(colors[i]);

canvas.drawArc(rect, start * i, 60, true, mPaint);

}

//绘制中心的圆

mPaint.setColor(Color.RED);

canvas.drawCircle(0, 0, 100, mPaint);

mPaint.setColor(Color.WHITE);

mPaint.setTextSize(40);

//获取文字宽度和高度

Rect rectText = new Rect();

mPaint.getTextBounds("start", 0, 5, rectText);

int width = rectText.width();

int height = rectText.height();

canvas.drawText("start", -width / 2, height / 2, mPaint);

//绘制描述信息

RectF rectF = new RectF(-200, -200, 200, 200);

for (int i = 0; i < 6; i++) {

mPaint.setColor(Color.WHITE);

Path path = new Path();

path.addArc(rectF, start * i + 15, 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);

}

private void initAnimation() {

rotateAnimation = new RotateAnimation(0, 360,

centerX, centerY);

rotateAnimation.setDuration(800);

rotateAnimation.setFillAfter(true);

rotateAnimation.setRepeatCount(-1);

rotateAnimation.setInterpolator(new LinearInterpolator());

rotateAnimation.setRepeatMode(Animation.RESTART);

}

private void startAnima() {

isRote=true;

startAnimation(rotateAnimation);

}

private void stopAnima() {

isRote=false;

clearAnimation();

}

@Override

public void onClick(View v) {

if (isRote) {

stopAnima();

setRoundDom();

}else {

startAnima();

}

}

//给一个随机的抽奖结果

private void setRoundDom(){

double random = Math.random();

RotateAnimation  rotateAnimation2 = new RotateAnimation(0, (float) (360*random),

centerX, centerY);

rotateAnimation2.setDuration(100);

rotateAnimation2.setFillAfter(true);

startAnimation(rotateAnimation2);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值