1. 布局添加动画效果
① 视图 五个按钮
<?xml version="1.0" encoding="utf-8"?> <LinearLayout android:orientation="vertical" xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:animateLayoutChanges="true" android:id="@+id/myLinearLayout" > <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="New Button" android:id="@+id/button" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="New Button" android:id="@+id/button2" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="New Button" android:id="@+id/button3" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="New Button" android:id="@+id/button4" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="New Button" android:id="@+id/button5" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="New Button" android:id="@+id/button6" /> </LinearLayout>
② java执行代码
public class MainActivity extends Activity{ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 找到我们设置的布局 LinearLayout rootView = (LinearLayout)this.findViewById(R.id.myLinearLayout); ScaleAnimation as = new ScaleAnimation(0, 1, 0, 1); // 缩放动画 as.setDuration(5000); //动画时间5s // 布局动画控制器 LayoutAnimationController lac = new LayoutAnimationController(as, 0.5f); lac.setOrder(LayoutAnimationController.ORDER_REVERSE); // 动画类型 从下往上 默认是从上往下 /** LayoutAnimationController.ORDER_NORMAL; //顺序显示 LayoutAnimationController.ORDER_REVERSE; //反显示 LayoutAnimationController.ORDER_RANDOM //随机显示 * */ rootView.setLayoutAnimation(lac); System.out.println("onCreate"); } }
二。 这里在加一个布局内容改变动画
这个比较简单,用的最多的就是 在布局xml中加入 android:animateLayoutChanges="true"
也可以通过 布局对象的 setLayoutTransition() 设置
三。 列表添加动画效果
1.xml视图文件
<ListView android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@android:id/list" xmlns:android="http://schemas.android.com/apk/res/android" />
2. java代码
public class MainActivity extends ListActivity{ //继承ListActivity private ArrayAdapter adapter; //数据适配器 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, new String[]{"hello", "word", "chengzhier"}); this.setListAdapter(adapter); // 视图加载数据 ScaleAnimation sa = new ScaleAnimation(0, 1, 0, 1); // 缩放动画 sa.setDuration(1000); LayoutAnimationController lac = new LayoutAnimationController(sa, 0.5f); // 创建布局动画控制器 this.getListView().setLayoutAnimation(lac); //设置动画 System.out.println("onCreate"); } }
三。 列表添加动画效果(用xml文件配置)
① /res/anim/rotate.xml 缩放动画配置
<?xml version="1.0" encoding="utf-8"?> <rotate xmlns:android="http://schemas.android.com/apk/res/android" android:fromDegrees="0" android:toDegrees="360" android:duration="1000" android:pivotX="50%" android:pivotY="50%" />
② /res/anim/listview_anim.xml 列表动画配置
<!-- android:animation指的是我们上面创建的缩放动画 --> <layoutAnimation xmlns:android="http://schemas.android.com/apk/res/android" android:animation="@anim/scale" android:delay="0.5" > </layoutAnimation>
③ 我们的视图中使用
<?xml version="1.0" encoding="utf-8"?> <LinearLayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" xmlns:android="http://schemas.android.com/apk/res/android" > <!-- layoutAnimation 指定配置我们的配置文件 --> <ListView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@android:id/list" android:layoutAnimation="@anim/listview_anim" ></ListView> </LinearLayout>
④ 直接到ListActivit中使用
public class MainActivity extends ListActivity{ //继承ListActivity private ArrayAdapter adapter; //数据适配器 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, new String[]{"hello", "word", "chengzhier"}); this.setListAdapter(adapter); // 视图加载数据 System.out.println("onCreate"); } }