今天测试Animation动画代码时, 定义了四个ImageView组件, 并分别对其设置了四个不同的动画效果, 但在设置AlphaAnimation动画的一些属性时,发生了很诡异的问题, 就是我如果设置AlphaAnimation下列属性:alphaAnimation.setFillAfter(true);
alphaAnimation.setFillBefore(false);则AlphaAnimation动画效果不受影响,符合属性设置, 但只要呈现了该组件的动画效果后, 对于其他三个组件的动画效果就会产生影响, 其他三个动画效果很诡异但在为其他三个分别设置上述属性, 并注释掉AlphaAnimation的上述属性设置代码后, 则不会对其他组件动画产生影响, 所以说这个问题很诡异,不知道大家有没有遇到过, 希望知道原因的能解答一下,谢谢!下面是全部代码:csdn貌似不能上传附件, 麻烦大家自己建工程拷贝代码进行测试
main.xml:<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:gravity = "center"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<ImageView
android:id = "@+id/imageView01"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:src = "@drawable/icon"/>
<ImageView
android:id = "@+id/imageView02"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:src = "@drawable/icon"/>
<ImageView
android:id = "@+id/imageView03"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:src = "@drawable/icon"/>
<ImageView
android:id = "@+id/imageView04"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:src = "@drawable/icon"/>

</LinearLayout>
TestAnimation.javapackage snowfox.animation;import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;public class TestAnimation extends Activity implements OnClickListener{
AnimationTools animationTools;
ImageView imageView01, imageView02, imageView03, imageView04;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        imageView01 = (ImageView)findViewById(R.id.imageView01);
        imageView02 = (ImageView)findViewById(R.id.imageView02);
        imageView03 = (ImageView)findViewById(R.id.imageView03);
        imageView04 = (ImageView)findViewById(R.id.imageView04);
        animationTools = new AnimationTools();
        imageView01.setOnClickListener(this);
        imageView02.setOnClickListener(this);
        imageView03.setOnClickListener(this);
        imageView04.setOnClickListener(this);
    }
@Override
public void onClick(View view)
{
switch(view.getId())
{
case R.id.imageView01:
animationTools.translateAnimation(view);
break;
case R.id.imageView02:
animationTools.rotateAnimation(view);
break;
case R.id.imageView03:
animationTools.scaleAnimation(view);
break;
case R.id.imageView04:
animationTools.alphaAnimation(view);
break;
}
}
}
AnimationToolspackage snowfox.animation;import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;public class AnimationTools
{
public void alphaAnimation(View view)
{
System.out.println("alphaAnimation");
AlphaAnimation alphaAnimation = new AlphaAnimation(1, 0);

alphaAnimation .setDuration(1000);
alphaAnimation.setFillAfter(true);
alphaAnimation.setFillBefore(false);
view.startAnimation(alphaAnimation);
}

public void translateAnimation(View view)
{
System.out.println("translateAnimation");
TranslateAnimation translateAnimation = new TranslateAnimation(
Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 1.0f);

translateAnimation.setDuration(1000);
// translateAnimation.setFillAfter(true);
// translateAnimation.setFillBefore(true);
view.startAnimation(translateAnimation);
}
public void scaleAnimation(View view)
{
System.out.println("scaleAnimation");
ScaleAnimation scaleAnimation = new ScaleAnimation(1, 0.1f, 1, 0.1f,
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);

scaleAnimation.setDuration(1000);
// scaleAnimation.setFillAfter(true);
// scaleAnimation.setFillBefore(false);
view.startAnimation(scaleAnimation);
}
public void rotateAnimation(View view)
{
System.out.println("rotateAnimation");
RotateAnimation rotateAnimation = new RotateAnimation(0, 360,
Animation.RELATIVE_TO_SELF, 1f, Animation.RELATIVE_TO_SELF, 1f);

rotateAnimation.setDuration(1000);
// rotateAnimation.setFillAfter(true);
// rotateAnimation.setFillBefore(false);
view.startAnimation(rotateAnimation);
}
}

解决方案 »

  1.   

    前几天看了下bootanimation,以为你研究开机动画呢,原来少个boot呵呵
      

  2.   

    怎么感觉你AnimationTools animationTools;这个没实例化呢?animationTools=new AnimationTools();AnimationTools.translateAnimation(view);
      

  3.   

    你尝试new出4个animationtools,分别和四个imageview关联起来,再尝试一下。看看是否可以解决这个问题
      

  4.   


    main.xml做如下修改就ok了:
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:gravity = "center"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        >
        <LinearLayout 
        android:orientation="vertical"
        android:gravity = "center"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        >
        <ImageView
            android:id = "@+id/imageView01"
            android:layout_width = "wrap_content"
            android:layout_height = "wrap_content"
            android:src = "@drawable/icon"/>
        <ImageView
            android:id = "@+id/imageView02"
            android:layout_width = "wrap_content"
            android:layout_height = "wrap_content"
            android:src = "@drawable/icon"/>
        <ImageView
            android:id = "@+id/imageView03"
            android:layout_width = "wrap_content"
            android:layout_height = "wrap_content"
            android:src = "@drawable/icon"/>
        </LinearLayout>
        
        <ImageView
            android:id = "@+id/imageView04"
            android:layout_width = "wrap_content"
            android:layout_height = "wrap_content"
            android:src = "@drawable/icon"/>
            
    </LinearLayout>
      

  5.   

    四个ImageView 共用一个Drawable,AlphaAnimation会对Drawable产生影响。
      

  6.   

    ImageView 里的代码public void setAlpha(int alpha) {
            alpha &= 0xFF;          // keep it legal
            if (mAlpha != alpha) {
                mAlpha = alpha;
                applyColorMod();
                invalidate();
            }
        }    private void applyColorMod() {
            if (mDrawable != null) {
                mDrawable.setColorFilter(mColorFilter);
                mDrawable.setAlpha(mAlpha * mViewAlphaScale >> 8);
            }
        }