Android 为我的布局上下滑动动画

Android 为我的布局上下滑动动画,android,android-layout,Android,Android Layout,我有两个线性布局,将屏幕分成两部分,顶部和底部(请参阅屏幕截图)。当用户按下浮动操作按钮时,第一个线性布局应该向上滑动,第二个线性布局应该向下滑动,这是我的尝试,但它不起作用 这是我的xml文件 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.and

我有两个线性布局,将屏幕分成两部分,顶部和底部(请参阅屏幕截图)。当用户按下浮动操作按钮时,第一个线性布局应该向上滑动,第二个线性布局应该向下滑动,这是我的尝试,但它不起作用

这是我的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>

使用
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);
    }
});
向上滑动.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>
<?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>

向下滑动.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>
<?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>

下面是一个关于不同类型动画的好例子

希望这将有助于~

使用
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);
    }
});
向上滑动.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>
<?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>

向下滑动.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>
<?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>

下面是一个关于不同类型动画的好例子


希望这会有帮助~

但是它不起作用
你能指定什么实际上不起作用吗?第一次。设置动画(向上滑动);第二,设置动画(向下滑动);在浮动动作按钮的onclik上,它不是动画。当我把这些代码放到oncreate中时,它的动画now@rafsanahmad007if我用按钮代替浮动动作按钮,同样的问题there@rafsanahmad007
但它不工作
你能指定什么实际上不工作吗?第一次时。设置动画(向上滑动);第二,设置动画(向下滑动);在浮动动作按钮的onclik上,它不是动画。当我把这些代码放到oncreate中时,它的动画now@rafsanahmad007if我用按钮代替浮动动作按钮,同样的问题there@rafsanahmad007Ok这是可行的,你能建议我最好的动画(xml)来做这件事吗,上面的动画没有实现我的概念(见我的屏幕截图)…对于slide\U up.xml
这很合适,可以通过修改我的代码来给出slide\U down代码,并发布答案只需修改android:fromYScale=“0.0”和android:toYScale=“1.0”就可以进行幻灯片下载这还不够查看我的评论。好的,你能给我推荐最好的动画(xml)吗要做到这一点,上面的动画并没有实现我的概念(请参见我的截图)…对于slide\u up.xml
,这是合适的,可以通过修改我的代码来给出幻灯片向下的代码,并发布答案。对于幻灯片向下,只需修改android:fromYScale=“0.0”和android:toYScale=“1.0”这还不够。请查看我的评论。