zoukankan      html  css  js  c++  java
  • 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> 
    设置动画  

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

    为了设置动画 

    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); 
                   } 
               }); 
    }
  • 相关阅读:
    NumPy数组基本的索引和切片
    赫夫曼树编码解码实例(C)
    深度优先迷宫求解实例(C)
    创建ndarray的方法
    【学习笔记】计算机网络-利用TELNET进行SMTP的邮件发送
    【学习笔记】非递归实现先后根遍历二叉树
    【学习笔记】计算机网络-DNS层次查询
    【学习笔记】计算机网络-网络常用命令(一)
    【学习笔记】计算机网络-Ping命令(一)
    Win10下Wireshark找不到接口的解决办法
  • 原文地址:https://www.cnblogs.com/fuhaots2009/p/3471657.html
Copyright © 2011-2022 走看看