【Android】Animation动画(一)

Crossfading Two Views

两个界面切换,一个fade in, 一个fade out.下面是一个Demo:

 

  1 /*
  2  * Copyright 2012 The Android Open Source Project
  3  *
  4  * Licensed under the Apache License, Version 2.0 (the "License");
  5  * you may not use this file except in compliance with the License.
  6  * You may obtain a copy of the License at
  7  *
  8  *     http://www.apache.org/licenses/LICENSE-2.0
  9  *
 10  * Unless required by applicable law or agreed to in writing, software
 11  * distributed under the License is distributed on an "AS IS" BASIS,
 12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  * See the License for the specific language governing permissions and
 14  * limitations under the License.
 15  */
 16 
 17 package com.example.android.animationsdemo;
 18 
 19 import android.animation.Animator;
 20 import android.animation.AnimatorListenerAdapter;
 21 import android.app.Activity;
 22 import android.content.Intent;
 23 import android.os.Bundle;
 24 import android.support.v4.app.NavUtils;
 25 import android.view.Menu;
 26 import android.view.MenuItem;
 27 import android.view.View;
 28 
 29 /**
 30  * This sample demonstrates cross-fading between two overlapping views.
 31  *
 32  * <p>In this sample, the two overlapping views are a loading indicator and some text content. The
 33  * active view is toggled by touching the toggle button in the action bar. In real-world
 34  * applications, this toggle would occur as soon as content was available. Note that if content is
 35  * immediately available, a loading spinner shouldn't be presented and there should be no
 36  * animation.</p>
 37  */
 38 public class CrossfadeActivity extends Activity {
 39     /**
 40      * The flag indicating whether content is loaded (text is shown) or not (loading spinner is
 41      * shown).
 42      */
 43     private boolean mContentLoaded;
 44 
 45     /**
 46      * The view (or view group) containing the content. This is one of two overlapping views.
 47      */
 48     private View mContentView;
 49 
 50     /**
 51      * The view containing the loading indicator. This is the other of two overlapping views.
 52      */
 53     private View mLoadingView;
 54 
 55     /**
 56      * The system "short" animation time duration, in milliseconds. This duration is ideal for
 57      * subtle animations or animations that occur very frequently.
 58      */
 59     private int mShortAnimationDuration;
 60 
 61     @Override
 62     protected void onCreate(Bundle savedInstanceState) {
 63         super.onCreate(savedInstanceState);
 64         setContentView(R.layout.activity_crossfade);
 65 
 66         mContentView = findViewById(R.id.content);
 67         mLoadingView = findViewById(R.id.loading_spinner);
 68 
 69         // Initially hide the content view.
 70         mContentView.setVisibility(View.GONE);
 71 
 72         // Retrieve and cache the system's default "short" animation time.
 73         mShortAnimationDuration = getResources().getInteger(android.R.integer.config_shortAnimTime);
 74     }
 75 
 76     @Override
 77     public boolean onCreateOptionsMenu(Menu menu) {
 78         super.onCreateOptionsMenu(menu);
 79         getMenuInflater().inflate(R.menu.activity_crossfade, menu);
 80         return true;
 81     }
 82 
 83     @Override
 84     public boolean onOptionsItemSelected(MenuItem item) {
 85         switch (item.getItemId()) {
 86             case android.R.id.home:
 87                 // Navigate "up" the demo structure to the launchpad activity.
 88                 // See http://developer.android.com/design/patterns/navigation.html for more.
 89                 NavUtils.navigateUpTo(this, new Intent(this, MainActivity.class));
 90                 return true;
 91 
 92             case R.id.action_toggle:
 93                 // Toggle whether content is loaded.
 94                 mContentLoaded = !mContentLoaded;
 95                 showContentOrLoadingIndicator(mContentLoaded);
 96                 return true;
 97         }
 98 
 99         return super.onOptionsItemSelected(item);
100     }
101 
102     /**
103      * Cross-fades between {@link #mContentView} and {@link #mLoadingView}.
104      */
105     private void showContentOrLoadingIndicator(boolean contentLoaded) {
106         // Decide which view to hide and which to show.
107         final View showView = contentLoaded ? mContentView : mLoadingView;
108         final View hideView = contentLoaded ? mLoadingView : mContentView;
109 
110         // Set the "show" view to 0% opacity but visible, so that it is visible
111         // (but fully transparent) during the animation.
112         showView.setAlpha(0f);
113         showView.setVisibility(View.VISIBLE);
114 
115         // Animate the "show" view to 100% opacity, and clear any animation listener set on
116         // the view. Remember that listeners are not limited to the specific animation
117         // describes in the chained method calls. Listeners are set on the
118         // ViewPropertyAnimator object for the view, which persists across several
119         // animations.
120         showView.animate()
121                 .alpha(1f)
122                 .setDuration(mShortAnimationDuration)
123                 .setListener(null);
124 
125         // Animate the "hide" view to 0% opacity. After the animation ends, set its visibility
126         // to GONE as an optimization step (it won't participate in layout passes, etc.)
127         hideView.animate()
128                 .alpha(0f)
129                 .setDuration(mShortAnimationDuration)
130                 .setListener(new AnimatorListenerAdapter() {
131                     @Override
132                     public void onAnimationEnd(Animator animation) {
133                         hideView.setVisibility(View.GONE);
134                     }
135                 });
136     }
137 }

 

技巧:

1)出现的View一开始alpha设置为0,之后加上动画,这样就可以出现慢慢fade in的效果;

2)小时的View设置一个监听器,动画完毕设置为不可见,这样即可实现慢慢fade out的效果;

根据DOC讲述一下方法:

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 GONE. This prevents the view from taking up layout space and omits it from layout calculations, speeding up processing.
  3. Cache the config_shortAnimTime 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. config_longAnimTime and config_mediumAnimTime are also available if you wish to use them.(蓝色部分是常量)

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 VISIBLE. (Remember that it was initially set to GONE.) 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 onAnimationEnd() in an Animator.AnimatorListener, set the visibility of the view that was fading out to GONE. Even though the alpha value is 0, setting the view's visibility to GONE prevents the view from taking up layout space and omits it from layout calculations, speeding up processing.

 

 

posted @ 2012-12-08 11:08  大脚印  阅读(592)  评论(0编辑  收藏  举报