Android动画之补间动画(XML 形式)

理解:指定一个开始的位置,再指定一个结束的位置,自动补充中间的变化过程
为了更好的演示,写了一个Demo,xml界面如下(最后有源码)
在这里插入图片描述
首先得提一下现在越来越规范了,以前可以在alpha,translate,rotate,scale里面写一些其他属性,现在不行了,如

<scale xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="1000"
    android:fromXScale="1.0"
    android:fromYScale="1.0"
    android:pivotX="50%"
    android:pivotY="50%"
    android:toXScale="3.0"
    android:toYScale="3.0"/>

这是以前的,但是现在duration等属性在scale里写不出来!!!,得写在set里,用set包裹scale,(现在alpha,translate,rotate,scale里面只执行自己的参数)!!!

要介绍的有:
1.透明动画:alpha
2.位移动画:translate
3.旋转动画:rotate
4.缩放动画:scale
5.组合
先介绍下XML文件再统一在Java中实现
1.透明动画:alpha参数:制定透明度从多少到多少(范围0-1),0代表透明
在这里插入图片描述
2.位移动画:translate:四个参数:X轴开始位置,X轴结束位置,Y轴开始位置,Y轴结束位置
在这里插入图片描述
3.旋转动画:rotate:参数,开始时角度,结束时角度,旋转中心的横坐标,旋转中心的纵坐标
在这里插入图片描述
4.缩放动画:scale,参数:x开始的比例值,x结束的比例值,Y开始的比例值,Y结束的比例值,后面两个参数为缩放中心的坐标点
在这里插入图片描述
5.组合,还可以加。。。。
在这里插入图片描述
在Java文件中运用:AnimationUtils
在这里插入图片描述
源码如下:
anim_alpha.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="3000"
    android:fillAfter="true">
    <alpha
        android:fromAlpha="1"
        android:toAlpha="0.2">

    </alpha>
</set>

anim_translate.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="3000"
    android:fillAfter="true">
    <translate
        android:fromXDelta="0"
        android:fromYDelta="0"
        android:toXDelta="300"
        android:toYDelta="300">

    </translate>
</set>

anim_rotate.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="3000"
    android:fillAfter="true">
    <rotate
       android:fromDegrees="0"
        android:toDegrees="200"
        android:pivotX="50%"
        android:pivotY="50%">

    </rotate>
</set>

anim_scale.xml

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

    </scale>
</set>

anim_group.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="3000"
    android:fillAfter="true"
    >
    <alpha
        android:fromAlpha="1"
        android:toAlpha="0.5">
    </alpha>
    <scale
        android:fromXScale="1"
        android:fromYScale="1"
        android:toXScale="3"
        android:toYScale="3"
        android:pivotY="50%"
        android:pivotX="50%">

    </scale>

</set>

FourActivity.java

public class FourActivity extends AppCompatActivity implements View.OnClickListener {

    private Button btn_alpha;
    private Button btn_translate;
    private Button btn_rotate;
    private Button btn_scale;
    private ImageView iv_show;
    Animation animation;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_four);
        initView();
    }

    private void initView() {
        btn_alpha = (Button) findViewById(R.id.btn_alpha);
        btn_translate = (Button) findViewById(R.id.btn_translate);
        btn_rotate = (Button) findViewById(R.id.btn_rotate);
        btn_scale = (Button) findViewById(R.id.btn_scale);
        iv_show = (ImageView) findViewById(R.id.iv_show);

        btn_alpha.setOnClickListener(this);
        btn_translate.setOnClickListener(this);
        btn_rotate.setOnClickListener(this);
        btn_scale.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_alpha://透明动画
                //如果要用静态的动画(xml),就得用这个API
                animation = AnimationUtils.loadAnimation(this, R.anim.anim_alpha);
                break;
            case R.id.btn_translate://位移动画
                animation = AnimationUtils.loadAnimation(this, R.anim.anim_translate);
                break;
            case R.id.btn_rotate://旋转动画
                animation = AnimationUtils.loadAnimation(this, R.anim.anim_rotate);
                break;
            case R.id.btn_scale://缩放动画
                animation = AnimationUtils.loadAnimation(this, R.anim.anim_scale);
                break;
        }
        iv_show.startAnimation(animation);
    }

    public void groupshow(View view) {//组合
        animation = AnimationUtils.loadAnimation(this, R.anim.anim_group);
        iv_show.startAnimation(animation);
    }
}

activity_four.xml

<RelativeLayout 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"
    tools:context=".FourActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <Button
            android:id="@+id/btn_alpha"
            android:layout_width="0dp"
            android:layout_height="40dp"
            android:layout_weight="1"
            android:text="透明动画" />

        <Button
            android:id="@+id/btn_translate"
            android:layout_width="0dp"
            android:layout_height="40dp"
            android:layout_weight="1"
            android:text="位移动画" />

        <Button
            android:id="@+id/btn_rotate"
            android:layout_width="0dp"
            android:layout_height="40dp"
            android:layout_weight="1"
            android:text="旋转动画" />

        <Button
            android:id="@+id/btn_scale"
            android:layout_width="0dp"
            android:layout_height="40dp"
            android:layout_weight="1"
            android:text="缩放动画" />
    </LinearLayout>

    <ImageView
        android:id="@+id/iv_show"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:src="@mipmap/ic_launcher" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:onClick="groupshow"
        android:text="组合显示" />
</RelativeLayout>
发布了77 篇原创文章 · 获赞 411 · 访问量 27万+

猜你喜欢

转载自blog.csdn.net/qq_42761395/article/details/98892583