Android创建和配置布局动画

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/dpl12/article/details/82983941

demo1:为布局添加动画效果

运行效果:

View布局:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/constraint"
    tools:context=".MainActivity">


    <Button
        android:id="@+id/button"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        android:background="@android:color/holo_blue_bright"
        android:text="@string/button"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/button2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        android:background="@android:color/holo_blue_dark"
        android:text="@string/button"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/button" />

    <Button
        android:id="@+id/button3"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        android:background="@android:color/holo_blue_light"
        android:text="@string/button"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/button2" />

    <Button
        android:id="@+id/button4"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        android:background="@android:color/holo_green_dark"
        android:text="@string/button"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/button3" />

    <Button
        android:id="@+id/button5"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        android:background="@android:color/holo_green_light"
        android:text="@string/button"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/button4" />

    <Button
        android:id="@+id/button6"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        android:background="@android:color/holo_orange_dark"
        android:text="@string/button"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/button5" />

    <Button
        android:id="@+id/button7"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        android:background="@android:color/holo_orange_light"
        android:text="@string/button"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/button6" />

    <Button
        android:id="@+id/button8"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        android:background="@android:color/holo_purple"
        android:text="@string/button"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/button7" />

    <Button
        android:id="@+id/button9"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        android:background="@android:color/holo_red_dark"
        android:text="@string/button"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/button8" />
</android.support.constraint.ConstraintLayout>

Control层代码:

public class MainActivity extends AppCompatActivity {

    private ConstraintLayout constraint;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        constraint=findViewById(R.id.constraint);
        ScaleAnimation scale=new ScaleAnimation(0,1,0,1);//定义一个缩放动画
        scale.setDuration(5000);
        //LayoutAnimationController:可以控制一组控件按照规定显示,用于一个布局或者ViewGroup的动画控制器,
        // 它能够让每个子View在不同的时间点分别执行相同的动画
        LayoutAnimationController controller=new LayoutAnimationController(scale,0.5f);//item动画延时,控件出来一半时下一个出
        controller.setOrder(LayoutAnimationController.ORDER_NORMAL);//控件出来的顺序
        constraint.setLayoutAnimation(controller);//部署到布局中
    }
}

demo2:布局内容改变动画

运行效果:

View布局:

<?xml version="1.0" encoding="utf-8"?>
<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:id="@+id/linear"
    android:orientation="vertical"
    android:animateLayoutChanges="true"
    tools:context=".LayoutChangeAnimation">
    <!-- animateLayoutChanges:
         实现添加/删除其中控件时,带有默认动画效果
    -->
</LinearLayout>

Control层代码:

public class LayoutChangeAnimation extends AppCompatActivity {

    private LinearLayout linearLayout;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_layout_change_animation);
        linearLayout=findViewById(R.id.linear);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main,menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId()==R.id.action_add){
            Button add_button=new Button(this);
            add_button.setText("remove me");
            add_button.setTextSize(24);
            linearLayout.addView(add_button);//布局中添加button
            add_button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    linearLayout.removeView(v);//移除button
                }
            });  
        }
        return super.onOptionsItemSelected(item);
    }
}

demo3:为列表添加布局动画效果

运行效果:

View布局:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android" 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/list_view_anim"/>
</android.support.constraint.ConstraintLayout>

资源配置文件anim:

   item动画scale.xml:

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

  主布局动画list_view_anim.xml:

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

</layoutAnimation>

Control层代码:

public class ListViewLayoutAnimation extends ListActivity{
    //ListActivity类中集成了一个ListView控件。
    //通过继承ListActivity类可方便地使用ListView控件

    private ArrayAdapter<String> arrayAdapter;
    //private ScaleAnimation scaleAnimation;
    //private LayoutAnimationController controller;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        String[] strings=new String[]{"小明","小红","小兰","小绿","小明","小红","小兰","小绿","小明",
                "小红","小兰","小绿","小明","小红","小兰","小绿","小明","小红","小兰","小绿","小明",
                "小红","小兰","小绿","小明","小红","小兰","小绿","小明","小红","小兰","小绿"};
        //添加子布局和数据
        arrayAdapter=new ArrayAdapter<>(this,android.R.layout.simple_list_item_1,strings);
        setListAdapter(arrayAdapter);//部署适配器

        //第一种方法:Java代码
//        scaleAnimation=new ScaleAnimation(0,1,0,1);
//        scaleAnimation.setDuration(1000);
//        controller=new LayoutAnimationController(scaleAnimation,0.5f);
//
//        getListView().setLayoutAnimation(controller);//getListView():获取ListActivity自带的ListView对象

        //第二种方法:布局文件的配置
        setContentView(R.layout.activity_listview_layout_animation);
    }
}

猜你喜欢

转载自blog.csdn.net/dpl12/article/details/82983941
今日推荐