【问题标题】:Slide up animation not working correctrly上滑动画无法正常工作
【发布时间】:2018-05-24 08:31:23
【问题描述】:

我有一个线性布局。这是我的 xml 代码

      <LinearLayout
                xmlns:card_view="http://schemas.android.com/apk/res-auto"
                android:id="@+id/card_view"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:orientation="vertical"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:layout_marginTop="-4dp">


                <android.support.v4.widget.NestedScrollView
                    android:id="@+id/scrollView"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:fillViewport="true">
                <LinearLayout
                    android:id="@+id/paper_layout"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:focusableInTouchMode="true"
                    android:background="#ffffff"
                    android:orientation="vertical">

                    <ImageView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="26dp"
                        android:src="@mipmap/ic_app_icon" />


                    <TextView
                        android:id="@+id/company_name"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="10dp"
                        android:gravity="center"
                        android:paddingLeft="16dp"
                        android:paddingRight="16dp"
                        android:singleLine="true"
                        android:text="Company Name"
                        android:textColor="@android:color/black"
                        android:textSize="30dp" />

                    <TextView
                        android:id="@+id/company_address"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="5dp"
                        android:gravity="center"
                        android:paddingLeft="16dp"
                        android:paddingRight="16dp"
                        android:singleLine="true"
                        android:text="Company Address"
                        android:textColor="@android:color/black"
                        android:textSize="14dp" />

                    <android.support.v7.widget.RecyclerView
                        android:id="@+id/recyclerView"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="6dp"
                        android:layout_marginRight="6dp"
                        android:layout_marginTop="10dp"
                        android:focusableInTouchMode="false"
                        android:listSelector="#00000000"
                        android:overScrollMode="never"
                        android:scrollbars="none" />

                    <ImageView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="right"
                        android:layout_marginRight="15dp"
                        android:layout_marginTop="10dp"
                        android:src="@mipmap/test_img" />

                    <View
                        android:layout_width="match_parent"
                        android:layout_height="16dp" />
                </LinearLayout>
                </android.support.v4.widget.NestedScrollView>
            </LinearLayout>

我的目标是创建自下而上的动画。我写了一些代码,但动画无法正常工作。这是我的java代码。 在Activity的onCreate方法中

 slide = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.0f,
            Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF,
            0.0f, Animation.RELATIVE_TO_SELF, -5.0f);
    slide.setDuration(4000);
    slide.setFillAfter(true);
    slide.setFillEnabled(true);

我尝试像这样开始动画

 if (slide != null) {
        linearLayout.startAnimation(slide);
        slide.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                linearLayout.clearAnimation();
                startActivity(MainActivity.class);

            }

        });
    }

我的目标是在动画完成后立即开始另一个活动

 public void startActivity(Class<?> cls) {
    Intent intent = new Intent(this, cls);
    startActivity(intent);
    overridePendingTransition(R.anim.fadein, R.anim.fadeout);
}

但是,4秒后,动画完成后,新的活动并没有立即工作。有什么方法可以加快调用新活动的速度吗?

【问题讨论】:

  • 在开始动画之前添加监听器
  • 我改了但还是不行@LucaNicoletti
  • 你调试了吗,代码通过startActivity调用运行了吗?

标签: android android-animation android-linearlayout


【解决方案1】:

试试这个:

val animation = ObjectAnimator.ofFloat(layout, "translationY", 440f)
animation.duration = 4500
animation.interpolator = LinearInterpolator()
animation.addListener(AnimatorListenerAdapter() {
  //animation end is here
  // TODO
})

代码在kotlin中,但是你可以很容易地转换它

【讨论】:

  • 我改变了它,但仍然有同样的错误。我认为问题出在 TranslateAnimation @Luca Nicoletti
  • 你的动画是从上到下@Luca Nicoletti
  • 给负值
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-18
  • 2016-02-18
相关资源
最近更新 更多