布局动画

布局动画,顾名思义,通常用来布局上的显示view,为view groups的显示添加动画。

通常我们使用LayoutAnimationController的对象来为view添加一个动画,具体的操作是:先创建一个LayoutAnimationController的对象,然后用相应的view来加载该对象。

接下来我们来看看代码(我在这里展示的是给listview添加动画)

Android Layout 布局动画的介绍http://blog.csdn.net/wangjia55/article/details/42743305

anim文件下的代码

xml代码(这个最主要是用来给listview的每一个view添加动画)

<?xml version="1.0" encoding="utf-8"?>
  <set xmlns:android="http://schemas.android.com/apk/res/android">
      <scale 
          android:duration="300"
          android:fromXScale="0.0"
          android:fromYScale="0.0"
          android:toYScale="1.0"
          android:toXScale="1.0"
          android:pivotX="50%"
         android:pivotY="50%"
         />
  </set>

布局文件代码

1.Mainactivity的xml代码

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:tools="http://schemas.android.com/tools"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:orientation="vertical"
     tools:context="com.example.Tween_Animation.Alpha_MainActivity" >
 
     <Button
         android:id="@+id/button_scale"
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:text="@string/button_stringScaleAnimation" /> 
     <LinearLayout
         android:gravity="center"
         android:layout_width="fill_parent"
         android:layout_height="fill_parent"
         android:orientation="vertical" >
          <ImageView
             android:id="@+id/imageview_scale"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:src="@drawable/ic_launcher" />
     </LinearLayout>
  </LinearLayout>
2.MainActivity2的xml代码

 <?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" >
     <ListView 
         android:id="@+id/listview"
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         /> 
</LinearLayout>

Activity代码

1.MainActivity代码

package com.example.Layout_Animation;
import com.example.androidanimation.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button; 
public class MainActivity extends Activity implements OnClickListener{
     private Button button = null;
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);
         button = (Button) findViewById(R.id.button_scale);
         button.setOnClickListener(this);
     }
     public void onClick(View v) {
         Intent intent = new Intent(MainActivity.this,MainActivity2.class);
         startActivity(intent);
     }
 }
2.MainActivity2的代码

package com.example.Layout_Animation;
import com.example.androidanimation.R;
import android.app.Activity;
import android.os.Bundle;
import android.view.animation.AnimationUtils;
import android.view.animation.LayoutAnimationController;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class MainActivity2 extends Activity{
     private ListView listview = null;
     private ArrayAdapter<String> arrayadapter = null;
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.main2);
         listview = (ListView) findViewById(R.id.listview);
         String string[] = new String[20];
         arrayadapter = new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1, get_array(string));
         listview.setAdapter(arrayadapter);
         //创建一个LayoutAnimationController的对象
         LayoutAnimationController lac = new LayoutAnimationController(AnimationUtils.loadAnimation(this, R.anim.scale));
         //设置每个子控件动画播放的顺序(中间计算了每个子控件的动画显示时间的)
         /*
          * 从安卓api中可以看出,有3个参数供选择
          * ORDER_NORMAL--顺序显示
          * ORDER_RANDOM--随机显示
          * ORDER_REVERSE--倒叙显示
          */
         lac.setOrder(LayoutAnimationController.ORDER_RANDOM);
         listview.setLayoutAnimation(lac);
     }
     private String [] get_array(String string[]){
         for(int i = 0; i < 20; i++){
           string[i] = "小米" + (i + 1);
         }
         return string;
     }
 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值