Android 可控制概率家务转盘

import android.widget.RelativeLayout;

import android.widget.TextView;

import android.widget.Toast;

public class HouseWorkWheel extends RelativeLayout {

private RelativeLayout relativeLayout;

private ImageView imagePointer;

private TextView text1, text2, text3, text4, text5, text6;

private Context context;

/** 指针转一圈所需要的时间 */

private static final long ONE_WHEEL_TIME = 500;

/** 开始转动时候的角度,初始值为0 */

private int degreeStart = 0;

/** 指针转圈圈数数据源 */

private int[] laps = { 3, 4, 5, 6 };

/** 指针所指向的角度数据 */

private int[] angles = { 0, 60, 120, 180, 240, 300 };

/** 转盘内容数组 */

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

/** 监听动画状态的监听器 */

private AnimationListener animationListener = new AnimationListener() {

@Override

public void onAnimationStart(Animation animation) {

}

@Override

public void onAnimationRepeat(Animation animation) {

}

@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; // 计算动画播放总时间

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

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

rotateAnimation.setInterpolator(context,

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

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

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

}

});

}

}

Update:控制概率

/** 转盘内容数组 */

priva
te 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;

最后

小编这些年深知大多数初中级Android工程师,想要提升自己,往往是自己摸索成长,自己不成体系的自学效果低效漫长且无助

因此我收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友。

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人

都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

资料⬅专栏获取
ount[1]++;

break;

最后

小编这些年深知大多数初中级Android工程师,想要提升自己,往往是自己摸索成长,自己不成体系的自学效果低效漫长且无助

因此我收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友。

[外链图片转存中…(img-lHVZwdtG-1718822289902)]

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人

都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

资料⬅专栏获取

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值