Android简单实现圆盘抽奖界面
作者:andywuchuanlong 时间:2022-07-25 08:35:27
闲来无事,做了一个简单的抽奖转盘的ui实现,供大家参考
package com.microchange.lucky;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
public class HalfCircle extends View {
private Paint paint;
private RectF oval;
private float startAngle;
private float sweepSpeed;
private float sweepAngle;
boolean useCenter;
int count;// 等份
@Override
protected void onDraw(Canvas canvas) {
setSweepAngle(count);
while (startAngle <= 360) {
if (startAngle % (count*3) == 0) {
paint.setColor(Color.BLUE);
} else if (startAngle % (count*2) == 0){
paint.setColor(Color.GREEN);
}else {
paint.setColor(Color.RED);
}
Log.e(""+startAngle, paint.getColor()+"");
canvas.drawArc(oval, startAngle, sweepAngle, useCenter, paint);
startAngle += count;
}
float centerX = oval.centerX();
float centerY = oval.centerY();
paint.setColor(Color.WHITE);
// paint.setStrokeWidth(5);
// paint.setStyle(Paint.Style.STROKE); //设置空心
paint.setAntiAlias(true); //消除锯齿
canvas.drawCircle(centerX, centerY, 50, paint);
String text = "奖";
paint.setTextSize(20 * getContext().getResources().getDisplayMetrics().density);
float measureText = paint.measureText(text);
float textY = paint.descent() - paint.ascent();
paint.setColor(Color.RED);
// canvas.drawLine(0, centerY, 480, centerY, paint);
// canvas.drawText(text, centerX-(measureText/2), centerY, paint);
canvas.drawText(text, centerX-(measureText/2), centerY+(textY/4), paint);
}
private void init() {
paint = new Paint();
paint.setColor(Color.BLUE);
paint.setAntiAlias(true);
paint.setStrokeWidth(5);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
return super.onTouchEvent(event);
}
/**
* @return the count
*/
public int getCount() {
return count;
}
/**
* @param count the count to set
*/
public void setCount(int count) {
this.count = 360 / count;
}
public Paint getPaint() {
return paint;
}
public void setPaint(Paint paint) {
this.paint = paint;
}
public RectF getOval() {
return oval;
}
public void setOval(RectF oval) {
this.oval = oval;
}
public float getStartAngle() {
return startAngle;
}
public void setStartAngle(float startAngle) {
this.startAngle = startAngle;
}
public float getSweepSpeed() {
return sweepSpeed;
}
public void setSweepSpeed(float sweepSpeed) {
this.sweepSpeed = sweepSpeed;
}
public float getSweepAngle() {
return sweepAngle;
}
public void setSweepAngle(float sweepAngle) {
this.sweepAngle = sweepAngle;
}
public boolean isUseCenter() {
return useCenter;
}
public void setUseCenter(boolean useCenter) {
this.useCenter = useCenter;
}
public HalfCircle(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
public HalfCircle(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public HalfCircle(Context context) {
this(context, null, 0);
}
}
package com.microchange.lucky;
import android.app.Activity;
import android.graphics.RectF;
import android.os.Bundle;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.Interpolator;
import android.view.animation.RotateAnimation;
public class MainActivity extends Activity {
RectF rect;
int radius = 300;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
HalfCircle circle = new HalfCircle(getApplicationContext());
circle.setOval(getRectF());
// circle.setStartAngle(90);
circle.setUseCenter(true);
circle.setCount(9);
Animation animation = new RotateAnimation(0, 135*10, getRectF().centerX(), getRectF().centerY());
animation.setDuration(5000);
animation.setInterpolator(new DecelerateInterpolator());
// animation.setRepeatCount(-1);
circle.setAnimation(animation );
// animation.start();
setContentView(circle);
}
public RectF getRectF(){
if (rect==null){
// getWindow().getDecorView().getWidth()
int width = getResources().getDisplayMetrics().widthPixels;
int height = getResources().getDisplayMetrics().heightPixels;
int top = (height - radius)/2;
int left = (width - radius)/2;
rect = new RectF(left, top, left+radius, top+radius);
}
return rect;
}
}
希望本文所述对大家学习Android程序设计有所帮助。
标签:Android,抽奖
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Android 通过productFlavors实现多渠道打包方法示例
2022-08-27 16:04:35
![](https://img.aspxhome.com/file/2023/5/91715_0s.png)
Android自定义控件仿QQ编辑和选取圆形头像
2022-11-10 09:40:37
![](https://img.aspxhome.com/file/2023/2/91012_0s.gif)
java web服务器实现跨域访问
2023-09-17 06:55:08
Android切面编程知识点详解
2023-05-05 04:18:04
Springboot整合微信支付(订单过期取消及商户主动查单)
2023-05-15 23:40:50
![](https://img.aspxhome.com/file/2023/1/62511_0s.png)
Java DecimalFormat 保留小数位及四舍五入的陷阱介绍
2023-11-09 04:49:33
Spring Boot mybatis-config 和 log4j 输出sql 日志的方式
2021-05-31 22:13:20
![](https://img.aspxhome.com/file/2023/6/101756_0s.png)
Springboot项目中使用redis的配置详解
2021-11-26 03:43:44
![](https://img.aspxhome.com/file/2023/7/70367_0s.png)
JAVA实现的简单万年历代码
2021-10-02 21:35:34
Android 中通过实现线程更新Progressdialog (对话进度条)
2023-04-16 14:57:50
![](https://img.aspxhome.com/file/2023/4/95004_0s.gif)
详解获取Spring MVC中所有RequestMapping以及对应方法和参数
2023-12-09 21:29:17
Java实现二分查找的变种
2023-11-07 11:26:28
![](https://img.aspxhome.com/file/2023/6/85556_0s.jpg)
C#中的WebRequest与WebResponse抽象类、DNS静态类、Ping类介绍
2022-12-28 05:22:15
Mybatis一对多查询的两种姿势(值得收藏)
2023-07-01 00:20:08
![](https://img.aspxhome.com/file/2023/9/61669_0s.png)
java教程之java注解annotation使用方法
2023-11-13 20:18:57
C语言实现两个矩阵相乘
2023-07-22 12:41:20
![](https://img.aspxhome.com/file/2023/6/103076_0s.png)
Spring五大类注解读取存储Bean对象的方法
2023-11-09 22:07:07
![](https://img.aspxhome.com/file/2023/3/59003_0s.png)
基于ArrayList源码解析(基于JDK1.8)
2021-10-07 18:34:25
![](https://img.aspxhome.com/file/2023/0/71890_0s.png)
mybatis 映射文件中if标签判断字符串相等的两种方式
2023-08-02 16:06:44
java数据结构和算法学习之汉诺塔示例
2021-09-28 14:46:18