Android 可控制概率家务转盘,Android码农是如何进入腾讯的

本文介绍了如何在Android中实现一个可控制概率的家务转盘,详细展示了代码实现过程,包括转盘动画和概率计算。同时,文章提及Android开发者如何通过面试进入腾讯工作。
摘要由CSDN通过智能技术生成

}

@Override

public void onAnimationEnd(Animation animation) {

String name = arrayHouseWork[degreeStart % 360 / 60];

Toast.makeText(context, name, Toast.LENGTH_SHORT).show();

}

};

public HouseWorkWheel(Context context, AttributeSet attrs, int defStyle) {

super(context, attrs, defStyle);

initialView(context);

this.context = context;

}

public HouseWorkWheel(Context context, AttributeSet attrs) {

this(context, attrs, 0);

}

public HouseWorkWheel(Context context) {

this(context, null);

}

private void initialView(final Context context) {

if (relativeLayout == null) {

LayoutInflater inflater = (LayoutInflater) context

.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

relativeLayout = (RelativeLayout) inflater.inflate(

R.layout.view_lucky_wheel, this);

}

text1 = (TextView) findViewById(R.id.text1);

text1.setText(arrayHouseWork[0]);

text2 = (TextView) findViewById(R.id.text2);

text2.setText(arrayHouseWork[1]);

text3 = (TextView) findViewById(R.id.text3);

text3.setText(arrayHouseWork[2]);

text4 = (TextView) findViewById(R.id.text4);

text4.setText(arrayHouseWork[3]);

text5 = (TextView) findViewById(R.id.text5);

text5.setText(arrayHouseWork[4]);

text6 = (TextView) findViewById(R.id.text6);

text6.setText(arrayHouseWork[5]);

imagePointer = (ImageView) relativeLayout

.findViewById(R.id.imagePointer);

imagePointer.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

int lap = laps[(int) (Math.random() * 4)];

int angle = angles[(int) (Math.random() * 6)];

int degreeIncrease = lap * 360 + angle; // 每次转圈角度增量

// 初始化旋转动画

RotateAnimation rotateAnimation = new RotateAnimation(

degreeStart, degreeStart + degreeIncrease,

RotateAnimation.RELATIVE_TO_SELF, 0.5f,

RotateAnimation.RELATIVE_TO_SELF, 0.5f);

long time = (lap + angle / 360) * ONE_WHEEL_TIME; //

《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》

浏览器打开:qq.cn.hn/FTe 开源分享

计算动画播放总时间

rotateAnimation.setDuration(time); // 设置动画播放时间

rotateAnimation.setFillAfter(true); // 设置动画播放完后,停留在最后一帧画面上

rotateAnimation.setInterpolator(context,

android.R.anim.accelerate_decelerate_interpolator); // 设置动画的加速行为,是先加速后减速

rotateAnimation.setAnimationListener(animationListener); // 设置动画的监听器

imagePointer.startAnimation(rotateAnimation); // 开始播放动画

}

});

}

}

Update:控制概率

/** 转盘内容数组 */

private String[] arrayHouseWork = { “扫地”, “洗衣服”, “休息”, “做饭”, “洗碗”, “休息” };

/** 概率数组(千分之) */

private int[] arrayProbability = { 10, 10, 480, 10, 10, 480 };

private int countProbability(int[] arrayProbability) {

int temNumber = 1 + new Random().nextInt(1000); // 1-1000

int returnNumber;

if (temNumber <= arrayProbability[0]) {

returnNumber = 0;

} else if (temNumber <= arrayProbability[0] + arrayProbability[1]) {

returnNumber = 1;

} else if (temNumber <= arrayProbability[0] + arrayProbability[1]

  • arrayProbability[2]) {

returnNumber = 2;

} else if (temNumber <= arrayProbability[0] + arrayProbability[1]

  • arrayProbability[2] + arrayProbability[3]) {

returnNumber = 3;

} else if (temNumber <= arrayProbability[0] + arrayProbability[1]

  • arrayProbability[2] + arrayProbability[3]

  • arrayProbability[4]) {

returnNumber = 4;

} else {

returnNumber = 5;

}

Log.v(“AZ”, (returnNumber + 1) + “-” + temNumber);

return returnNumber;

}

执行10000次:

int[] arrayCount = { 0, 0, 0, 0, 0, 0 };

for (int i = 0; i < 10000; i++) {

int temp = countProbability(arrayProbability);

switch (temp) {

case 0:

arrayCount[0]++;

break;

case 1:

arrayCount[1]++;

break;

case 2:

arrayCount[2]++;

break;

case 3:

arrayCount[3]++;

break;

case 4:

arrayCount[4]++;

break;

case 5:

arrayCount[5]++;

break;

default:

break;

}

}

Log.v(“AZ”, arrayCount[0] + “\t” + arrayCount[1] + “\t” + arrayCount[2]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值