Android -TranslateAnimation不起作用

问题描述 投票:2回答:2

我试图在图像视图上启动一个闪亮的情感动画。问题是动画没有显示在屏幕上。

只有当我在按钮监听器中启动它时才能工作。

这是我的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);
}
android animation translate-animation
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()视图方法。


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);
        }
    }
© www.soinside.com 2019 - 2024. All rights reserved.