android共享元素动画

In this tutorial we’ll implement a different kind of animation transition namely Shared Element Transition between activities.

在本教程中,我们将实现另一种动画过渡,即活动之间的共享元素过渡

Android共享元素过渡动画 (Android Shared Element Transition Animation)

Android Shared Element Transition determines how shared element views are animated from one Activity/Fragment to another during a scene transition.

Android共享元素过渡确定在场景过渡期间如何将共享元素视图从一个活动/片段动画化为另一个活动/片段。

In pre-Lollipop devices Android used to support transitions between activities and fragments that involved transitions of the entire view hierarchies. However there are many cases when a view (let’s say ListView) consists of different row items. More often than not, clicking any row would show details of that respective row in the next screen. So to emphasise continuity between the two activities, we’ll show a circular reveal animation. This improves the user experience by drawing their focus towards the relationship between the new screen and the previous screen. A Shared Element Transition like this is more commonly seen in music playlist apps.

在Lollipop之前的设备中,Android用于支持活动和涉及整个视图层次结构过渡的片段之间的过渡。 但是,在许多情况下,视图(例如ListView)由不同的行项目组成。 通常,单击任何行都会在下一个屏幕中显示相应行的详细信息。 因此,为了强调两个活动之间的连续性,我们将展示一个圆形的揭示动画。 通过将注意力集中在新屏幕和先前屏幕之间的关系上,可以改善用户体验。 这样的共享元素转换在音乐播放列表应用中更为常见。

Note: This type of transition works only for android SDK>21.

注意:这种类型的转换仅适用于android SDK> 21。

Let’s begin the implementation of the app. In this tutorial we’ll implement custom ListView rows and show the desired transition for each of them.

让我们开始执行该应用程序。 在本教程中,我们将实现自定义ListView行,并为每个行显示所需的过渡。

Android共享元素过渡动画项目结构 (Android Shared Element Transition Animation Project Structure)

This project consists of 2 activities and a CustomAdapter for the ListView.

该项目包括2个活动和一个ListView的CustomAdapter。

Android过渡动画–共享元素过渡代码 (Android Transition Animation – Shared Element Transition Code)

To enable this transitions add the following snippet inside the AppTheme tag in styles.xml.

要启用此转换,请在styles.xml的AppTheme标记内添加以下代码段。

<item name="android:windowContentTransitions">true</item>

For both the layouts with this transition we need to assign a android:transitionName attribute.

对于这两个具有此过渡的布局,我们需要分配android:transitionName属性。

The activity_main.xml populates a ListView and the details_activity.xml is for the the details screen. Both are shown below.

activity_main.xml填充一个ListView,而details_activity.xml用于详细信息屏幕。 两者都显示如下。

<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:transitionName="@string/transition"android:orientation="vertical"><ListViewandroid:layout_width="wrap_content"android:id="@+id/list_view"android:layout_height="wrap_content"/></LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayoutxmlns:android="https://schemas.android.com/apk/res/android"xmlns:tools="https://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:padding="@dimen/activity_horizontal_margin"android:id="@+id/layout"android:transitionName="@string/transition"tools:context="com.journaldev.sharedelementtransition.MainActivity"><TextViewandroid:gravity="center"android:textColor="@android:color/white"android:id="@+id/heading"android:layout_width="match_parent"android:textAppearance="?android:attr/textAppearanceLarge"android:layout_height="wrap_content" /><TextViewandroid:gravity="center"android:id="@+id/language"android:textColor="@android:color/white"android:layout_width="match_parent"android:textAppearance="?android:attr/textAppearanceMedium"android:layout_height="wrap_content"android:layout_below="@+id/heading"android:layout_alignParentLeft="true"android:layout_alignParentStart="true" /><TextViewandroid:gravity="center"android:id="@+id/desc"android:textColor="@android:color/white"android:layout_width="match_parent"android:textAppearance="?android:attr/textAppearanceMedium"android:layout_height="wrap_content"android:layout_centerInParent="true"/></RelativeLayout>

As you can see a android:transitionName attribute is declared as a string in the root view of both the layouts.

如您所见,在两个布局的根视图中android:transitionName属性都声明为字符串。

We’ve created a custom ListView which populates its layout from a ArrayList of String arrays. The layout and adapter of the ListView are given below.

