android 卡片旋转动画,Android-显示卡片翻转的动画片效果

Android---显示卡片翻转的动画效果

本文译自:http://developer.android.com/training/animation/cardflip.html

本文介绍如何是一个自定义的Fragment动画来制作卡片翻转动画。卡片翻转动画是在内容视图之间模拟卡片翻转的效果。

创建动画器

创建用于卡片翻转的动画,需要两个用于前景的动画器,它们会让卡片的前景向左侧退出,从左侧进入。还需要两个用于背景的动画器,它们会让卡片的背景从右侧进入,向右侧退出。

card_filp_left_in.xml

android:valueFrom="1.0"

android:valueTo="0.0"

android:propertyName="alpha"

android:duration="0" />

android:valueFrom="-180"

android:valueTo="0"

android:propertyName="rotationY"

android:interpolator="@android:interpolator/accelerate_decelerate"

android:duration="@integer/card_flip_time_full" />

android:valueFrom="0.0"

android:valueTo="1.0"

android:propertyName="alpha"

android:startOffset="@integer/card_flip_time_half"

android:duration="1" />

card_flip_left_out.xml

android:valueFrom="0"

android:valueTo="180"

android:propertyName="rotationY"

android:interpolator="@android:interpolator/accelerate_decelerate"

android:duration="@integer/card_flip_time_full" />

android:valueFrom="1.0"

android:valueTo="0.0"

android:propertyName="alpha"

android:startOffset="@integer/card_flip_time_half"

android:duration="1" />

card_flip_right_in.xml

android:valueFrom="1.0"

android:valueTo="0.0"

android:propertyName="alpha"

android:duration="0" />

android:valueFrom="180"

android:valueTo="0"

android:propertyName="rotationY"

android:interpolator="@android:interpolator/accelerate_decelerate"

android:duration="@integer/card_flip_time_full" />

android:valueFrom="0.0"

android:valueTo="1.0"

android:propertyName="alpha"

android:startOffset="@integer/card_flip_time_half"

android:duration="1" />

card_flip_right_out.xml

android:valueFrom="0"

android:valueTo="-180"

android:propertyName="rotationY"

android:interpolator="@android:interpolator/accelerate_decelerate"

android:duration="@integer/card_flip_time_full" />

android:valueFrom="1.0"

android:valueTo="0.0"

android:propertyName="alpha"

android:startOffset="@integer/card_flip_time_half"

android:duration="1" />

创建View

“卡片”每个面都是一个独立的布局,布局中可以包含任何你想要的内容,如文字、图像,或者是任意View的组合。然后,要在随后的Fragment动画中使用这两个布局。以下是创建了一个显示“卡片”文本面的布局:

android:layout_height="match_parent"

android:orientation="vertical"

android:background="#a6c"

android:padding="16dp"

android:gravity="bottom">

style="?android:textAppearanceLarge"

android:textStyle="bold"

android:textColor="#fff"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="@string/card_back_title" />

android:textAllCaps="true"

android:textColor="#80ffffff"

android:textStyle="bold"

android:lineSpacingMultiplier="1.2"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="@string/card_back_description" />

以下是显示“卡片”图片面的布局

android:layout_height="match_parent"

android:src="@drawable/image1"

android:scaleType="centerCrop"

android:contentDescription="@string/description_image_1" />

创建Fragment

给“卡片”的前后两面创建Fragment类。这些Fragment类会在它的onCreateView()方法中返回你之前创建的布局。然后,在你要显示卡片的Activity中创建相应的Fragment类的实例。以下实例说明了嵌入到它的父Activity内部的Fragment类的使用方法:

publicclassCardFlipActivityextendsActivity{...

/**

* A fragment representing the front of thecard.

*/

public class CardFrontFragment extends Fragment {

@Override

public View onCreateView(LayoutInflater inflater, ViewGroup container,

Bundle savedInstanceState) {

return inflater.inflate(R.layout.fragment_card_front,container, false);

}

}

/**

* A fragment representing the back of thecard.

*/

public class CardBackFragment extends Fragment {

@Override

public View onCreateView(LayoutInflaterinflater, ViewGroup container,

Bundle savedInstanceState) {

return inflater.inflate(R.layout.fragment_card_back,container, false);

}

}

}

让卡片翻转

现在,你需要在一个父Activity内显示Fragment。首先给你的Activity创建布局。下例中创建了一个可以在运行时添加Fragment的FrameLayout布局:

android:layout_width="match_parent"

android:layout_height="match_parent" />

在Activity代码中,把内容视图设置给刚刚创建的布局。这也是创建Activity时,显示默认Fragment的好主意,因此下例会说明如何显示默认的“卡片”的前面:

publicclassCardFlipActivityextendsActivity{@Override

protected void onCreate(BundlesavedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_activity_card_flip);

if (savedInstanceState == null) {

getFragmentManager()

.beginTransaction()

.add(R.id.container, new CardFrontFragment())

.commit();

}

}

...

}

现在,你有了卡片的前景显示,这样就可以在适当的时候,用翻转动画来显示卡片的背景。以下是创建一个用于显示卡片另一面的方法的步骤:

1.给Fragment变换设置自定义动画;

2.用一个新的Fragment来替换当前的的Fragment,并且使用你创建的动画让这个事件动起来;

3.把被替换的Fragment添加到Fragment的回退堆栈中,以便在用户按下回退按钮时,该卡片会翻转返回。

privatevoidflipCard(){if (mShowingBack) {

getFragmentManager().popBackStack();

return;

}

// Flip to the back.

mShowingBack = true;

// Create and commit a newfragment transaction that adds the fragment for the back of

// the card, uses customanimations, and is part of the fragment manager's back stack.

getFragmentManager()

.beginTransaction()

// Replace the default fragment animations with animator resourcesrepresenting

// rotations when switching to the back of the card, as well asanimator

// resources representing rotations when flipping back to the front(e.g. when

// the system Back button is pressed).

.setCustomAnimations(

R.animator.card_flip_right_in, R.animator.card_flip_right_out,

R.animator.card_flip_left_in, R.animator.card_flip_left_out)

// Replace any fragments currently in the container view with afragment

// representing the next page (indicated by the just-incrementedcurrentPage

// variable).

.replace(R.id.container, new CardBackFragment())

// Add this transaction to the back stack, allowing users to pressBack

// to get to the front of the card.

.addToBackStack(null)

// Commit the transaction.

.commit();

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值