翻译动画不起作用

时间:2018-03-28 10:06:31

标签: android translate-animation

  

我尝试从左下角到右中心再做一个简单的动画,再到屏幕中心

    Display display = getWindowManager().getDefaultDisplay();
    Point size = new Point();
    display.getSize(size);
    screenWidthPx = size.x;
    screenHeightPx = size.y;

   final TranslateAnimation slideFromRight = new TranslateAnimation(screenWidthPx, screenWidthPx/2, screenHeightPx, screenHeightPx);
    slideFromRight.setInterpolator(new AccelerateDecelerateInterpolator());
    slideFromRight.setStartOffset(300);
    slideFromRight.setDuration(600);

    slideFromRight.setAnimationListener(new Animation.AnimationListener() {
        @Override
        public void onAnimationStart(Animation animation) {
        }

        @Override
        public void onAnimationEnd(Animation animation) {
            TranslateAnimation titleIconSlideUp = new TranslateAnimation(0, screenWidthPx/2, screenHeightPx, screenHeightPx/2);
            titleIconSlideUp.setInterpolator(new AccelerateDecelerateInterpolator());
            titleIconSlideUp.setStartOffset(500);
            titleIconSlideUp.setFillAfter(true);
            titleIconSlideUp.setDuration(1000);
            tvAnimView.startAnimation(titleIconSlideUp);
        }

        @Override
        public void onAnimationRepeat(Animation animation) {
        }
    });

    tvAnimView.startAnimation(slideFromRight);

onAnimationEnd 中的第二个动画正常运行。但不知道为什么第一个动画不能正常工作。我的布局很简单

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/rl_root"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:clipChildren="false"
    android:clipToPadding="false"
    tools:context="broadpeak.animationlearning.AnimTestActivity">


    <TextView
        android:id="@+id/tv_anim_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World"
        android:textColor="@color/colorPrimaryDark"
        android:textSize="24sp" />
</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

您应该使用以下行开始动画:

tvAnimView.startAnimation(slideFromRight);

根据您的需要,将这段代码放在点击监听器或onCreate(), onResume()方法中。