您的位置:首页 > 移动开发 > Android开发

【Android开发小记--4】动画--线性布局动画

2016-02-02 20:37 495 查看

线性布局的动画效果效果图





一、先进行布局设置,在 activity_main.xml 的 LinearLayout 中添加控件:

<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"
    android:id="@+id/linearLayout"
    tools:context="com.dingding.animationlayout.MainActivity">
    <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>


二、MainActivity.java中

第一种效果,button同时出现,缩放效果动画:

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //为布局添加动画效果
        LinearLayout rootView = (LinearLayout) findViewById(R.id.linearLayout);
        ScaleAnimation sa = new ScaleAnimation(0,1,0,1);
        sa.setDuration(5000);
        //-----1-----延迟时间为0,布局中的控件同时开始动画
        LayoutAnimationController lac = new LayoutAnimationController(sa,0);//( ,延迟的时间)
        
        rootView.setLayoutAnimation(lac);
    }
}


第二种效果,button依次出现:

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //为布局添加动画效果
        LinearLayout rootView = (LinearLayout) findViewById(R.id.linearLayout);
        ScaleAnimation sa = new ScaleAnimation(0,1,0,1);
        sa.setDuration(5000);
        //-----1-----延迟时间为0,布局中的控件同时开始动画
//        LayoutAnimationController lac = new LayoutAnimationController(sa,0);//( ,延迟的时间)
        //-----2-----延迟时间为前一个控件出现了一半,第二个开始出现
        LayoutAnimationController lac = new LayoutAnimationController(sa,0.5f);

        rootView.setLayoutAnimation(lac);
    }
}


第三种效果,可以设置button出现的顺序,有顺序、随机、逆序这几种:

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //为布局添加动画效果
        LinearLayout rootView = (LinearLayout) findViewById(R.id.linearLayout);
        ScaleAnimation sa = new ScaleAnimation(0,1,0,1);
        sa.setDuration(5000);
        //-----1-----延迟时间为0,布局中的控件同时开始动画
//        LayoutAnimationController lac = new LayoutAnimationController(sa,0);//( ,延迟的时间)
        //-----2-----延迟时间为前一个控件出现了一半,第二个开始出现
        LayoutAnimationController lac = new LayoutAnimationController(sa,0.5f);
        //设置控件出现的顺序
        //ORDER_NORMAL--顺序,ORDER_RANDOM--随机,ORDER_REVERSE--逆序
        lac.setOrder(LayoutAnimationController.ORDER_REVERSE);
        rootView.setLayoutAnimation(lac);
    }
}


具体代码点击

在线性布局中布局改变动画的效果图:



一、在menu中设置添加 "add" 按钮

menu_main.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context="com.dingding.animationlayoutchange.MainActivity">
    <item
        android:id="@+id/action_settings"
        android:orderInCategory="100"
        android:title="@string/action_settings"
        app:showAsAction="never" />
    <item
        android:id="@+id/action_add"
        app:showAsAction="always"
        android:icon="@android:drawable/ic_input_add"
        android:title="add" />
</menu>


二、MainActivity.java

public class MainActivity extends AppCompatActivity {
    private LinearLayout linearLayout;
    //button监听,点击则移除该button
    private View.OnClickListener btnOnClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            linearLayout.removeView(v);
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        linearLayout = (LinearLayout) findViewById(R.id.linearLayout);
    }
    /*添加button*/
    private void addButton(){
        Button btn = new Button(this);
        btn.setText("Remove me");
        linearLayout.addView(btn);
        btn.setOnClickListener(btnOnClickListener);//对按钮监听
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();

        switch (id){
            //设置按钮
            case R.id.action_settings:
                return true;
            //添加button按钮
            case R.id.action_add:
                addButton();
                break;
            default:
                break;
        }
        return super.onOptionsItemSelected(item);
    }
}


三、为了动画效果更柔和流畅,在对应的 LinearLayout 中添加:

android:animateLayoutChanges="true"


具体代码点击






ListView的动画效果图:



MainActivity.java继承自ListActivity,设置适配器和缩放效果:

public class MainActivity extends ListActivity {
    private ArrayAdapter<String> adapter;
    private LayoutAnimationController lac;
    private ScaleAnimation sa;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
          //----1---代码编辑------------------------
        adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, new String[]{"Good", "morning", "everybody", "!!!"});
        setListAdapter(adapter);

        sa = new ScaleAnimation(0,1,0,1);
        sa.setDuration(5000);
        lac = new LayoutAnimationController(sa,0.5f);

        getListView().setLayoutAnimation(lac);
    }
}


用XML文件配置

配置anim缩放文件 anim_scale.xml

<?xml version="1.0" encoding="utf-8"?>
<scale xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromXScale="0"
    android:toXScale="1"
    android:fromYScale="0"
    android:toYScale="1"
    android:duration="5000">
</scale>


再配置listview的动画文件 anim_listview.xml

<?xml version="1.0" encoding="utf-8"?>
<layoutAnimation xmlns:android="http://schemas.android.com/apk/res/android"
    android:animation="@anim/anim_scale"
    android:delay="0.5">
</layoutAnimation>


接着,配置listview的布局文件 listview.xml:

注意:这里listview的id要设置为 "@android:id/" 型的,

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
    android:id="@android:id/list"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layoutAnimation="@anim/anim_listview">
</ListView>
</LinearLayout>


最后,在 MainActivity.xml 文件中设置:

public class MainActivity extends ListActivity {
    private ArrayAdapter<String> adapter;
    private LayoutAnimationController lac;
    private ScaleAnimation sa;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //----2----XML编辑---------------------------
        setContentView(R.layout.listview);
        adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, new String[]{"Good", "morning", "everybody", "!!!"});
        setListAdapter(adapter);
    }
}


具体代码点击

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: