And*_*oob 5 animation android imageview
对于我的应用程序,我正在尝试在我的布局中为每个ImageView添加[grow/shrink + alpha change]动画.我设法让动画工作,并通过为我的两个XML文件(grow.xml和shrink.xml)设置fillAfter ="true",让每个动画在完成后保持不变.然而,似乎有一些奇怪的动画错误导致未选择的图像增长,然后当我设置fillAfter ="true为shrink.xml 时,"快速恢复到正常大小!让我解释一下应用程序是如何工作的,然后给出一个场景它变得更加清晰:
最初,所有图像的alpha级别设置为50%.当我点击特定图像时,它将增长到120%,其alpha级别将变为100%('点亮'效果).当我单击另一个图像时,先前选择的图像将缩小回100%并且其alpha级别将返回到50%,并且当前所选图像将如前所述增长.
在我的布局中,我有三个相同大小的图像排成一排.我单击第一个图像,然后单击第二个图像,然后再次单击第一个图像.好的,没有问题.现在,我点击第三张图片,我得到了第一张图片的奇怪的对齐问题.知道如何解决这个问题吗?
我试过了:
shrink.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>
grow.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>
fade_out.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>
Test.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);
}}
Run Code Online (Sandbox Code Playgroud)
问题是您的收缩动画仍然分配给其他视图。当您调用 mSelected.startAnimation() 时,您正在启动动画对象,该对象附加到其他视图,因此它们也会产生动画。mSelected.startAnimation(shrink);
您可以通过更改为来创建动画的新实例
mSelected.startAnimation(AnimationUtils.loadAnimation(Test.this, R.anim.shrink));
Run Code Online (Sandbox Code Playgroud)
这是解决问题的简单(尽管效率低下)的方法,或者您可以通过从视图中取消分配动画来自行管理动画周期(mSelected.setAnimation(null)
)。
归档时间: |
|
查看次数: |
3410 次 |
最近记录: |