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



hi在我的应用程序中,我需要在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],它就不起作用。请帮助我如何解决这个问题。

改为使用gridview,并使用gridview。setonItemClich监听器这是解决您问题的最简单和优化的解决方案,如果您需要有关gridview 的帮助,请告诉我

祝你今天过得愉快!!

好吧,当你有这样的循环时,你不能有加号[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<3不会有加号[3]。

f的定义在哪里?

最新更新