淡入淡出动画(也被称为渐隐)逐渐淡出一个UI组件,同时淡入另一个。这个动画在你想在你的应用程序中切换内容或者是视图的情况下非常有用。淡入淡出非常微妙并短,但支持从一个屏幕到下一个屏幕流畅的过渡。当你不使用它们的时候,然而,过渡经常感觉生硬和仓促。

下面是从一个进度指示器到一些文本内容渐变的例子。

如果你想跳过并查看一个完整的工作示例,下载并运行这个实例应用,并选着渐变例子。查看下面的文件的代码实现:

  • src/CrossfadeActivity.java

  • layout/activity_crossfade.xml

  • menu/activity_crossfade.xml

创建视图

————————————————————————————————————————————-------------------------------------------------------——

创建两个你想渐变的视图。下面的例子创建了一个过程指示器和一个滑动文本视图:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"> <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent"> <TextView style="?android:textAppearanceMedium"
android:lineSpacingMultiplier="1.2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/lorem_ipsum"
android:padding="16dp" /> </ScrollView> <ProgressBar android:id="@+id/loading_spinner"
style="?android:progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" /> </FrameLayout>

设置动画

——————————————————————————————————————————————————————————————————————

为了设置动画:

  1. 创建你想渐变的视图的成员变量。你稍后在动画过程中修改视图时需要这些引用。

  2. 对于淡入的视图,设置它的可见性为GONE。它阻止了视图占用布局控件,并在布局计算中忽略它,加快处理时间。

  3. 在一个成员变量中缓存config_shortAnimTime系统属性。这个属性定了一个标准的“短”动画的时间段。这个时间对于微妙的动画或者经常发生的动画是理想的。如果你想使用它们,config_longAnimTime和config_mediumAnimTime也是有效的。

下面是一个例子,使用前面代码块的布局作为Activity的内容视图:

public class CrossfadeActivity extends Activity { 

   private View mContentView;
private View mLoadingView;
private int mShortAnimationDuration; ... @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_crossfade); mContentView = findViewById(R.id.content);
mLoadingView = findViewById(R.id.loading_spinner); // Initially hide the content view.
mContentView.setVisibility(View.GONE); // Retrieve and cache the system's default "short" animation time.
mShortAnimationDuration = getResources().getInteger(
android.R.integer.config_shortAnimTime);
}

淡入淡出视图

——————————————————————————————————————————————————--------------———————————

现在这个试图是正确的设置,通过执行下面的操作渐入渐出它们:

  1. 对于渐入的视图,从0到可见性VISIBLE设置透明度值.(记住它初始化为GONE.)它会使的这个视图可见但是完全透明.

  2. 对于渐入的视图,推动它的透明值从0到1.在这个时候,对于这个渐出的视图,推动它的透明值从1到0.

  3. 在一个Animator.AnimatorListener中使用nonAnimationEnd()方法,设置淡出为GONE的视图的可见性。即使alpha值为0 ,设置这个视图的可见性为GONE来阻止视图占用布局空间,并在布局计算器的时候忽略它,加快处理。

下面的方法显示了如何执行这个的例子:

private View mContentView;
private View mLoadingView;
private int mShortAnimationDuration; ... private void crossfade() { // Set the content view to 0% opacity but visible, so that it is visible
// (but fully transparent) during the animation.
mContentView.setAlpha(0f);
mContentView.setVisibility(View.VISIBLE); // Animate the content view to 100% opacity, and clear any animation
// listener set on the view.
mContentView.animate()
.alpha(1f)
.setDuration(mShortAnimationDuration)
.setListener(null); // Animate the loading view to 0% opacity. After the animation ends,
// set its visibility to GONE as an optimization step (it won't
// participate in layout passes, etc.)
mHideView.animate()
.alpha(0f)
.setDuration(mShortAnimationDuration)
.setListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
mHideView.setVisibility(View.GONE);
}
});
}

Android Developers:两个视图渐变的更多相关文章

  1. 【Android Developers Training】 69. 视图切换的淡入淡出效果

    注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...

  2. 【Android Developers Training】 72. 缩放一个视图

    注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...

  3. 【Android Developers Training】 65. 应用投影和相机视图

    注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...

  4. 【Android Developers Training】 71. 显示翻牌动画

    注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...

  5. 【Android Developers Training】 63. 定义形状

    注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...

  6. 【Android Developers Training】 62. 搭建一个OpenGL ES环境

    注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...

  7. 【Android Developers Training】 61. 序言:使用OpenGL ES显示图像

    注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...

  8. 【Android Developers Training】 9. 覆盖于布局之上的Action Bar

    注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...

  9. 【Android Developers Training】 8. 定义Action Bar风格

    注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...

随机推荐

  1. mac 连接linux

    1.ssh 通过ssh 的方式直接连接linux ssh name@ip -22 例:ssh  zyc@192.168.1.100 -22 这个的前提是linux 要开启ssh 服务 先看一下linu ...

  2. Prototype 模式

    Prototype 模式提供了一个通过已存在对象进行新对象创建的接口(Clone) ,Clone()实现和具体的实现语言相关,在 C++中我们将通过拷贝构造函数实现之. /////////////// ...

  3. SGU 128.Snake

    时间限制:0.25s 空间限制:4m 题意: 在一个平面坐标中有N个点,现在要你用这N个点构造一个闭合图形,这个图形要满足以下条件: 1.这个图形要是闭合的:          2.图形上的点只能是给 ...

  4. NPOI读写Excel0307

    #region NPOI 操作 Excel 2007 /// <summary> /// 将Excel文件中的数据读出到DataTable中(xlsx) /// </summary& ...

  5. .htaccess内容

    <IfModule mod_rewrite.c> Options +FollowSymlinks RewriteEngine On RewriteCond %{REQUEST_FILENA ...

  6. 完美PNG半透明窗体解决方案

    当年Vista系统刚出来的时候,最吸引人的莫过于半透明磨砂的窗体界面了,迷倒了多少人.这个界面技术随即引发了编程界的一阵骚动,很多人都在问:如何实现这一界面效果?当然,在Vista下倒是很简单,系统本 ...

  7. Python 异常处理--raise函数用法

    raise语句手工引发一个异常: "raise" [expression ["," expression ["," expression]] ...

  8. Python Tutorial 学习(三)--An Informal Introduction to Python

    3.1. 将Python用作计算器 3.1.1. Numbers 数 作为一个计算器,python支持简单的操作, '+','-','*','/'地球人都知道的加减乘除. ()可以用来改变优先级,同数 ...

  9. MySQL zip版安装配置

    文章出处:http://www.cnblogs.com/winstic/,请保留此连接 这段时间在学习Python 数据库操作知识,简单整理MySQL zip文件安装方法 下载 在MySQL官网htt ...

  10. 啊哈,yield

    文章出处:http://www.cnblogs.com/winstic/,请保留此连接 在python编程中,我们经常会看到函数中带有yield关键字,但请注意,此时的函数不再是我们熟知的一般函数,而 ...