314_抽签器项目完整代码
抽签器项目完整代码
public class MainActivity extends AppCompatActivity {
private LotterView lotterView;
private PointerView pointerView;
private EditText et1;
private EditText et2;
private EditText et3;
private EditText et4;
private EditText et5;
private EditText et6;
private EditText et7;
private EditText et8;
private ArrayList<String> list = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lotterView = (LotterView) findViewById(R.id.lotterView);
pointerView = (PointerView) findViewById(R.id.pointerView);
for (int i = 1; i <= 5; i++) {
String string = "选项" + i;
list.add(string);
}
lotterView.setData(list);
lotterView.invalidate();
}
public void set(View view) {
AlertDialog dialog = new AlertDialog.Builder(this).create();
dialog.show();
dialog.getWindow().setContentView(R.layout.dialog_setting);
dialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
et1 = (EditText) dialog.findViewById(R.id.et1);
et2 = (EditText) dialog.findViewById(R.id.et2);
et3 = (EditText) dialog.findViewById(R.id.et3);
et4 = (EditText) dialog.findViewById(R.id.et4);
et5 = (EditText) dialog.findViewById(R.id.et5);
et6 = (EditText) dialog.findViewById(R.id.et6);
et7 = (EditText) dialog.findViewById(R.id.et7);
et8 = (EditText) dialog.findViewById(R.id.et8);
Button btn = (Button) dialog.findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
list.clear();
handleEdittext(et1);
handleEdittext(et2);
handleEdittext(et3);
handleEdittext(et4);
handleEdittext(et5);
handleEdittext(et6);
handleEdittext(et7);
handleEdittext(et8);
lotterView.setData(list);
lotterView.invalidate();
}
});
setEdittextString();
}
private void setEdittextString() {
for (int i = 1; i <= list.size(); i++) {
getEdittext(i).setText(list.get(i - 1));
}
}
private void handleEdittext(EditText editText) {
String string = editText.getText().toString().trim();
if (string != null && !string.isEmpty()) {
list.add(string);
}
}
private EditText getEdittext(int i) {
switch (i) {
case 1:
return et1;
case 2:
return et2;
case 3:
return et3;
case 4:
return et4;
case 5:
return et5;
case 6:
return et6;
case 7:
return et7;
case 8:
return et8;
}
return null;
}
public void start(View view) {
int size = list.size();
if (size == 0) {
return;
}
//计算每个角度
float angle = 360 / size;
//5个angle是一圈,最起码来个5圈
double random = Math.random();
double v = random * size + 1;
float rotateAngle = 360f * size + angle * (float) v;
//创建旋转动画
RotateAnimation animation = new RotateAnimation(0, rotateAngle, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
//持续5秒,结束后保持状态
animation.setDuration(5000);
animation.setFillAfter(true);
pointerView.startAnimation(animation);
}
}
public class LotterView extends View {
private ArrayList<String> list = new ArrayList<>();
public LotterView(Context context) {
this(context, null);
}
public LotterView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public LotterView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, widthMeasureSpec);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
int size = list.size();
//获取宽高
int width = getWidth();
int height = getHeight();
//初始化数据,半径,颜色,圆环宽度
int radius = width / 2;
int ringWidth = 15;
int tempWidth = ringWidth / 2;
//初始化数据
float amount = size;
float angle = 360f / amount;
//创建画笔
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(ringWidth);
paint.setColor(Color.BLACK);
//创建画圆弧要用的RectF
RectF rectF = new RectF(tempWidth, tempWidth, width - tempWidth, width - tempWidth);
float angleSum = 0;
for (int i = 1; i <= amount; i++) {
canvas.drawArc(rectF, angleSum, angle, true, paint);
double x = radius + getRoundX(radius / 3 * 2, i - 1, (int) amount, 0 + angle / 2);
double y = radius + getRoundY(radius / 3 * 2, i - 1, (int) amount, 0 + angle / 2);
Paint textPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
textPaint.setColor(Color.BLACK);
int textSize = 50;
textPaint.setTextSize(textSize);
String str = list.get(i - 1);
Rect rect = new Rect();
textPaint.getTextBounds(str, 0, str.length(), rect);
int strW = rect.width();
int strH = rect.height();
canvas.drawText(str, (float) x - (strW + ringWidth) / 2, (float) ((float) y - strW / 2 + textSize + ringWidth), textPaint);
angleSum += angle;
}
}
private double getRoundY(float r, int i, int n, float offset_angle) {
return r * Math.sin(i * 2 * Math.PI / n + Math.PI / 180
* offset_angle);
}
private double getRoundX(float r, int i, int n, float offset_angle) {
return r * Math.cos(i * 2 * Math.PI / n + Math.PI / 180
* offset_angle);
}
public void setData(ArrayList<String> list) {
this.list = list;
}
}
public class PointerView extends View {
private ArrayList<String> list = new ArrayList<>();
public PointerView(Context context) {
this(context, null);
}
public PointerView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public PointerView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, widthMeasureSpec);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//获取宽高
int width = getWidth();
int height = getHeight();
int radius = 50;
//画圆
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setColor(Color.RED);
canvas.drawCircle(width / 2, width / 2, radius, paint);
//画三角形
Path path = new Path();
path.moveTo(width / 2 - radius, width / 2);
path.lineTo(width / 2 + radius, width / 2);
path.lineTo(width / 2, width / 2 - radius * 2);
path.close();
canvas.drawPath(path, paint);
}
public void setData(ArrayList<String> list) {
this.list = list;
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1">
<com.lich.lichlotter.LotterView
android:id="@+id/lotterView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true" />
<com.lich.lichlotter.PointerView
android:id="@+id/pointerView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" />
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:onClick="set"
android:text="设置"
android:textSize="20sp" />
<Button
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:onClick="start"
android:text="开始"
android:textSize="20sp" />
</LinearLayout>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="200dp"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText
android:id="@+id/et1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<EditText
android:id="@+id/et2"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<EditText
android:id="@+id/et3"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<EditText
android:id="@+id/et4"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<EditText
android:id="@+id/et5"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<EditText
android:id="@+id/et6"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<EditText
android:id="@+id/et7"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<EditText
android:id="@+id/et8"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="确定" />
</LinearLayout>
</ScrollView>
314_抽签器项目完整代码相关推荐
- C++银行账户 项目完整代码
C++银行账户 项目完整代码,连续敲打5遍,就学会了. //date.h #ifndef __DATE_H__ #define __DATE_H__class Date { //日期类 privat ...
- P74-前端基础项目开发-首页main部分开发广告栏-项目完整代码
P74-前端基础项目开发-首页main部分开发广告栏-项目完整代码 1.概述 这篇文章是首页开发最后一个部分,也是这个项目的结束部分.通过这个项目练习让我们掌握了HTML+CSS的基础使用. 2.广告 ...
- 机票管理系统 python3 航班动态管理 文件存储 项目完整代码
机票管理系统(python3) 一.问题分析 问题分析:从数据的逻辑结构.数据存储以及对数据的操作三个方面出发分析.各数据的关系是集合:数据存在文件之中,在内存中用对象数组来存储:涉及对数据的增删改查 ...
- 狂神聊 ElasticSearch(IK分词器+Rest+集成SpringBoot+实战爬虫项目+完整代码及资料)
Bilibili 搜索关注:狂神说 Java(和狂神一起学习,共同进步) 公众号:狂神说(文章日更) 狂神聊 ElasticSearch 版本:ElasticSearch 7.6.1(全网最新了) 6 ...
- 第二十三章《斗地主游戏》第3节:项目完整代码
对于初学者来说,斗地主游戏是一个比较复杂的项目,它涉及的类很多,以下是这个项目所有类的源代码,源码中有两个Main.java文件,它们虽然文件名称相同,但位于不同的包下,读者在复制粘贴这两个文件时需要 ...
- 如何读取照片的GPS信息?—最好的语言Java实现起来就这么简单【手把手教程+完整代码】
大家好,我是Lex 喜欢欺负超人那个Lex 上次,我们用Python读取照片的GPS信息,可以获取拍摄时间.手机型号等信息,还可以对 拍摄地点进行精确定位.Java表示不服~~~ 今日重点:用Java ...
- Java实现智能语音朗读(完整代码+EXE程序制作)
注意 本文主要介绍整个实现过程,一步一步跟着做,相信大家都可以完成的比我好.想要完整代码的可以跳过文章直接去文末获取,如果想要在本机上运行,请首先搭建好环境,如何搭建环境在项目的第二阶段里面. 概述 ...
- 完整打造一个多功能音乐播放器项目(初步设想跟酷狗类似)
本人目前准备利用闲暇时间打造一个完整的音乐播放器项目,主要用于学习及分享!原创不易,转载请注明出处. 这是一个什么样的音乐播放器呢?整体的架构跟酷狗差不多吧,我的方式呢,是一个个组件一个个模块先做好, ...
- c语言编写音乐播放器完整代码(mciSendString函数的使用方法,第一次使用Visual Studio 2019的详细步骤)
本文会尽量让和笔者一样只有一点点可怜的c语言基础的小白看懂,并写出自己的音乐播放器. 笔者使用的编译器是Visual Studio 2019,直接在官网下载就行了.下面是Visual Studio 2 ...
最新文章
- 详解计算机视觉五大技术:图像分类、对象检测、目标跟踪、语义分割和实例分割
- 【python3】 抓取异常信息try/except
- vue如何发送网络请求,使用axios事半功倍!
- JAVA Web Servlet中的异步处理 (2) -- Servlet3.1中的Non-blocking IO支持
- spring mvc学习(19):cookievalue注解(显示cookie的值,默认必须有值)
- Python 并行编程
- smarty 简单分页
- php array 删除末尾,PHP array_pop():删除数组末尾的元素
- linux搭建博客day5-安装Mysql
- 秋叶一键重装系统连接服务器失败,秋叶一键重装系统win7系统安装和使用DAEMONToolsLite的方法【图文教程】...
- 2019年上海交通大学816自动控制理论考研经验分享
- 京瓷1125打印机清零_京瓷1125驱动下载
- VUE post请求下载文件
- 一个关于容抗与感抗的实验
- HDU 6438Buy and Resell
- 洛谷 UVA1395 苗条的生成树 Slim Span
- 财务软件“新物种”来了!唯你科技“精采智能”领势
- 注解与APT、JavaPoet
- fatal error LNK1104:
- 线下增加设备接口开发(SDK)