Android-图片动画使用

本文介绍了如何在Android中通过创建XML文件在res/drawable目录下定义帧动画,然后在布局文件中设置ImageView背景,并在Java代码中控制动画的开始和结束。示例代码包括了动画列表的配置、布局文件的设定以及Java代码的实现,展示了如何启动和停止帧动画。
摘要由CSDN通过智能技术生成

1、在res/drawable中创建***.xml文件

示例代码:

<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:oneshot="false"><!--控制<item> 标签执行一遍还是n遍-->

    <item android:drawable="@drawable/action_one"
            android:duration="500"/>
    <item android:drawable="@drawable/action_two"
        android:duration="500"/>
    <item android:drawable="@drawable/action_three"
        android:duration="500"/>
    <item android:drawable="@drawable/action_four"
        android:duration="500"/>
    <item android:drawable="@drawable/action_five"
        android:duration="500"/>
    <item android:drawable="@drawable/action_six"
        android:duration="500"/>
</animation-list>

2、布局文件

示例代码:

<?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:orientation="vertical"
    tools:context=".MainActivity">

   <ImageView
       android:id="@+id/actionImg"
       android:layout_width="match_parent"
       android:layout_height="500dp"
       android:background="@drawable/action_animation"/>

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

        <Button
            android:id="@+id/startBtn"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="开始动画"/>

        <Button
            android:id="@+id/endBtn"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="结束动画"/>
    </LinearLayout>

</LinearLayout>

3、java代码

代码示例:

  private ImageView actionImg;
    private Button startBtn;
    private Button endBtn;


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

        // 初始化图片控件
        actionImg = findViewById(R.id.actionImg);

        // 初始化开始按钮
        startBtn = findViewById(R.id.startBtn);
        startBtn.setOnClickListener(startActionClick);
        // 初始化结束按钮
        endBtn = findViewById(R.id.endBtn);
        endBtn.setOnClickListener(endActionClick);

    }

    private AnimationDrawable animationDrawable = null;// 图片对象
    /**
     * 开始动画
     * */
    View.OnClickListener startActionClick = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (animationDrawable == null){
                // 获取背景图片
                animationDrawable = (AnimationDrawable) actionImg.getBackground();
                // 启动动画
                animationDrawable.start();
            }
        }
    };

    /**
     * 结束动画
     * */
    View.OnClickListener endActionClick = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (animationDrawable != null){
                // 停止动画
                animationDrawable.stop();
                animationDrawable = null;
            }
        }
    };

 

所需图片和文件目录:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值