自定义view画抽奖转盘

首先自已创建个类继承view

package com.example.zhuzhuangtu.weight;

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.view.View;
import android.view.animation.RotateAnimation;

import java.util.Random;

public class MyView extends View implements View.OnClickListener {
    private Paint mPaint;
    private int mWidth;
    //private int mPadding;
    private RectF mRectF;
    private String mStr = "开始";
    private float kaishijiaodu;
    //1转盘首先想好几个扇形并创建数组
    private String[] contents = new String[]{"美 女", "女 神", "热 舞", "丰 满", "性 感", "知 性"};
    //2创建颜色数组
    public int[] colors = new int[]{Color.parseColor("#8EE5EE"), Color.parseColor("#FFD700"), Color.parseColor("#FFD39B"), Color.parseColor("#FF8247"), Color.parseColor("#FF34B3"), Color.parseColor("#F0E68C")};
    public MyView(Context context, AttributeSet attrs) {
        super(context, attrs);
        //实例化画笔对象
        mPaint = new Paint();
        //设置点击开的旋转的监听
        setOnClickListener(this);
    }
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //绘制 6个椭圆
        initArc(canvas);
        //给小圆填充颜色
        mPaint.setColor(Color.RED);
        //让圆边更加圆滑
        mPaint.setStyle(Paint.Style.FILL);
        //绘画小圆
        canvas.drawCircle(mWidth / 2, mWidth / 2, 50, mPaint);
        //设置小圆里的字体颜色
        mPaint.setColor(Color.WHITE);

        //设置小圆里的字是否居中
        Rect rect = new Rect();
        mPaint.getTextBounds(mStr, 0, mStr.length(), rect);
        int width = rect.width();
        int height = rect.height();
        canvas.drawText(mStr, mWidth / 2 - width / 2, mWidth / 2 + height / 2, mPaint);
    }
    private void initArc(Canvas canvas) {

        for (int i = 0; i < 6; i++) {
            //依次根据i来设置6个扇形的颜色
            mPaint.setColor(colors[i]);
            //绘制圆弧,含圆心
            canvas.drawArc(mRectF, 60*i, 60, true, mPaint);
        }
        for (int i = 0; i < 6; i++) {
            //设置6个扇形的字体颜色
            mPaint.setColor(Color.BLACK);
            mPaint.setTextSize(24);
            Path path = new Path();
            //创建path把小圆上的字
            path.addArc(mRectF, 60*i, 60);
            //都保存添加到圆上
            canvas.drawTextOnPath(contents[i], path, 60, 60, mPaint);
        }
    }
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        setMeasuredDimension(300, 300);
        mWidth = getWidth();
        //mPadding = 1;
        initRectf();
    }
    private void initRectf() {
        //initrectf既可以画矩形,也可以画圆,而rect不可以画圆
        mRectF = new RectF(0, 0, mWidth, mWidth);
    }
    @Override
    public void onClick(View v) {
        Random random = new Random();
        int f =random.nextInt(1000);
        RotateAnimation rotateAnimation = new RotateAnimation(kaishijiaodu, f+360,mWidth/2,mWidth/2);
        //旋转时间
        rotateAnimation.setDuration(3000);
        //执行完后的位置
        rotateAnimation.setFillAfter(true);
        startAnimation(rotateAnimation);
        kaishijiaodu=f%360;
    }
}

然后在main的布局里调用自己创建的类名

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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.example.zhuzhuangtu.weight.MyView
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值