Android 5.0 转场动画的使用

Google在Android 5.0之后为了提高用户体验,增加了几种特殊的转场动画,简单的说也就是activity与activity之间进行跳转的动画效果,接下来对这几种动画效果做简要的说明

要使用这几种特殊的转场方式,首先我们要激活Activity 元素中的过度效果

使用代码
   
   
  1. getWindow().requestFeature(Window.FEATURE_CONTENT_TRANSITIONS);
注意:此行代码只需在需要使用startActivity()方法的活动中使用

接下来我们以一个简答的例子来对每一个转场动画做一个简单的介绍,要使用转场动画,肯定需要多个Activity
这里我们以MainActivity作为程序的主入口(其实页面很简单,就是几个Button ,点击不同的Button跳转到不同的Activty用以测试不同的转场动画)
先放代码,然后做解释
MainActivity 的布局文件如下
   
   
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:tools="http://schemas.android.com/tools"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. android:layout_margin="16dp"
  7. android:orientation="vertical"
  8. tools:context="com.example.jin.myarttest.MainActivity">
  9. <Button
  10. android:id="@+id/button1"
  11. android:layout_width="match_parent"
  12. android:layout_height="wrap_content"
  13. android:text="按钮1" />
  14. <Button
  15. android:id="@+id/button2"
  16. android:layout_width="match_parent"
  17. android:layout_height="wrap_content"
  18. android:layout_marginTop="16dp"
  19. android:text="按钮2" />
  20. <Button
  21. android:id="@+id/button3"
  22. android:layout_width="match_parent"
  23. android:layout_height="wrap_content"
  24. android:layout_marginTop="16dp"
  25. android:text="按钮3" />
  26. <Button
  27. android:id="@+id/button4"
  28. android:layout_width="match_parent"
  29. android:layout_height="wrap_content"
  30. android:layout_marginTop="16dp"
  31. android:text="按钮4" />
  32. <android.support.design.widget.FloatingActionButton
  33. android:id="@+id/floating_btn"
  34. android:layout_width="wrap_content"
  35. android:layout_height="wrap_content"
  36. android:layout_marginTop="16dp"
  37. android:transitionName="mybtn" />
  38. <Button
  39. android:id="@+id/btn_shenqi"
  40. android:layout_width="wrap_content"
  41. android:layout_height="wrap_content"
  42. android:layout_marginTop="16dp"
  43. android:text="好神奇"
  44. android:transitionName="btn" />
  45. </LinearLayout>

MainActivity.class的代码如下
   
   
  1. package com.example.jin.myarttest;
  2. import android.app.ActivityOptions;
  3. import android.content.Intent;
  4. import android.os.Bundle;
  5. import android.support.design.widget.FloatingActionButton;
  6. import android.support.v7.app.AppCompatActivity;
  7. import android.util.Pair;
  8. import android.view.View;
  9. import android.view.Window;
  10. import android.widget.Button;
  11. public class MainActivity extends AppCompatActivity implements View.OnClickListener {
  12. private int NOTIFICATION_ID = 00000000;
  13. private Button button1;
  14. private Button button2;
  15. private Button button3;
  16. private Button button4;
  17. private FloatingActionButton floating_btn;
  18. private Button btn_shenqi;
  19. @Override
  20. protected void onCreate(Bundle savedInstanceState) {
  21. super.onCreate(savedInstanceState);
  22. // 激活Activity元素中的过度效果,一定要写在setContentView()方法之前
  23. getWindow().requestFeature(Window.FEATURE_CONTENT_TRANSITIONS);
  24. setContentView(R.layout.activity_main);
  25. initView();
  26. }
  27. /**
  28. * 初始化页面
  29. */
  30. private void initView() {
  31. button1 = (Button) findViewById(R.id.button1);
  32. button1.setOnClickListener(this);
  33. button2 = (Button) findViewById(R.id.button2);
  34. button2.setOnClickListener(this);
  35. button3 = (Button) findViewById(R.id.button3);
  36. button3.setOnClickListener(this);
  37. button4 = (Button) findViewById(R.id.button4);
  38. button4.setOnClickListener(this);
  39. floating_btn = (FloatingActionButton) findViewById(R.id.floating_btn);
  40. btn_shenqi = (Button) findViewById(R.id.btn_shenqi);
  41. }
  42. @Override
  43. public void onClick(View view) {
  44. switch (view.getId()) {
  45. case R.id.button1:
  46. /**
  47. * 用以测试分解效果
  48. */
  49. startActivity(new Intent(getApplicationContext(), Main2Activity.class), ActivityOptions.makeSceneTransitionAnimation(this).toBundle());
  50. break;
  51. case R.id.button2:
  52. /**
  53. * 用以测试滑动效果
  54. */
  55. startActivity(new Intent(getApplicationContext(), Main3Activity.class), ActivityOptions.makeSceneTransitionAnimation(this).toBundle());
  56. break;
  57. case R.id.button3:
  58. /**
  59. * 用以测试淡入淡出效果
  60. */
  61. startActivity(new Intent(getApplicationContext(), Main4Activity.class), ActivityOptions.makeSceneTransitionAnimation(this).toBundle());
  62. break;
  63. case R.id.button4:
  64. /**
  65. * 用以测试共享元素效果
  66. */
  67. //两个页面共享单个元素
  68. // startActivity(new Intent(getApplicationContext(), Main5Activity.class), ActivityOptions.makeSceneTransitionAnimation(this,floating_btn,"mybtn").toBundle());
  69. /**
  70. * 共享多个元素
  71. */
  72. startActivity(new Intent(getApplicationContext(), Main5Activity.class), ActivityOptions.makeSceneTransitionAnimation(
  73. this,
  74. Pair.create(((View) floating_btn), "mybtn"),
  75. Pair.create(((View) btn_shenqi), "btn")).toBundle());
  76. break;
  77. }
  78. }
  79. }

1 分解动画需要在目标活动中设置
   
   
  1. getWindow().setEnterTransition(new Explode().setDuration(500));
  2. getWindow().setExitTransition(new Explode().setDuration(500));
2 滑动动画需要在目标Activity中设置
   
   
  1. getWindow().setEnterTransition(new Slide().setDuration(500));
  2. getWindow().setExitTransition(new Slide().setDuration(500));
 淡入淡出效果需要在目标Activity中设置
   
   
  1. getWindow().setEnterTransition(new Fade().setDuration(2000));
  2. getWindow().setExitTransition(new Fade().setDuration(2000));

4 共享元素动画相比于其他三种动画稍微的复杂一些,首先,我们要给需要共享的元素设置 transitionName 属性并且两个需要共享的元素 transitionName 必须相同。
共享元素动画又分为单个共享元素和多个共享元素
如果是单个共享元素,需要使用
   
   
  1. startActivity(new Intent(getApplicationContext(), Main5Activity.class), ActivityOptions.makeSceneTransitionAnimation(this,floating_btn,"mybtn").toBundle());
其中第三个参数即是设置的transitionName的名字
假如有多个共享元素
   
   
  1. /**
  2. * 共享多个元素
  3. */
  4. startActivity(new Intent(getApplicationContext(), Main5Activity.class), ActivityOptions.makeSceneTransitionAnimation(this,Pair.create(((View) floating_btn), "mybtn"),Pair.create(((View) btn_shenqi), "btn")).toBundle());

本次就介绍到这里,我们下次再见,更多技术咨询请关注我的微信公众号


  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值