1

嗨朋友们,我有两个数组列表,一个是图像数组列表,另一个是声音数组列表,我必须使用图像数组列表随机为图像视图提供背景,当我单击该图像时,我需要播放与图像相关的声音,两个数组列表现在都在改组,我可以随机设置图像背景,但我的问题出在声音上

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

}

}
4

2 回答 2

2

创建一图片和音乐(或更好的你自己的类),将它们放入列表并随机播放。

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)

PS你有很多重复的代码。例如,将 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 复制粘贴相同的代码。

于 2013-07-23T07:03:13.037 回答
0

您可以创建一个内部类来保存字符串和声音值,如下所示:private

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

然后打乱 stringsAndSounds 列表:

Collections.shuffle(stringsAndSounds);

现在您可以在 for 循环中的任何位置访问 stringsAndSounds,如下所示,该循环已被打乱:

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

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

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

}
于 2013-07-23T07:15:10.583 回答