1. 随机数组元素排序算法的实现
想通过对一个数组元素随机排序,从而实现上部分按钮图片的随机排布,但网上的所有类似算法都试过,没有一个可以正常运行的,基本都是一运行app强制停止,最后只能使用最蠢的算法来实现,虽然代码效率不高,但精确度是肯定的,但只适用于数组元素很少的情况下哦,实在不会随机排序的可以稍微看下这个最蠢的算法。
int temp;
Random rand=new Random();
images[0]=rand.nextInt(4); //获取到第一个随机量
while(true){
rand=new Random();
temp=rand.nextInt(4);
if(temp!=images[0]){
images[1]=temp; //获取第二个随机量
break;
}
}
while(true){
rand=new Random();
temp=rand.nextInt(4);
if(temp!=images[0] && temp!=images[1]){
images[2]=temp; //获取到第三个随机量
break;
}
}
while(true){
rand=new Random();
temp=rand.nextInt(4);
if(temp!=images[0] && temp!=images[1] && temp!=images[2]){
images[3]=temp; //获取到第四个随机量
break;
}
}
2.在java中对ImageView控件进行清空操作
ImageView.setImageResource(0);
3.将图片地址或控件id定义为一个数组操作
private Integer[] Btn_ids=new Integer[]
{R.id.fir_btn,R.id.sec_btn,R.id.thi_btn,R.id.for_btn,R.id.main_btn,R.id.bottom_btn};
主要就用Interger[]这个数组类型来定义,赋值方式遵循java语言的规律来就行了。