我有一个GridView,其中12个元素排列在3x4网格中。每个元素都是一个名为grid_item.xml的布局,其中包含一个ImageView和一个TextView。最初,ImageView是图块的图像。当您单击/触摸任何GridView元素时,会向ImageView应用动画以将其缩小并显示下面的TextView。
除了GridView中的最终(右下)元素外,这一切都正常。当我点击它(在Android模拟器中,尚未在设备上测试)时,会调用click事件(称为grid_click),但不调用“onAnimationStart”侦听器(当我调用 时单击任何其他GridView元素)。
换句话说,当我单击右下角的GridView元素时,没有动画,我在Logcat中看到了这一点:
TEST开始动画
但是,如果我点击任何其他元素,动画工作正常,在Logcat中我看到:
TEST开始动画
测试onAnimationStart
Logcat不会输出任何其他内容来指示问题。如果有人可以提供帮助,我会很感激,因为我花了几天时间试图解决这个问题。我的代码如下:
private void init_gridview()
{
setContentView(R.layout.game);
GridView gridview = (GridView) findViewById(R.id.gridview);
gridview.setNumColumns(3);
gridview.setColumnWidth(32);
gridview.setAdapter(new ImageAdapter(this));
gridview.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View v, int position, long id)
{
grid_click(position,v);
}
});
}
public class ImageAdapter extends BaseAdapter
{
private Context mContext;
public ImageAdapter(Context c)
{
mContext = c;
}
public int getCount()
{
return(3*4);
}
public Object getItem(int position)
{
return null;
}
public long getItemId(int position)
{
return 0;
}
// inflate grid_item.xml and return a View combining textview and imageview
public View getView(int position, View convertView, ViewGroup parent)
{
View MyView = convertView;
if (convertView == null)
{
LayoutInflater li = getLayoutInflater();
MyView = li.inflate(R.layout.grid_item, null);
}
else
{
MyView=convertView;
}
TextView tv = (TextView)MyView.findViewById(R.id.grid_item_text);
tv.setText("something");
ImageView iv = (ImageView)MyView.findViewById(R.id.grid_item_image);
iv.setScaleType(ImageView.ScaleType.CENTER_CROP);
iv.setPadding(0,0,0,0);
iv.setImageResource(R.drawable.tile);
MyView.setLayoutParams(new GridView.LayoutParams(32,32));
return(MyView);
}
}
private void grid_click(int position, View v)
{
View clicked_view=v;
Animation scale_down = AnimationUtils.loadAnimation(this, R.anim.scale_down);
scale_down.setAnimationListener(new AnimationListener()
{
public void onAnimationStart(Animation animation)
{
Log.i("TEST","onAnimationStart");
}
public void onAnimationEnd(Animation animation)
{
}
public void onAnimationRepeat(Animation animation) {}
});
Log.i("TEST","start animation");
ImageView iv2 = (ImageView)clicked_view.findViewById(R.id.grid_item_image);
iv2.startAnimation(scale_down);
}
这是scale_down.xml动画:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<scale android:pivotX="50%" android:pivotY="50%" android:fromXScale="1" android:fromYScale="1" android:toXScale="0" android:toYScale="0" android:duration="250" />
<rotate android:fromDegrees="0" android:toDegrees="180" android:pivotX="50%" android:pivotY="50%" android:duration="250" />
</set>
以下是GridView中每个项目的布局,称为grid_item.xml:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:id="@+id/grid_item_image"
android:src="@drawable/tile"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/grid_item_text"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:textStyle="bold"
android:textSize="17sp"
android:shadowColor="#555555"
android:shadowDx="1.0"
android:shadowDy="1.0"
android:shadowRadius="1.5"
/>
</FrameLayout>
最后包含GridView的主要布局,名为game.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/Layout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:orientation="vertical"
android:gravity="center"
android:background="@android:color/white">
<GridView
android:id="@+id/gridview"
android:background="@android:color/white"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:columnWidth="106dp"
android:numColumns="auto_fit"
android:verticalSpacing="0dp"
android:horizontalSpacing="0dp"
android:stretchMode="none"
android:gravity="center"
android:listSelector="@android:color/transparent"
/>
</LinearLayout>
答案 0 :(得分:0)
在gridview中更改
android:layout_height="wrap_content"
到
android:layout_height="fill_parent"
永远不要将wrap_content用作gridview的布局高度。