如何对android中的Image数组中的每个Image执行操作

时间:2012-03-19 10:26:49

标签: android android-layout

在我的应用程序中,我需要在3行和2列中显示6个图像。我需要对每个图像执行操作。但我只能对前三个图像执行操作。我可以对6个图像执行操作。

  TableLayout.LayoutParams param,param1;

    ImageView[] plus=new ImageView[6];
     TableRow[] row = new TableRow[6];

      RelativeLayout.LayoutParams lp2,lp1,lp3,lp4,lp5;
      RelativeLayout linear;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main1);


         layout = new TableLayout (this);
         layout.setLayoutParams( new TableLayout.LayoutParams(40,50) );
         param=new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT,TableLayout.LayoutParams.WRAP_CONTENT);
         param.setMargins(25, 0, 0, 0);
         lp1=new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
         linear=(RelativeLayout)findViewById(R.id.linear);
         Collections.shuffle(solutionList);

         randomNumbers = (Integer[])solutionList.toArray();

         int unique=0;

         for (f=0; f<3; f++) {
             row[f] = new TableRow(this);
             for (int c=0; c<2; c++) {

                 plus[f]=new ImageView(this);

                 plus[f].setBackgroundResource(randomNumbers[unique]);



                 plus[f].setOnClickListener(this);

                 row[f].addView(plus[f], 200,100);

                 unique++;
             } // for
             layout.addView(row[f],param);
         } // for

         linear.addView(layout,lp1);

         setContentView(linear);
        image1=new ImageView(this);
         image1.setBackgroundResource(R.drawable.pennib);

         lp2=new RelativeLayout.LayoutParams(200,100);
         lp2.setMargins(230, 0, 0, 0);
         linear.addView(image1,lp2);

              image1.setOnClickListener(new View.OnClickListener() {
           public void onClick(View view) {

               Log.i("",""+i);
            if (isFirstImage) {       
             applyRotation(0, 90);
             isFirstImage = !isFirstImage;

            } else {    
             applyRotation(0, -90);
             isFirstImage = !isFirstImage;
            }

           }
        });    



        }

        private void applyRotation(float start, float end) {
        // Find the center of image
        final float centerX = image1.getWidth() / 2.0f;
        final float centerY = image1.getHeight() / 2.0f;

        // Create a new 3D rotation with the supplied parameter
        // The animation listener is used to trigger the next animation
        final Flip3dAnimation rotation =
               new Flip3dAnimation(start, end, centerX, centerY);
        rotation.setDuration(500);
        rotation.setFillAfter(true);
        rotation.setInterpolator(new AccelerateInterpolator());
        rotation.setAnimationListener(new DisplayNextView(isFirstImage, image1,plus[0] ));

        if (isFirstImage)
        {
        image1.startAnimation(rotation);
        } else {
            plus[0].startAnimation(rotation);
        }
    }



        }

}

但是在加号[0]的地方,如果我给加号[3]它不起作用。请帮助我如何解决这个问题。

2 个答案:

答案 0 :(得分:0)

使用gridview代替并使用gridview.setonItemClich监听器这是最简单的优化解决方案,如果您需要gridview上的帮助,请告诉我

度过美好的一天!!

答案 1 :(得分:0)

嗯,当你有像这样的循环时,你不能有加号[3]

 for (f=0; f<3; f++) {
         row[f] = new TableRow(this);
         for (int c=0; c<2; c++) {

             plus[f]=new ImageView(this);

             plus[f].setBackgroundResource(randomNumbers[unique]);



             plus[f].setOnClickListener(this);

             row[f].addView(plus[f], 200,100);

             unique++;
         } // for
         layout.addView(row[f],param);
     } // for

f&lt; 3 不会有加[3]。

f的定义在哪里?