代码之家  ›  专栏  ›  技术社区  ›  BekaKK

向上滑动动画无法正常工作

  •  0
  • BekaKK  · 技术社区  · 7 年前

    我有一个线性布局

          <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代码。

     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秒钟后,当动画完成时,新活动不会立即工作。有没有办法加快调用新活动的速度?

    1 回复  |  直到 7 年前
        1
  •  0
  •   Luca Nicoletti    7 年前

    尝试使用以下方法:

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

    代码在kotlin中,但是您可以轻松地转换它