自定义View-幸运轮盘

因为在有些时候Android Studio中提供的控件不能满足我们的功能需求,但是我们又要做一个幸运大轮盘的功能,所以这时候我们需要用到我们Android中的一个知识点--------自定义View

在写代码之前我们先来理一下这个思路

1.定义数据
2. 画圆
3. 画弧并设置颜色区分
4. 在每个弧度上将数据放到上面
5. 设置指针选到的位置
6. 动画—用到旋转动画将我们的自定义View旋转起来
7. 定义随机数代表每次转到的次数

这是我的布局


这里是自定义View的代码

public class View_DLP extends View implements View.OnClickListener{
private String[] contents = new String[]{“美 女”, “女 神”, “热 舞”, “丰 满”, “性 感”, “知 性”};
public int[] colors = new int[]{Color.parseColor("#8EE5EE"), Color.parseColor("#FFD700"), Color.parseColor("#FFD39B"), Color.parseColor("#FF8247"), Color.parseColor("#FF34B3"), Color.parseColor("#F0E68C")};
public String centerText = “start”;
private int mWidth;
private Paint mPaint;
private Context mContext;
private RotateAnimation rotateAnimation;
private float newdul;

public View_DLP(Context context, @Nullable AttributeSet attrs) {super(context, attrs);this.mContext = context;mPaint = new Paint();setOnClickListener(this);
}@Override
protected void onDraw(Canvas canvas) {super.onDraw(canvas);//设置画笔颜色mPaint.setColor(Color.YELLOW);//画圆canvas.drawCircle(mWidth / 2, mWidth / 2, mWidth / 2, mPaint);//写一个跟圆一样占满全屏RectF rectF = new RectF(0, 0, mWidth, mWidth);//再次定义一下画笔mPaint.setStyle(Paint.Style.FILL);//设置弧和弧的颜色for (int i = 0; i < colors.length; i++) {//设置弧的颜色为我们数组中定义的颜色mPaint.setColor(colors[i]);//弧度设置为60int startjd = i * 60;canvas.drawArc(rectF, startjd, 60, true, mPaint);}//再次定义一下画笔mPaint.setColor(Color.RED);mPaint.setTextSize(25);//设置数据for (int i = 0; i < contents.length; i++) {int startjd = i * 60;Path path = new Path();//文字位置 rectF ,startjd 所占的角度  , 60  文字所占大小path.addArc(rectF,startjd,60);//文字水平和垂直方向的偏移量canvas.drawTextOnPath(contents[i],path,50,50,mPaint);}mPaint.setColor(Color.GREEN);canvas.drawCircle(mWidth / 2,mWidth / 2, 50,mPaint);mPaint.setColor(Color.BLACK);mPaint.setTextSize(24);Rect rect = new Rect();mPaint.getTextBounds(centerText, 0, centerText.length(), rect);int textWidth = rect.width();int textHeight = rect.height();canvas.drawText(centerText, mWidth / 2 - textWidth / 2, mWidth / 2 + textHeight / 2, mPaint);
}//测量宽高的方法
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {//super.onMeasure(widthMeasureSpec, heightMeasureSpec);setMeasuredDimension(300, 300);//得到测量过后的宽和高      注意这是得到测量过后的宽和高使用getMeasuredWidth()方法mWidth = getMeasuredWidth();
}@Override
public void onClick(View view) {//Random  定义一个随机数Random random = new Random();//nextInt  定义随机数取0-1000之间float dul = random.nextInt(1000);//其实在这里如果不加这个值的话  可能会出现随机顺时针逆时针的情况   然后下边是解决方案//rotateAnimation = new RotateAnimation(newdul, dul, mWidth / 2,mWidth / 2);rotateAnimation = new RotateAnimation(newdul, dul+1000, mWidth / 2, mWidth / 2);rotateAnimation.setFillAfter(true);rotateAnimation.setDuration(3000);startAnimation(rotateAnimation);//将随机数赋值给这个成员变量   便于下次点击在结束的地方开始抽奖newdul = dul % 360 +1000;} 

}

如果还没有看懂的话可以参考代码
地址:https://github.com/qq1341738311/zdyView_TheLuckyRoller


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部