2013-07-23 15 views
1

喜的朋友我感到有两个的ArrayList一个是图像的ArrayList,另外一个声音ArrayList中,我不得不放弃背景imageviews随机使用图像arrylist,当我点击那个图像,我需要播放该图像相关的声音,这两个arraylists我现在正在shuffling,我可以随机设置图像背景,但我的问题是在声音,洗牌后声音播放错误,如果'a'图像意味着它播放'c'听起来有些时候开始播放b听起来etc..how解决这个任何一个建议我如何洗牌两个不同的数组列表值有两个的ArrayList出切换顺序

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(); 
    } 

} 

} 

回答

2

创建图片和音乐的Pair(或更好的自己的类),把它们放到列表和洗牌了。

List<Pair<Integer, Integer>> pics = new ArrayList<Pair<Integer, Integer>>(); 
pics.add(new Pair<Integer, Integer>(R.drawable.a_white, R.raw.a)); 
... 
Collections.shuffle(pics); 

image1.setBackgroundResource(pics.get(0).first) 

P.S.你有很多重复的代码。例如,将Pair添加到imageView作为标记。

pict = pics.get(0); 
image1.setBackgroundResource(pict.first); 
image1.setTag(pict); 

然后在clickListener:

Pair<Integer, Integer> pict = (Pair<Integer, Integer>)v.getTag(); 
... 
filpsound = MediaPlayer.create(getApplicationContext(), pict.second) 

所以你不要需要复制粘贴代码相同的代码为每个ImageView的。

+0

没错,创建自己的类,它具有图像和声音,使这个类的arrayList,而不是制作两个不同的arrayLists。 –

+0

okk,谢谢你我会试着让你 – user2401554

+0

Pair pict = pics.get(0); 但不是配对,你最好用你的班级。配对太笼统。从@Suji答案创建类似Holder(但明智地命名)的东西。 – Leonidos

0

您可以同时持有字符串,公允价值这样创建一个内部类:私人

class Holder{ 
    int stringVal; 
    int soundVal; 

    public Holder(int stringVal,int soundVal) { 
     this.stringVal=stringVal; 
     this.soundVal=soundVal; 
    } 

} 

然后声明持有人的矢量如下:

Vector<Holder> stringsAndSounds=new Vector<Holder>(); 

然后做拷贝stringvalues和soundvalues stringsAndSounds arraylist如下:

for(int i=0;i<sltdalphabet.size() && i<sounds.size();i++){ 
    stringsAndSounds.add(new Holder(sltdalphabet.get(i), sounds.get(i))); 
} 

然后洗牌串AndSounds列表:

Collections.shuffle(stringsAndSounds); 

现在,你可以在任何地方访问stringsAndSounds在for循环如下,这已经是洗牌:

for(int i=0;i<stringsAndSounds.size();i++){ 

int stringVal = stringsAndSounds.elementAt(0).stringVal; 

int soundVal = stringsAndSounds.elementAt(0).soundVal; 
// do your code 

} 
+0

在这种情况下,ArrayList比Vector好。 – Leonidos

+0

okk.thankyou我会尝试这一个也 – user2401554

+0

喜筋我尝试使用乌尔方式如下面矢量 stringsAndSounds =新向量(); \t \t \t \t \t for(int i = 0; i user2401554

相关问题