Android 片段和动画 [英] Android Fragments and animation

查看:21
本文介绍了Android 片段和动画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您应该如何实现 Honeycomb Gmail 客户端使用的那种滑动?

How should you implement the sort of sliding that for example the Honeycomb Gmail client uses?

TransactionManager 能否通过添加和删除 Fragment 来自动处理这个问题,由于模拟器是幻灯片,因此很难对此进行测试:)

Can TransactionManager handle this automatically by adding and removing the Fragments, it's kind of difficult to test this due to the emulator being a slideshow :)

推荐答案

要动画片段之间的过渡,或动画显示或隐藏片段的过程,请使用 片段管理器 创建一个分片交易.

To animate the transition between fragments, or to animate the process of showing or hiding a fragment you use the Fragment Manager to create a Fragment Transaction.

在每个片段事务中,您可以指定分别用于显示和隐藏的输入和输出动画(或在使用替换时两者).

Within each Fragment Transaction you can specify in and out animations that will be used for show and hide respectively (or both when replace is used).

以下代码显示了如何通过滑出一个片段并将另一个片段滑入其位置来替换一个片段.

The following code shows how you would replace a fragment by sliding out one fragment and sliding the other one in it's place.

FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.setCustomAnimations(R.anim.slide_in_left, R.anim.slide_out_right);

DetailsFragment newFragment = DetailsFragment.newInstance();

ft.replace(R.id.details_fragment_container, newFragment, "detailFragment");

// Start the animated transition.
ft.commit();

要通过隐藏或显示片段实现相同的目的,您只需调用 ft.showft.hide,传入您希望显示或分别隐藏.

To achieve the same thing with hiding or showing a fragment you'd simply call ft.show or ft.hide, passing in the Fragment you wish to show or hide respectively.

作为参考,XML 动画定义将使用 objectAnimator 标记.slide_in_left 的示例可能如下所示:

For reference, the XML animation definitions would use the objectAnimator tag. An example of slide_in_left might look something like this:

<?xml version="1.0" encoding="utf-8"?>
<set>
  <objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
    android:propertyName="x" 
    android:valueType="floatType"
    android:valueFrom="-1280"
    android:valueTo="0" 
    android:duration="500"/>
</set>

这篇关于Android 片段和动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