欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  技术分享

Android自定义随机抽奖例子_Android抽奖转盘实例

程序员文章站 2022-03-01 14:53:20
...

一个模拟抽奖的效果、用户设定若干个选项、添加之后、就可以通过程序

来帮助随机选择其中一项出来、这个类似超市里面那种指针转盘抽奖

run之后是一个动态效果图、初始快速转动、最后慢慢停在某个选项上

Android自定义随机抽奖例子_Android抽奖转盘实例

初始化代码


protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView)findViewById(R.id.tv);
tv1 = (TextView)findViewById(R.id.tv1);
tv2 = (TextView)findViewById(R.id.tv2);
tv3 = (TextView)findViewById(R.id.tv3);
tv4 = (TextView)findViewById(R.id.tv4);
tv5 = (TextView)findViewById(R.id.tv5);
tv6 = (TextView)findViewById(R.id.tv6);
tv7 = (TextView)findViewById(R.id.tv7);
tv8 = (TextView)findViewById(R.id.tv8);
tv9 = (TextView)findViewById(R.id.tv9);

tv.setText("dwtedx个人博客");
tv1.setText(string1);
tv2.setText(string2);
tv3.setText(string3);
tv4.setText(string4);
tv5.setText(string5);
tv6.setText(string6);
tv7.setText(string7);
tv8.setText(string8);
tv9.setText("Run");
 iv = (ImageView)findViewById(R.id.iv);
	
WindowManager wm=(WindowManager)getSystemService(Context.WINDOW_SERVICE);
//手机屏幕的宽度
width=getWindowManager().getDefaultDisplay().getWidth();
//手机屏幕的高度
height=getWindowManager().getDefaultDisplay().getHeight()+90;
	
wheel = BitmapFactory.decodeResource(getResources(), R.drawable.wheel);
wheel1 = BitmapFactory.decodeResource(getResources(), R.drawable.wheel1);
wheel2 = BitmapFactory.decodeResource(getResources(), R.drawable.wheel2);
wheel3 = BitmapFactory.decodeResource(getResources(), R.drawable.wheel3);
wheel4 = BitmapFactory.decodeResource(getResources(), R.drawable.wheel4);
wheel5 = BitmapFactory.decodeResource(getResources(), R.drawable.wheel5);
wheel6 = BitmapFactory.decodeResource(getResources(), R.drawable.wheel6);
wheel7 = BitmapFactory.decodeResource(getResources(), R.drawable.wheel7);
wheel8 = BitmapFactory.decodeResource(getResources(), R.drawable.wheel8);
}
实现抽奖的代码



Bundle bundle = new Bundle();
bundle = msg.getData();
int i = bundle.getInt("msg");
tv9.setText("RUN");
switch (i%8) {
case 0:
	tv.setText("哎,"+string8);
	break;
case 1:
	tv.setText("恭喜,"+string1+"同学,请吃饭咯!");
	break;
case 2:
	tv.setText("恭喜,"+string2+"同学,请吃饭咯!");
	break;
case 3:
	tv.setText("恭喜,"+string3+"同学,请吃饭咯!");
	break;
case 4:
	tv.setText("恭喜,"+string4+"同学,请吃饭咯!");
	break;
case 5:
	tv.setText("恭喜,"+string5+"同学,请吃饭咯!");
	break;
case 6:
	tv.setText("恭喜,"+string6+"同学,请吃饭咯!");
	break;
case 7:
	tv.setText("恭喜,"+string7+"同学,请吃饭咯!");
	break;
	
default:
	break;
}

源代码下载链接链接: http://dwtedx.com/download.html?bdkey=s/1gd3nsT9 密码: l4ik