安卓-TranslateAnimation不起作用

3

我正在尝试在ImageView上启动一个闪亮效果动画,但问题是动画不会显示在屏幕上。

只有在按钮监听器内启动时才能正常工作。

这是我的shine_effect.xml文件:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
    android:centerColor="#beffffff"
    android:endColor="#00ffffff"
    android:startColor="#00ffffff" />
 </shape> 

这是我的布局XML文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/gradient"
android:orientation="vertical">


<RelativeLayout
    android:id="@+id/relativeLayout"
    android:layout_width="170dp"
    android:layout_height="170dp"
    android:layout_centerInParent="true">

    <ImageView
        android:id="@+id/img"
        android:layout_width="170dp"
        android:layout_height="170dp"
        android:contentDescription="@string/desc"
        android:src="@drawable/logo" />

    <ImageView
        android:id="@+id/shine"
        android:layout_width="50dp"
        android:layout_height="170dp"
        android:layout_marginStart="-50dp"
        android:contentDescription="@string/desc"
        android:src="@drawable/shine_effect" />
</RelativeLayout>

<TextView
    android:id="@+id/textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/relativeLayout"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="43dp"
    android:text="@string/pending"
    android:textColor="@android:color/white" />


 </RelativeLayout>

我的活动 onCreate 方法:

   protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_chat_pending);
    img=(ImageView)findViewById(R.id.img) ;
    shine=(ImageView)findViewById(R.id.shine) ;

  shine_anim=new TranslateAnimation(0, img.getWidth()+ shine.getWidth(),0, 
  0);
    shine_anim.setDuration(550);
    shine_anim.setFillAfter(true);
    shine_anim.setRepeatMode(Animation.RESTART);
    shine_anim.setInterpolator(new AccelerateDecelerateInterpolator());
    shine_anim.setRepeatMode(Animation.INFINITE);
    shine.startAnimation(shine_anim);
}
2个回答

0

这是因为 ImageView 还没有布局,所以宽度为0。您可以给它一个布局周期,然后开始动画。

img.getViewTreeObserver().addOnGlobalLayoutListener(
        new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                img.getViewTreeObserver().removeOnGlobalLayoutListener(this);

                // Create and start animation
            }
        });

或者,如果您的图像宽度始终保持不变,您可以将那个170dp转换为px,并在创建TranslateAnimation时使用该值,而不是使用视图的getWidth()方法。


谢谢 :) 它能工作了。但是它重新启动得太快了..我怎么控制迭代时间呢? - m.d

0

在onWindowFocusChanged方法中调用您的动画

@Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);

        if(hasFocus) {
            shine_anim = new TranslateAnimation(0, img.getWidth() + shine.getWidth(), 0,
                    0);
            shine_anim.setDuration(550);
            shine_anim.setFillAfter(true);
            shine_anim.setRepeatMode(Animation.RESTART);
            shine_anim.setInterpolator(new AccelerateDecelerateInterpolator());
            shine_anim.setRepeatMode(Animation.INFINITE);
            shine.startAnimation(shine_anim);
        }
    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接