android - 为我的布局上下滑动动画

标签 android android-layout

我有 2 个 LinearLayouts,它将屏幕分成两部分,顶部和底部(请参阅屏幕截图)。当用户按下 FloatingActionButton 时,第一个 LinearLayout 应该向上滑动,第二个 LinearLayout 应该向下滑动,这是我的尝试,但它不起作用,see my concept

这是我的xml文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:showIn="@layout/activity_main"
tools:context="com.example.jaisonjoseph.sample.MainActivity"
android:orientation="vertical">


<LinearLayout
    android:id="@+id/first"
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    android:layout_gravity="top"
    android:layout_weight=".1"
    android:background="@android:color/holo_blue_dark"
    android:orientation="horizontal">


</LinearLayout>

<LinearLayout
    android:id="@+id/second"
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    android:layout_gravity="bottom"
    android:layout_weight=".1"
    android:background="@android:color/holo_green_light"
    android:orientation="horizontal" />


</LinearLayout>

这些是动画的 2 个 xml 文件

第一个

  <?xml version="1.0" encoding="utf-8"?>
 <set xmlns:android="http://schemas.android.com/apk/res/android" >

    <translate
    android:duration="1000"
    android:fromYDelta="0"
    android:toYDelta="100%" />

第二个

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >

<translate
    android:duration="1000"
    android:fromYDelta="100%"
    android:toYDelta="0" />

这是主要 Activity 的代码

   final LinearLayout first =(LinearLayout)findViewById(R.id.first) ;
    final LinearLayout second =(LinearLayout)findViewById(R.id.second) ;

    final Animation slide_down = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.slide_down);
    final Animation slide_up = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.slide_up);

   FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {





           first.setAnimation(slide_up);
           second.setAnimation(slide_down);

        }
    });
}

after adding the answer

最佳答案

使用 startAnimation() 而不是 setAnimation()

试试这个:

FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {

       first.startAnimation(slide_up);
       second.startAnimation(slide_down);
    }
});

slide_up.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >

<translate
    android:duration="1000"
    android:fromYDelta="0%"
    android:toYDelta="-200%" />
</set>

slide_down.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >

<translate
    android:duration="1000"
    android:fromYDelta="0%"
    android:toYDelta="200%" />
</set>

这是一个很好的tutorial关于不同类型的动画。

希望对你有帮助~

关于android - 为我的布局上下滑动动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43703511/

相关文章:

java - Android中如何让6个按钮调用不同参数的同一个方法?

android - 对于以上 Android 7,Genymotion ARM 翻译失败

java - Android DefaultHttpClient() 发布请求

android - WebViewClient 类不适用于 android api 级别 15

android - Android 代码中的 NullPointerException

android - 使用 LoaderManager 加载依赖查询的首选方法

android - 如何将关闭按钮放在 android 警告对话框的右上角?

android - 我可以在 Android 的不同布局中使用相同的 id 吗?

Android : on changing EditText background color, EditText 样式改变

android - 如何在android xml中绘制三角形