我的应用程序有一个充满图片的网格视图(卡片记忆游戏)。触摸后,翻转卡片并显示面部图片。但有时脸部图片不会出现。如果我再用卡片翻转,它就会恢复。
点击后我拨打1/2翻转动画:
this.gridview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
actPosition = position;
cardsTurned++;
gridview.setEnabled(false); // prevents clicking on other cards when one is actually animated
// first part of card turn animation
((ImageView) gridview.getChildAt(actPosition)).clearAnimation();
((ImageView) gridview.getChildAt(actPosition)).setAnimation(animation1);
((ImageView) gridview.getChildAt(actPosition)).startAnimation(animation1);
}
});
在动画结束后,我将位图设置到正确的位置:
@Override
public void onAnimationEnd(Animation animation) {
Bitmap bitmapFace = BitmapFactory.decodeFile(this.imageAdapter.getItem(actPosition).getBitmapFace());
Bitmap bitmapBack = BitmapFactory.decodeResource(getResources(), R.drawable.card_back);
// after animation1 should come animation2
if (animation==animation1) {
// now we see nothing, change picture to face/back
if (imageAdapter.getItem(actPosition).getBackShowed()) // if we see back
((ImageView) gridview.getChildAt(actPosition)).setImageBitmap(bitmapFace); // show face
else // if we see face
((ImageView) gridview.getChildAt(actPosition)).setImageBitmap(bitmapBack); // show back
// complete animation with the second part and show changed picture
((ImageView) gridview.getChildAt(actPosition)).clearAnimation();
((ImageView) gridview.getChildAt(actPosition)).setAnimation(animation2);
((ImageView) gridview.getChildAt(actPosition)).startAnimation(animation2);
}
else {
if(this.cardsTurned == 1) { // 1 card is turned now
card1 = imageAdapter.getItem(actPosition); // first turned card
gridview.getChildAt(actPosition).setClickable(true); // wait for the second card
}
else if(this.cardsTurned == 2) { // 2 cards are turned now
card2 = imageAdapter.getItem(actPosition); // second turned card
// if cards are a PAIR, remove them from gridview
if(card1.getPair() == actPosition) { // turned cards are a PAIR
// remove cards
gridview.getChildAt(card1.getPosition()).setAlpha(0);
gridview.getChildAt(card1.getPosition()).setOnClickListener(null);
gridview.getChildAt(actPosition).setAlpha(0);
gridview.getChildAt(actPosition).setOnClickListener(null);
}
else { // cards are not a PAIR
// card2 turn back
((ImageView) gridview.getChildAt(actPosition)).clearAnimation();
((ImageView) gridview.getChildAt(actPosition)).setAnimation(animation1);
((ImageView) gridview.getChildAt(actPosition)).startAnimation(animation1);
((ImageView) gridview.getChildAt(actPosition)).setImageBitmap(bitmapBack);
((ImageView) gridview.getChildAt(actPosition)).clearAnimation();
((ImageView) gridview.getChildAt(actPosition)).setAnimation(animation2);
((ImageView) gridview.getChildAt(actPosition)).startAnimation(animation2);
// card1 turn back
((ImageView) gridview.getChildAt(card1.getPosition())).clearAnimation();
((ImageView) gridview.getChildAt(card1.getPosition())).setAnimation(animation1);
((ImageView) gridview.getChildAt(card1.getPosition())).startAnimation(animation1);
((ImageView) gridview.getChildAt(card1.getPosition())).setImageBitmap(bitmapBack);
((ImageView) gridview.getChildAt(card1.getPosition())).clearAnimation();
((ImageView) gridview.getChildAt(card1.getPosition())).setAnimation(animation2);
((ImageView) gridview.getChildAt(card1.getPosition())).startAnimation(animation2);
imageAdapter.getItem(card1.getPosition()).changeBackShowed();
gridview.getChildAt(card1.getPosition()).setClickable(false);
}
this.cardsTurned = 0;
}
// unlock grid view for another clicking
imageAdapter.getItem(actPosition).changeBackShowed();
gridview.setEnabled(true);
}
}
我的适配器的getCount()总是返回20:
@Override
public int getCount() {
Log.d(TAG, "getCount(): " + cards.length);
return cards.length;
}
当图片未显示时,也不会调用适配器的getView()。
怎么可能?是GridView还是我做错了什么?