Android 动画基础-布局动画

  布局动画,顾名思义,通常用来布局上的显示view,为view groups的显示添加动画。
  通常我们使用LayoutAnimationController的对象来为view添加一个动画,具体的操作是:先创建一个LayoutAnimationController的对象,然后用相应的view来加载该对象。
  接下来我们来看看代码(我在这里展示的是给listview添加动画)

1. 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>

2. 布局文件代码

(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>

3. 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;
    }
}
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 176,711评论 25 709
  • ¥开启¥ 【iAPP实现进入界面执行逐一显】 〖2017-08-25 15:22:14〗 《//首先开一个线程,因...
    小菜c阅读 11,922评论 0 17
  • 1 背景 不能只分析源码呀,分析的同时也要整理归纳基础知识,刚好有人微博私信让全面说说Android的动画,所以今...
    未聞椛洺阅读 7,701评论 0 10
  • 新的一个星期,新的开始。今天我们班的班主任在我们班的群里早早的就发了照片,看到我们班的同学们,迎来了我...
    开心快乐的人阅读 1,342评论 0 0
  • “借审判之镜啊...” 主殿之前,当听到牧尘的请求后,太苍院长他们眉头都是皱了皱,半晌后,他方才看向牧尘,沉吟道:...
    混沌天书阅读 3,388评论 0 0

友情链接更多精彩内容