Android Animation 为布局添加动画效果
16lz
2021-01-23
该文章的目的就是为布局文件添加简单的动画效果:
实现效果图:
源代码:
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;}}
源代码下载:
点击下载源码
更多相关文章
- android 开发 RecyclerView 横排列列表布局
- 增加滚动布局
- 帧动画
- [置顶] 我的Android进阶之旅------>Android之动画之Frame Animat
- android 自定义属性实现 ImageView 透明度渐变效果
- 实现Android立体翻页效果的工具类!
- Android简单实现启动画面的方法
- android中简单的图片翻页效果
- android按Menu出现菜单的动画