我们创建了一个自定义ListView,它从String数组的ArrayList填充其布局。 ListView的布局和适配器如下。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"android:orientation="vertical" android:layout_width="match_parent"android:padding="@dimen/activity_horizontal_margin"android:background="@color/md_black_1000"android:layout_margin="5dp"android:id="@+id/rl"android:layout_height="wrap_content"><TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:textAppearance="?android:attr/textAppearanceLarge"android:id="@+id/primary_textview"android:gravity="center"android:textColor="@android:color/white"/><TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:textAppearance="?android:attr/textAppearanceMedium"android:id="@+id/textView"android:layout_below="@+id/primary_textview"android:textColor="@android:color/white"android:gravity="center"/></RelativeLayout>
public class CustomAdapter extends BaseAdapter {ArrayList<String[]> arrayList;Context c;public CustomAdapter(Context c, ArrayList<String[]> list) {arrayList = list;this.c = c;}@Overridepublic int getCount() {// TODO Auto-generated method stubreturn arrayList.size();}@Overridepublic Object getItem(int position) {// TODO Auto-generated method stubreturn arrayList.get(position);}@Overridepublic long getItemId(int position) {// TODO Auto-generated method stubreturn position;}@Overridepublic View getView(int position, View convertView, ViewGroup parent) {// TODO Auto-generated method stubView row = null;LayoutInflater inflater = (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);if (convertView == null) {row = inflater.inflate(R.layout.row_layout, parent,false);} else {row = convertView;}String[] detail = arrayList.get(position);RelativeLayout rl= (RelativeLayout)row.findViewById(R.id.rl);rl.setBackgroundColor(Color.parseColor(detail[3]));TextView name = (TextView) row.findViewById(R.id.primary_textview);name.setText(detail[0]);TextView email = (TextView) row.findViewById(R.id.textView);email.setText(detail[1]);return row;}}

The MainActivity.java and DetailsActivity.java are given below.

MainActivity.javaDetailsActivity.java如下所示。

package com.journaldev.sharedelementtransition;import android.content.Intent;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.ActivityOptionsCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;import java.util.ArrayList;public class MainActivity extends AppCompatActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);final ArrayList<String[]> values = new ArrayList<String[]>();values.add(new String[]{"Android", "Java", getString(R.string.android),'#' + Integer.toHexString(getResources().getColor(R.color.md_light_green_900))});values.add(new String[]{"iOS", "Swift", getString(R.string.ios),'#' + Integer.toHexString(getResources().getColor(R.color.md_amber_A700))});values.add(new String[]{"Xamarin", "C#",getString(R.string.xamarin),'#' + Integer.toHexString(getResources().getColor(R.color.md_pink_A700))});values.add(new String[]{"PhoneGap", "HTML CSS and JScript",getString(R.string.phonegap),'#' + Integer.toHexString(getResources().getColor(R.color.md_brown_800))});ListView listView = (ListView) findViewById(R.id.list_view);CustomAdapter adapter = new CustomAdapter(this, values);listView.setAdapter(adapter);listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {@Overridepublic void onItemClick(AdapterView<?> parent, View view, int position, long id) {Intent intent = new Intent(MainActivity.this, DetailsActivity.class);intent.putExtra("array",values.get(position));// Get the transition name from the stringString transitionName = getString(R.string.transition);ActivityOptionsCompat options =ActivityOptionsCompat.makeSceneTransitionAnimation(MainActivity.this,view,   // Starting viewtransitionName    // The String);ActivityCompat.startActivity(MainActivity.this, intent, options.toBundle());}});}
}

When an activity is finished, instead of finish() we invoke ActivityCompat.finishAfterTransition(this); as shown in the code below.

活动结束后,我们将调用ActivityCompat.finishAfterTransition(this);而不是finish( ActivityCompat.finishAfterTransition(this); 如下面的代码所示。

public class DetailsActivity extends AppCompatActivity {@Overrideprotected void onCreate(@Nullable Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.details_activity);String[] array= getIntent().getStringArrayExtra("array");RelativeLayout rl= (RelativeLayout)findViewById(R.id.layout);rl.setBackgroundColor(Color.parseColor(array[3]));TextView textView= (TextView)findViewById(R.id.heading);textView.setText(array[0]);TextView type= (TextView)findViewById(R.id.language);type.setText(array[1]);TextView desc=(TextView)findViewById(R.id.desc);desc.setText(array[2]);}@Overridepublic void onBackPressed() {ActivityCompat.finishAfterTransition(this);}
}

The output of the application in action is given below.

实际应用程序的输出如下。

This brings an end to this tutorial. You can download the final Android Transition Animation – Shared Element Transition Project from the link below.

本教程到此结束。 您可以从下面的链接下载最终的Android Transition Animation –共享元素过渡项目

Download Android Transition Animation Project下载Android Transition Animation Project

翻译自: https://www.journaldev.com/10473/android-shared-element-transition-animation

android共享元素动画

android共享元素动画_Android共享元素过渡动画相关推荐

  1. android过渡动画软件,安卓P过渡动画下载-安卓P过渡动画 v1.0_手机乐园

    软件简介 安卓P过渡动画是一款xp模块,免费的而且安装包也是超级的小,操作起来比较方便,可以从Android P-jfy中进行模块的分离,通过进行定义每一个关键帧的元素属性值进行复杂动画效果的轻松实现 ...

  2. android 官方默认动画,Android动画一:Activity过渡动画详细实现原理

    虽然 Android 5.0 之后推出了新的过渡动画方式,但通常只是用于特定的场合使用,activity.overridePendingTransition() 通用方式的过渡动画还是很常用. 原理分 ...

  3. android图片缩放动画,Android开发笔记——点击查看大图过渡动画与图片缩放与移动...

    从一个activity到另一个activity的过渡 1.小图点击事件代码@Override public void onClick(View view) { switch (view.getId() ...

  4. Vue 动画 —— 滑动切换动画 / 滑动翻页过渡动画——从顶部到底部、从底部到顶部、从左侧到右侧、从右侧到左侧

    要点: 动画容器为相对定位,动画元素为绝对定位 每个动画元素都需单独用 <transition></transition> 包裹来添加过渡动画效果 完整演示范例代码 <t ...

  5. flash一个按钮控制动画_flutter闪屏过渡动画,闪光占位动画

    在程序设计的理念中,讲究一切都来源于物理世界,在现实世界中,人们在每接触到一个新的事物或者说在手指触碰到一个事物时,总是心里默许期望有一个反馈效果,这就是来源于心底深处常常被人忽略的一个潜在期望. 在 ...

  6. ios css动画残影,CSS 过渡动画在IOS中表现异常

    设置了一个隐藏的圆环,在页面开始时旋转露出.发现在IOS(safari)中过渡动画展示不完整,只展示了起始部分,后面直接跳转到结束状态了. demo .wrap { width: 200px; hei ...

  7. android 按键上浮动画_android – 浮动动作按钮动画

    从@Zielony的回答中,我确切地说到了我想要的地方. 下面是正确应用效果的代码. scale_fab_in.xml android:duration="500" android ...

  8. java逐帧动画_android的逐帧动画

    逐帧动画是指按照给定的顺序轮流显示一定数目的图像而产生的动画效果.一般应用于比较简单的场景,比如一个监测手机wifi信号强度的应用中,不断的显示wifi信号的强弱程度就比较适合用逐帧动画. 逐帧动画的 ...

  9. ue4树叶飘落动画_Android:使用属性动画制作器的类似于树叶的动画

    ue4树叶飘落动画 在上一教程中,我们解释了属性动画通常如何工作. 现在,我们将进一步讲解如何创建属性动画,该动画将为ImageView产生类似于树叶飘落的效果,在其中我们显然将放置树叶的图像. 为此 ...

最新文章

  1. 解题报告:luogu P1688 新单词接龙问题【trie树、dfs、DP递推】
  2. Making Your Own iPhone Frameworks. In Xcode
  3. python自动化测试视频百度云-Python接口自动化测试视频教程下载
  4. WebRTC各种资料集合
  5. mysql 读写引擎_揭秘MySQL存储引擎spider
  6. res.status === 200含义
  7. java 数组 c foreach_在Java 8中,为什么Arrays没有给出forEach的Iterable方法?
  8. java电脑上运行_java-在本地计算机上运行的Web应用程序
  9. 交错排列(Alternating Permutation)问题详解
  10. java安装选择哪个可选功能_java章节习题及期末考试题答案.doc
  11. 如何快速水一篇NLP论文?
  12. java hibernate 插入数据_hibernate 批量插入数据
  13. 一文读懂《“十四五”软件和信息技术服务业发展规划》
  14. 计算机应该玩什么游戏,电脑玩游戏主要靠什么配置
  15. Exception in thread “main“ org.apache.spark.sql.catalyst.parser.ParseException: extraneous input ‘$
  16. 819A - 如何成为一名职业程序员
  17. java程序设计高级教程答案_Java高级程序设计实战教程答案
  18. 五、动态软件体系结构
  19. Android: SQLite + ListView 实现 新闻 App
  20. 蓝牙协议分析(2)_协议架构

热门文章

  1. Nautilus获得了标签化支持
  2. 手机网页 复制信息方法 免费短信
  3. [转载] python中字典copy_python深度复制字典,copy方法与deepcopy方法
  4. [转载] python isinstance()方法的使用
  5. C#使用并行任务库(TPL)
  6. JFinal Web开发学习(一)开启HelloWorld
  7. less(css)语言快速入门
  8. sed与正则用法收集
  9. 【IE】IE对line-height 失效的的解决方案
  10. HDU 2087 剪花布条 KMP入门