Android Animation 为布局添加动画效果

来源:互联网 发布:icloud怎么用恢复数据 编辑:程序博客网 时间:2024/05/20 20:57

该文章的目的就是为布局文件添加简单的动画效果:

实现效果图:

源代码:


fragment_main:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" >    <Button        android:id="@+id/Button05"        style="?android:attr/buttonStyleSmall"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="Button" />    <Button        android:id="@+id/Button04"        style="?android:attr/buttonStyleSmall"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="Button" />    <Button        android:id="@+id/Button03"        style="?android:attr/buttonStyleSmall"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="Button" />    <Button        android:id="@+id/Button02"        style="?android:attr/buttonStyleSmall"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="Button" />    <Button        android:id="@+id/Button01"        style="?android:attr/buttonStyleSmall"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="Button" />    <Button        android:id="@+id/button1"        style="?android:attr/buttonStyleSmall"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="Button" /></LinearLayout>

MainActivity:

package com.animationdemo;import android.app.Activity;import android.os.Bundle;public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);if (savedInstanceState == null) {/** * 在Activity中动态的添加一个Fragment */getFragmentManager().beginTransaction().add(android.R.id.content, new PlaceholderFragment()).commit();}}}

PlaceholderFragment:

package com.animationdemo;import android.app.Fragment;import android.os.Bundle;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.view.animation.LayoutAnimationController;import android.view.animation.ScaleAnimation;import android.widget.LinearLayout;public class PlaceholderFragment extends Fragment {@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {//获取该Fragment的布局文件LinearLayout rootView = (LinearLayout) inflater.inflate(R.layout.fragment_main, container, false);//设置动画的形式为缩放动画ScaleAnimation sa = new ScaleAnimation(0, 1, 0, 1);sa.setDuration(3000);//使用动画控制器,控制每个动画的显示时间(0.2f表示第一个按钮动画出现其整个的20%后,第二个按钮开始出现)。LayoutAnimationController lac = new LayoutAnimationController(sa, 0.2f);rootView.setLayoutAnimation(lac);return rootView;}}

源代码下载:

点击下载源码



0 0
原创粉丝点击