i am trying to do a kind of flipcard app in which there is gridview of images showing a same image ,say a card_back .

On clicking each image,it flips showing another image which is different for every item of gridview,say a card_front.

what i am trying to do is that ,if two card_front are shown,on clicking the next item,the first two cards should be flipped to show their card_back image.ie,at a time a maximum of only two cards can show their card_front image.But i dont know how to do that.

Can anyone please help me out.Thanks in advance.

This is the code for flipping images in gridview.

   public class FullscreenActivity extends Activity
 implements OnItemClickListener, AnimationListener  

{    ImageView imageView ;
     int pos;
     Animation animation1;
     Animation animation2;
     int[] clicked = {0,0,0,0,0,0,0,0,0,0,0,0};



      public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);       
            setContentView(R.layout.activity_fullscreen);
            animation1 = AnimationUtils.loadAnimation(this, R.anim.to_middle);
            animation1.setAnimationListener(this);

            animation2 = AnimationUtils.loadAnimation(this, R.anim.from_middle);
            animation2.setAnimationListener(this);

        GridView gridview = (GridView) findViewById(R.id.gridview);
        gridview.setAdapter(new ImageAdapter(this));            

        gridview.setOnItemClickListener(new OnItemClickListener()
        {
            public void onItemClick(AdapterView<?> parent, View v, int position, long id)
             {   
                  pos=position;
                  imageView = ((ViewHolder) v.getTag()).img;

              (imageView).clearAnimation();
              (imageView).setAnimation(animation1);
              (imageView).startAnimation(animation1);

             }

        }); 
    }


    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3)
    {
        // TODO Auto-generated method stub

    }


    @Override
    public void onAnimationEnd(Animation arg0) {

        if (arg0==animation1) {

                if(clicked[pos]==0)
        {
                 imageView.setImageResource(ImageAdapter.mThumbSelected[pos]);

                 clicked[pos]=1;

              } else if(clicked[pos]==1) {
                  clicked[pos]=0; 

                  imageView.setImageResource(R.drawable.card_back);
             }

   imageView.clearAnimation();
   imageView.setAnimation(animation2);
   imageView.startAnimation(animation2);
        } 

    }


    @Override
    public void onAnimationRepeat(Animation animation) {
        // TODO Auto-generated method stub

    }


    @Override
    public void onAnimationStart(Animation animation) {
        // TODO Auto-generated method stub

    }



}
有帮助吗?

解决方案

define a counter and save two identifiers to the cards somewhere. here is one solution in pseudocode:

//global variable 
int flippedCardCounter = 0;
Array[] flippedCards = new Array[2];

gridview.setOnItemClickListener(new OnItemClickListener()
    {
        public void onItemClick(AdapterView<?> parent, View v, int position, long id)
         {   
              switch(flippedCardCounter)

              //flippedCardCounter = 0 -> no cards flipped
              case 0:
                  flippeCardCounter++;    //increase counter
                  flippedCard[0] = position //save reference
                  startFlipAnimation(yourItemAtPosition(0));
                  break;
              //flippedCardCounter = 1 -> one card flipped
              case 1:
                  flippeCardCounter++;    //increase counter
                  flippedCard[1] = position //save reference
                  startFlipAnimation(yourItemAtPosition(1));
                  break;
              //flippedCardCounter = 2 -> hide two flipped card, flip one card
              case 2:
                  flippeCardCounter = 0;    //reset counter

                  //get reference to already flipped card and hide it
                  startUnFlipAnimation(yourItemAtPosition(flippedCards[0]));
                  startUnFlipAnimation(yourItemAtPosition(flippedCards[1]));

                  //reset your array ()
                  flippedCards = new Array[2]; //not really needed
                  flippedCards[0] = position
                  flippeCardCounter++;   //increase counter
                   startFlipAnimation(yourItemAtPosition(position));
                  break;
         }

    }); 
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top