Android的碎片和动画 [英] Android Fragments and animation

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

问题描述

如何实现滑动的那种例如蜂窝的Gmail客户端使用?

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

能否事务管理通过添加和删除的片段会自动处理这个问题,它是一种很难对此进行测试,由于模拟器是一个幻灯片:)

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).

下面code说明了如何更换片段通过滑动出一个片段,滑动另一个在它的地方。

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.show ft.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天全站免登陆