[英]Android Gridview Animation not working in final item
我有一个以3x4网格排列的12个元素的GridView。 每个元素都是一个名为grid_item.xml的布局,在其下面包含一个ImageView和一个TextView。 最初,ImageView是图块的图像。 当您单击/触摸任何GridView元素时,会将动画应用于ImageView以按比例缩小并在下面显示TextView。
除了GridView中的最后一个(右下)元素之外,所有其他方法都工作正常。 当我单击它时(在Android模拟器中,尚未在设备上进行测试),单击事件(称为grid_click)被调用,但未调用“ onAnimationStart”侦听器(当我单击任何其他GridView元素时被调用)。
换句话说,当我单击右下角的GridView元素时,没有动画,而我在Logcat中看到了这一点:
测试开始动画
但是,如果单击任何其他元素,动画将正常运行,并且在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>
在gridview中更改
android:layout_height="wrap_content"
至
android:layout_height="fill_parent"
切勿将wrap_content用作gridview的布局高度。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.