自定义大转盘(简陋版)

  1. 自定义大转盘
    写一个.class文件,布局里面用包名点到就可以了.
    简易的效果

  2. 布局

    <jt.com.fan.FanView
        android:id="@+id/fanView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
    
  3. MyClass( 自定义类 )

package jt.com.fan;

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

import java.util.ArrayList;

public class FanView extends View implements View.OnClickListener {

    private Paint mFanPaint , mTextPaint ;
    private ArrayList<Fan> mFanList = new ArrayList<Fan>();
    private RotateAnimation mRotateAnimation;//控件旋转动画

    private boolean isStartAnim;//是否已经启动了动画


    public FanView(Context context) {
        super(context);
        init();
    }

    public FanView(Context context,   AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public FanView(Context context,   AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        //初始化
        //画笔
        mFanPaint = new Paint();
        mFanPaint.setStyle(Paint.Style.FILL);
        //扇形中的文字
        mTextPaint = new Paint();
        mTextPaint.setTextSize(60);
        //单击事件
        setOnClickListener(this);
    }
    public void addFan(Fan fan){
        //添加bean对象
        mFanList.add(fan);
        //初始化
        invalidate();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //定义矩形区域用于画环形,圆,椭圆;
        RectF rectF = new RectF();
        rectF.left = 0 ;
        rectF.top = 0;
        rectF.right = 600;
        rectF.bottom = 600 ;
        //fori循环画扇形
        for (int i = 0; i < mFanList.size(); i++) {
            Fan fan = mFanList.get(i);
            //扇形*****颜色
            mFanPaint.setColor(fan.getColor());
            //画扇形
            canvas.drawArc(rectF,fan.getStartAngle(),360/mFanList.size(),true,mFanPaint);
            //文字画到扇形
            Path path = new Path();
            //定义文字区域,需要传入开始角度和结束角度
            path.addArc(rectF,fan.getStartAngle(),360/mFanList.size());
            mTextPaint.setColor(Color.BLACK);
            //沿路径path画文字,第一个参数是文字,第二个参数是路径,
            // 第三个参数是水平距离,也就是扇形的起点边界
            //第四个参数是纵向距离
            canvas.drawTextOnPath(fan.getName(),path,90,60,mTextPaint);
        }
        mFanPaint.setColor(Color.WHITE);//我把中间图片为白色
        canvas.drawCircle(300,300,150,mFanPaint);
        mTextPaint.setColor(Color.BLACK);//我把字体为黑色
        canvas.drawText("sssss",300-mTextPaint.measureText("Start")/2,
                300+mTextPaint.getTextSize()/2,mTextPaint);//画中间文字
    }

    @Override
    public void onClick(View v) {
        if (isStartAnim){
            this.clearAnimation();//清除动画
            mRotateAnimation = new RotateAnimation(0,(int)(360*Math.random()),300,300);
            mRotateAnimation.setDuration(100);//动画时间
            mRotateAnimation.setRepeatCount(0);//重复次数
            mRotateAnimation.setFillEnabled(true);//动画执行保留目前的状态
            mRotateAnimation.setFillAfter(true);//动画执行结束保留目前的状态
            startAnimation(mRotateAnimation);//开启
            isStartAnim =false;//设置变量根据(if条件判断)
        }else{
            mRotateAnimation = new RotateAnimation(0,360,300,300);
            mRotateAnimation.setDuration(1000);//动画时间
            mRotateAnimation.setRepeatCount(-1);//重复次数
            mRotateAnimation.setInterpolator(new LinearInterpolator());//差速器:匀速执行
            startAnimation(mRotateAnimation);//开启
            isStartAnim = true;//设置变量根据(if条件判断)
        }
    }
}
  1. MyBean(看情况 我写的是开始角度,扇形上的文字,以及扇形颜色)
package jt.com.fan;

public class Fan {

    //开始角度
    int startAngle;
    //扇形的名字(显示于扇形中的文字)
    String name;
    //颜色
    int color;

    //set/get方法
    public Fan(int startAngle, String name, int color) {
        this.startAngle = startAngle;
        this.name = name;
        this.color = color;
    }

    public int getStartAngle() {
        return startAngle;
    }

    public void setStartAngle(int startAngle) {
        this.startAngle = startAngle;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getColor() {
        return color;
    }

    public void setColor(int color) {
        this.color = color;
    }
}
  1. 效果实现
    在这里插入图片描述
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值