博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Crossfading Two Views 淡入淡出的两种观点
阅读量:4046 次
发布时间:2019-05-24

本文共 4165 字,大约阅读时间需要 13 分钟。

Crossfade animations (also know as dissolve) gradually fade out one UI component while simultaneously fading in another. This animation is useful for situations where you want to switch content or views in your app. Crossfades are very subtle and short but offer a fluid transition from one screen to the next. When you don't use them, however, transitions often feel abrupt or hurried.

Here's an example of a crossfade from a progress indicator to some text content. http://blog.csdn.net/sergeycao

 

If you want to jump ahead and see a full working example, and run the sample app and select the Crossfade example. See the following files for the code implementation:

  • src/CrossfadeActivity.java
  • layout/activity_crossfade.xml
  • menu/activity_crossfade.xml

Create the Views

Create the two views that you want to crossfade. The following example creates a progress indicator and a scrollable text view:

Set up the Animation

To set up the animation:

  1. Create member variables for the views that you want to crossfade. You need these references later when modifying the views during the animation.
  2. For the view that is being faded in, set its visibility to . This prevents the view from taking up layout space and omits it from layout calculations, speeding up processing.
  3. Cache the system property in a member variable. This property defines a standard "short" duration for the animation. This duration is ideal for subtle animations or animations that occur very frequently. and are also available if you wish to use them.

Here's an example using the layout from the previous code snippet as the activity content view:

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);    }

Crossfade the Views

Now that the views are properly set up, crossfade them by doing the following:

  1. For the view that is fading in, set the alpha value to 0 and the visibility to . (Remember that it was initially set to .) This makes the view visible but completely transparent.
  2. For the view that is fading in, animate its alpha value from 0 to 1. At the same time, for the view that is fading out, animate the alpha value from 1 to 0.
  3. Using in an , set the visibility of the view that was fading out to . Even though the alpha value is 0, setting the view's visibility to prevents the view from taking up layout space and omits it from layout calculations, speeding up processing.

The following method shows an example of how to do this:

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);                }            });}
你可能感兴趣的文章
枚举用法详解
查看>>
设计模式常见面试题
查看>>
curl
查看>>
hibernateTemplate常用方法
查看>>
windows下查看端口监听情况
查看>>
windows下mongodb安装
查看>>
win7下安装mongodb
查看>>
mongodb使用笔记
查看>>
ZeroMQ研究与应用分析
查看>>
mongodb--nodejs
查看>>
linux vi 操作笔记
查看>>
使用TortoiseGit对Git版本进行分支操作
查看>>
Linux tcpdump命令详解
查看>>
mysql日期格式化
查看>>
MySQL数据库中的Date,DateTime,TimeStamp和Time类型
查看>>
MySQL索引
查看>>
mongodb 的重启
查看>>
Mongoose - 让NodeJS更容易操作Mongodb数据库
查看>>
module.exports与exports的区别
查看>>
Nodejs开发框架Express3.0开发手记
查看>>