[英]Android 2.3: Grow/Shrink animation bug?
对于我的应用程序,我正在尝试在我的布局中为每个 ImageView 添加一个 [增长/收缩 + alpha 更改] animation。 通过为我的两个 XML 文件(grow.xml 和shrink.xml)设置fillAfter="true" ,我设法使动画正常工作并让每个动画在完成后仍然存在。 但是,似乎有一些奇怪的 animation 错误会导致未选择的图像增长,然后当我将fillAfter="true设置为 shrink.xml:让我解释一下应用程序是如何工作的,然后给出一个场景它变得更加清晰:
最初,所有图像的 alpha 级别都设置为 50%。 当我单击特定图像时,它将增长到 120%,并且其 alpha 级别将变为 100%(“点亮”效果)。 当我单击另一个图像时,之前选择的图像将缩小到 100%,其 alpha 级别将恢复到 50%,并且当前选择的图像将如前所述增长。
在我的布局中,我将三个大小相同的图像排成一行。 我单击第一个图像,然后单击第二个图像,然后再次单击第一个图像。 好的,那里没有问题。 现在,我点击第三张图片,我得到了第一张图片的奇怪捕捉问题。 知道如何解决这个问题吗?
我试过了:
收缩。xml:
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/accelerate_decelerate_interpolator" android:fillAfter="true"> <scale android:fromXScale="1.2" android:toXScale="1.0" android:fromYScale="1.2" android:toYScale="1.0" android:duration="300" android:pivotX="50%" android:pivotY="50%"/> <alpha android:fromAlpha="1.0" android:toAlpha="0.5" android:duration="300"/> </set>
成长.xml
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/accelerate_decelerate_interpolator" android:fillAfter="true"> <scale xmlns:android="http://schemas.android.com/apk/res/android" android:fromXScale="1.0" android:toXScale="1.20" android:fromYScale="1.0" android:toYScale="1.20" android:duration="300" android:pivotX="50%" android:pivotY="50%" /> <alpha android:fromAlpha="0.5" android:toAlpha="1.0" android:duration="300"/> </set>
淡出.xml:
<?xml version="1.0" encoding="UTF-8"?> <alpha xmlns:android="http://schemas.android.com/apk/res/android" android:startOffset="300" android:fromAlpha="1.0" android:toAlpha="0.5" android:fillAfter="true"> </alpha>
main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="center"> <ImageView android:id="@+id/image1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingLeft="20dip" android:paddingRight="20dip" android:src="@drawable/image1"/> <ImageView android:id="@+id/image2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingLeft="20dip" android:paddingRight="20dip" android:src="@drawable/image2"/> <ImageView android:id="@+id/image3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingLeft="20dip" android:paddingRight="20dip" android:src="@drawable/image3"/> </LinearLayout>
测试.java:
public class Test extends Activity {
private View mSelected;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Animation fadeOut = AnimationUtils.loadAnimation(this, R.anim.fade_out);
final Animation grow = AnimationUtils.loadAnimation(this, R.anim.grow);
final Animation shrink = AnimationUtils.loadAnimation(this, R.anim.shrink);
OnClickListener listener = new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (mSelected == v)
return;
if (mSelected != null)
mSelected.startAnimation(shrink);
mSelected = v;
mSelected.startAnimation(grow);
}
};
ImageView image1 = (ImageView)findViewById(R.id.image1);
image1.startAnimation(fadeOut);
image1.setOnClickListener(listener);
ImageView image2 = (ImageView)findViewById(R.id.image2);
image2.startAnimation(fadeOut);
image2.setOnClickListener(listener);
ImageView image3 = (ImageView)findViewById(R.id.image3);
image3.startAnimation(fadeOut);
image3.setOnClickListener(listener);
}}
问题是您的收缩 Animation 仍然分配给其他视图。 当您调用 mSelected.startAnimation() 时,您正在启动 Animation object,它附加到其他视图,因此它们也具有动画效果。 您可以通过更改mSelected.startAnimation(shrink);
至
mSelected.startAnimation(AnimationUtils.loadAnimation(Test.this, R.anim.shrink));
这是解决问题的简单(尽管效率低下)方法,或者您可以通过取消分配视图中的动画( mSelected.setAnimation(null)
)自己管理 Animation 循环。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.