首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >Android动画之补间动画(XML 形式)

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

作者头像
计蒙不吃鱼
发布2025-06-12 14:40:17
发布2025-06-12 14:40:17
11900
代码可运行
举报
文章被收录于专栏:Android开发Android开发
运行总次数:0
代码可运行

理解:指定一个开始的位置,再指定一个结束的位置,自动补充中间的变化过程 为了更好的演示,写了一个Demo,xml界面如下(最后有源码)

首先得提一下现在越来越规范了,以前可以在alpha,translate,rotate,scale里面写一些其他属性,现在不行了,如

代码语言:javascript
代码运行次数:0
运行
复制
<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

代码语言:javascript
代码运行次数:0
运行
复制
<?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

代码语言:javascript
代码运行次数:0
运行
复制
<?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

代码语言:javascript
代码运行次数:0
运行
复制
<?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

代码语言:javascript
代码运行次数:0
运行
复制
<?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

代码语言:javascript
代码运行次数:0
运行
复制
<?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

代码语言:javascript
代码运行次数:0
运行
复制
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

代码语言:javascript
代码运行次数:0
运行
复制
<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>
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019-08-08,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档