简体   繁体   English

适用于linearlayout的Android幻灯片动画

[英]Android slide animation for linearlayout

One activity of my Android-app uses a linearlayout with a couple of textviews and one imageview. 我的Android应用程序的一个活动使用带有两个textview和一个imageview的linearlayout。 the textviews and the imageview get the data from a sqlite-database. textview和imageview从sqlite数据库获取数据。 i also have two buttons (prev, next) to navigate back and forth through the database. 我也有两个按钮(上一个,下一个)来回浏览数据库。 works fine. 工作正常。

Now i want to add animation to my app: when i push the next-button the linearlayout (with the textviews and the image) should slide to the left and a linearlayout containing data from the next row should slide in from the right. 现在,我想向我的应用程序添加动画:当我按下next按钮时,linearlayout(带有textviews和图像)应该向左滑动,而包含下一行数据的linearlayout应该从右侧滑动。 the other way round for the prev-button. 反之亦然。

Any idea how to solve this? 任何想法如何解决这个问题?

You can achieve this using the translate tween animation. 您可以使用平移补间动画来实现此目的。

Place the linearlayout holding the textviews, imageview and buttons in a parent Linear/Relative layout: 将包含文本视图,图像视图和按钮的linearlayout放置在父级Linear / Relative布局中:

<LinearLayout android:id="@+id/parent_layout">
    <LinearLayout android:id="@+id/container_layout">
        <Textview />

        <ImageView />

        <Buttons />
    </LinearLayout>
</LinearLayout>

Define two translate tween animations in res/anim folder, one for previous and the other for next 在res / anim文件夹中定义两个转换补间动画,一个用于上一个,另一个用于下一个

 `previous.xml` -

<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="1000"
    android:fromXDelta="-100%p"
    android:toXDelta="0">
</translate>

`next.xml` -

<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="1000"
    android:fromXDelta="100%p"
    android:toXDelta="0">
</translate>

Initialize the animations and get a reference of the "container" layout in the onCreate() method of the activity: 初始化动画,并在活动的onCreate()方法中获取“容器”布局的引用:

Animation previousAnim = AnimationUtils.loadAnimation(MainMenuActivity.this, R.anim.previous);
Animation nextAnim = AnimationUtils.loadAnimation(MainMenuActivity.this, R.anim.next);
LinearLayout containerLayout = (LinearLayout) findViewById(R.id.container_layout);

When next and prev are clicked, navigate through the db, update the textviews,imageviews etc and fire the animations 单击next和prev时,浏览数据库,更新textviews,imageviews等并触发动画

previousButtonOnClick {
    //navigate db and update views 
    containerLayout.startAnimation(previousAnim);
}

nextButtonOnClick {
    //navigate db and update views 
    containerLayout.startAnimation(nextAnim);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM