Android Developers:两个视图渐变
淡入淡出动画(也被称为渐隐)逐渐淡出一个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>
设置动画
——————————————————————————————————————————————————————————————————————
为了设置动画:
创建你想渐变的视图的成员变量。你稍后在动画过程中修改视图时需要这些引用。
对于淡入的视图,设置它的可见性为GONE。它阻止了视图占用布局控件,并在布局计算中忽略它,加快处理时间。
在一个成员变量中缓存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);
}
淡入淡出视图
——————————————————————————————————————————————————--------------———————————
现在这个试图是正确的设置,通过执行下面的操作渐入渐出它们:
对于渐入的视图,从0到可见性VISIBLE设置透明度值.(记住它初始化为GONE.)它会使的这个视图可见但是完全透明.
对于渐入的视图,推动它的透明值从0到1.在这个时候,对于这个渐出的视图,推动它的透明值从1到0.
在一个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:两个视图渐变的更多相关文章
- 【Android Developers Training】 69. 视图切换的淡入淡出效果
注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...
- 【Android Developers Training】 72. 缩放一个视图
注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...
- 【Android Developers Training】 65. 应用投影和相机视图
注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...
- 【Android Developers Training】 71. 显示翻牌动画
注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...
- 【Android Developers Training】 63. 定义形状
注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...
- 【Android Developers Training】 62. 搭建一个OpenGL ES环境
注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...
- 【Android Developers Training】 61. 序言:使用OpenGL ES显示图像
注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...
- 【Android Developers Training】 9. 覆盖于布局之上的Action Bar
注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...
- 【Android Developers Training】 8. 定义Action Bar风格
注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...
随机推荐
- SGU 179.Brackets light
时间限制:0.25s 空间限制:12M 题意 给定一个合法的仅由'(',')'组成的括号序列,求它的下一个合法排列.假定'('<')'. Solution: ...
- POJ 1236.Network of Schools (强连通)
首先要强连通缩点,统计新的图的各点的出度和入度. 第一问直接输出入度为0的点的个数 第二问是要是新的图变成一个强连通图,那么每一个点至少要有一条出边和一条入边,输出出度和入度为0的点数大的那一个 注意 ...
- css3基础教程十六变形与动画animation
前面我们讲过的变形与动画一般都是通过鼠标的单击.获得焦点,被点击或对元素进行一定改变后以后触发效果的,那么有没有像Flash一样自动播放的动画效果呢?答案当然是肯定的,这就是我们今天要讲到的anima ...
- (转载)css垂直水平居中的整理
方法一 .demo1 { width:180px; height:180px; line-height:180px; *font-size:160px; border:1px solid #ddd; ...
- 翻译-让ng的$http服务与jQuerr.ajax()一样易用
Make AngularJS $http service behave like jQuery.ajax() 让ng的$http服务与jQuerr.ajax()一样易用 作者zeke There is ...
- yii 验证用户名是否存在 array("name","unique",'message'=>'用户名已经存在'),
//验证用户名是否存在 array("name","unique",'message'=>'用户名已经存在'),
- vm拷贝cloudera-scm-agent造成问题
------------网络问题---------- ifconfig...没有看到eth0..然后重启网卡又报下面错误. 故障现象: service network restartShutting ...
- 关于存储的--b
iphone沙箱模型的有四个文件夹,分别是什么,永久数据存储一般放在什么位置,得到模拟器的路径的简单方式是什么. documents,tmp,app,Library. (NSHomeDirectory ...
- 使用date命令,进行时间戳和日期时间的互转
首先是知道时间转成时间戳 date -d "2014-01-16 12:30:11" +%s - :: - :: 其次是知道时间戳,想要知道当时的时间 date -d '1970- ...
- go程序性能优化
性能优化总结: 1 尽量避免频繁创建对象,即减少&{},new,make的使用2 数组可当切片用,当需要使用切片时,可考虑能使用数组来减少切片的创建3 当某类临时对象被多个协频繁程使用时,可用 ...