嗨朋友们,我有两个数组列表,一个是图像数组列表,另一个是声音数组列表,我必须使用图像数组列表随机为图像视图提供背景,当我单击该图像时,我需要播放与图像相关的声音,两个数组列表现在都在改组,我可以随机设置图像背景,但我的问题出在声音上
public class MainActivity extends Activity implements OnClickListener {
float screenHeight, screenWidth, screendensity;
ImageView image1,image2,image3,image4,image5,image6;
static ArrayList<Integer> sltdalphabet=new ArrayList<Integer>();
static ArrayList<Integer> sounds=new ArrayList<Integer>();
Integer[] stringArray1;
Integer[] soundarray;
MediaPlayer filpsound;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
screenHeight = displaymetrics.heightPixels;
screenWidth = displaymetrics.widthPixels;
screendensity = displaymetrics.densityDpi;
setContentView(R.layout.flipcaps);
sounds.clear();
sltdalphabet.clear();
sltdalphabet.add(R.drawable.a_white);
sltdalphabet.add(R.drawable.b_white);
sltdalphabet.add(R.drawable.c_white);
sltdalphabet.add(R.drawable.d_white);
sltdalphabet.add(R.drawable.e_white);
sltdalphabet.add(R.drawable.f_white);
sounds.add(R.raw.a);
sounds.add(R.raw.b);
sounds.add(R.raw.c);
sounds.add(R.raw.d);
sounds.add(R.raw.e);
sounds.add(R.raw.f);
image1=(ImageView)findViewById(R.id.img1);
image1.setOnClickListener(this);
RelativeLayout.LayoutParams layoutflipbtn1copy = (RelativeLayout.LayoutParams) image1
.getLayoutParams();
layoutflipbtn1copy.height = (int) (170 * (screenHeight / 600));
layoutflipbtn1copy.width = (int) (170 * (screenWidth / 1024));
layoutflipbtn1copy.topMargin = (int) (120 * (screenHeight / 600));
layoutflipbtn1copy.leftMargin = (int) (240 * (screenWidth / 1024));
image2=(ImageView)findViewById(R.id.img2);
image2.setOnClickListener(this);
RelativeLayout.LayoutParams layoutflipbtn2copy = (RelativeLayout.LayoutParams) image2
.getLayoutParams();
layoutflipbtn2copy.height = (int) (170 * (screenHeight / 600));
layoutflipbtn2copy.width = (int) (170 * (screenWidth / 1024));
layoutflipbtn2copy.topMargin = (int) (120 * (screenHeight / 600));
layoutflipbtn2copy.leftMargin = (int) (430 * (screenWidth / 1024));
image3=(ImageView)findViewById(R.id.img3);
image3.setOnClickListener(this);
RelativeLayout.LayoutParams layoutflipbtn3copy = (RelativeLayout.LayoutParams) image3
.getLayoutParams();
layoutflipbtn3copy.height = (int) (170 * (screenHeight / 600));
layoutflipbtn3copy.width = (int) (170 * (screenWidth / 1024));
layoutflipbtn3copy.topMargin = (int) (120 * (screenHeight / 600));
layoutflipbtn3copy.leftMargin = (int) (620 * (screenWidth / 1024));
image4=(ImageView)findViewById(R.id.img4);
image4.setOnClickListener(this);
RelativeLayout.LayoutParams layoutflipbtn4copy = (RelativeLayout.LayoutParams) image4
.getLayoutParams();
layoutflipbtn4copy.height = (int) (170 * (screenHeight / 600));
layoutflipbtn4copy.width = (int) (170 * (screenWidth / 1024));
layoutflipbtn4copy.topMargin = (int) (300 * (screenHeight / 600));
layoutflipbtn4copy.leftMargin = (int) (240 * (screenWidth / 1024));
image5=(ImageView)findViewById(R.id.img5);
image5.setOnClickListener(this);
RelativeLayout.LayoutParams layoutflipbtn5copy = (RelativeLayout.LayoutParams) image5
.getLayoutParams();
layoutflipbtn5copy.height = (int) (170 * (screenHeight / 600));
layoutflipbtn5copy.width = (int) (170 * (screenWidth / 1024));
layoutflipbtn5copy.topMargin = (int) (300 * (screenHeight / 600));
layoutflipbtn5copy.leftMargin = (int) (430 * (screenWidth / 1024));
image6=(ImageView)findViewById(R.id.img6);
image6.setOnClickListener(this);
RelativeLayout.LayoutParams layoutflipbtn6copy = (RelativeLayout.LayoutParams) image6
.getLayoutParams();
layoutflipbtn6copy.height = (int) (170 * (screenHeight / 600));
layoutflipbtn6copy.width = (int) (170 * (screenWidth / 1024));
layoutflipbtn6copy.topMargin = (int) (300 * (screenHeight / 600));
layoutflipbtn6copy.leftMargin = (int) (620 * (screenWidth / 1024));
Collections.shuffle(sltdalphabet);
Collections.shuffle(sounds);
stringArray1 = sltdalphabet.toArray(new Integer[sltdalphabet.size()]);
soundarray = sounds.toArray(new Integer[sounds.size()]);
image1.setBackgroundResource(stringArray1[0]);
image2.setBackgroundResource(stringArray1[1]);
image3.setBackgroundResource(stringArray1[2]);
image4.setBackgroundResource(stringArray1[3]);
image5.setBackgroundResource(stringArray1[4]);
image6.setBackgroundResource(stringArray1[5]);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(v==image1)
{
if (filpsound == null) {
filpsound = MediaPlayer.create(getApplicationContext(),
soundarray[0]);
}
filpsound.start();
}
else if(v==image2)
{
if (filpsound == null) {
filpsound = MediaPlayer.create(getApplicationContext(),
soundarray[1]);
}
filpsound.start();
}
else if(v==image3)
{
if (filpsound == null) {
filpsound = MediaPlayer.create(getApplicationContext(),
soundarray[2]);
}
filpsound.start();
}
else if(v==image4)
{
if (filpsound == null) {
filpsound = MediaPlayer.create(getApplicationContext(),
soundarray[3]);
}
filpsound.start();
}
else if(v==image5)
{
if (filpsound == null) {
filpsound = MediaPlayer.create(getApplicationContext(),
soundarray[4]);
}
filpsound.start();
}
else if(v==image6)
{
if (filpsound == null) {
filpsound = MediaPlayer.create(getApplicationContext(),
soundarray[5]);
}
filpsound.start();
}
}
}