HackSix 为ViewGroup的子视图添加悦目的动画效果

1.默认情况下他,添加到viewGrop的子视图是直接显示出来的。有一个比较简单的方法可以为这个过程增加动画效果。

2.知识点:
    给子视图添加动画效果就用:LayoutAnimationController类,用LayoutAnimationController要强调一点的是不可以给每个子视图指定
不同的动画效果,但可以决定每个子视图显示动画效果的时间
3.例子:
    下面将结合托名都渐变动画(alpha animation)和位移动画(translate animation)演示给ListView的子视图添加动画效果。。
    使用LayoutAnimationContriller的方法有两种:一是直接在代码中使用或者在XML文字间中配置,
下面例子将在代码中使用:
 1 public class MainActivity extends Activity {
 2     private ListView mListView;
 3     @Override
 4     protected void onCreate(Bundle savedInstanceState) {
 5         super.onCreate(savedInstanceState);
 6         setContentView(R.layout.activity_main);
 7         mListView = (ListView) findViewById(R.id.listView);
 8         mListView.setAdapter(new ArrayAdapter<String>(this,
 9                 android.R.layout.simple_list_item_1, Countries.COUNTRIES));
10         AnimationSet set = new AnimationSet(true);
11         Animation alpha = new AlphaAnimation(0.0f, 1.0f);
12         alpha.setDuration(2000);
13         Animation translate = new TranslateAnimation(
14                 Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF,
15                 0.0f, Animation.RELATIVE_TO_SELF, -1.0f,
16                 Animation.RELATIVE_TO_SELF, 0.0f);
17         translate.setDuration(1000);
18         set.addAnimation(alpha);
19         set.addAnimation(translate);
20         LayoutAnimationController controller = new LayoutAnimationController(
21                 set);
22         mListView.setLayoutAnimation(controller);
23     }
24 }  
activity_main.xml
 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:orientation="vertical"
 6     tools:context=".MainActivity" >
 7 
 8     <ListView
 9         android:id="@+id/listView"
10         android:layout_width="match_parent"
11         android:layout_height="match_parent" >
12     </ListView>
13 
14 </LinearLayout>
 
  
原文地址:https://www.cnblogs.com/liangstudyhome/p/4057642.html