Android自定义控件入门到精通--帧动画

《Android自定义控件入门到精通》文章索引 ☞ https://blog.csdn.net/Jhone_csdn/article/details/118146683

《Android自定义控件入门到精通》所有源码 ☞ https://gitee.com/zengjiangwen/Code


动画分为三种

  • 帧动画(FrameAnimation/DrawableAnimation)
  • 补间动画(TweenAnimation/ViewAnimation)
  • 属性动画(PropertyAnimation)

帧动画

类似我们常见的gif动画(由一帧一帧的图片循环播放达到动画效果)

帧动画优点是使用简单又能实现比较复杂的动画效果

在这里插入图片描述

使用方法一:xml

步骤一:准备图片素材

在这里插入图片描述

步骤二:在drawable目录下写动画xml文件(anima_discove.xml)

<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:oneshot="false" >

    <item
        android:drawable="@drawable/radar01"
        android:duration="250">
    </item>
    <item
        android:drawable="@drawable/radar02"
        android:duration="250">
    </item>
    <item
        android:drawable="@drawable/radar03"
        android:duration="250">
    </item>
    <item
        android:drawable="@drawable/radar04"
        android:duration="250">
    </item>
    <item
        android:drawable="@drawable/radar05"
        android:duration="250">
    </item>
    <item
        android:drawable="@drawable/radar06"
        android:duration="250">
    </item>
    <item
        android:drawable="@drawable/radar07"
        android:duration="250">
    </item>
    <item
        android:drawable="@drawable/radar08"
        android:duration="250">
    </item>
    <item
        android:drawable="@drawable/radar09"
        android:duration="250">
    </item>
    <item
        android:drawable="@drawable/radar10"
        android:duration="250">
    </item>
    <item
        android:drawable="@drawable/radar11"
        android:duration="250">
    </item>
    <item
        android:drawable="@drawable/radar12"
        android:duration="250">
    </item>

</animation-list>

步骤三:引用动画文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="wrap_content"
    android:orientation="vertical"
    android:background="#333333"
    tools:context=".activity.MainActivity">
    <ImageView
        android:id="@+id/imageView"
        android:layout_width="160dp"
        android:layout_height="160dp"
        android:layout_gravity="center_horizontal"
        android:src="@drawable/anima_discove"
        />
    <Button
        android:id="@+id/playBtn"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:layout_margin="20dp"
        android:textSize="14sp"
        android:text="播放"
        android:textColor="#222222"
        android:background="#e7e7e7"
        android:gravity="center"
        />
    <Button
        android:id="@+id/stopBtn"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:layout_margin="20dp"
        android:textSize="14sp"
        android:textColor="#222222"
        android:text="停止"
        android:background="#e7e7e7"
        android:gravity="center"
        />
</LinearLayout>

步骤四:代码实现控制播放和停止

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ImageView imageView=findViewById(R.id.imageView);
        Button playBtn=findViewById(R.id.playBtn);
        Button stopBtn=findViewById(R.id.stopBtn);

        final AnimationDrawable animationDrawable= (AnimationDrawable) imageView.getDrawable();
        animationDrawable.setOneShot(false);//设置是否只执行一遍(循环)
        playBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                animationDrawable.start();
            }
        });
        stopBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                animationDrawable.stop();
            }
        });
    }
}

除了ImageView的src引用,还可以设置给任意View的background,background动画的获取方式为:

 AnimationDrawable animationDrawable= (AnimationDrawable) imageView.getBackground();

使用方法二:java

final AnimationDrawable animationDrawable= new AnimationDrawable();
animationDrawable.addFrame(getDrawable(R.drawable.radar01),250);
animationDrawable.addFrame(getDrawable(R.drawable.radar02),250);
animationDrawable.addFrame(getDrawable(R.drawable.radar03),250);
animationDrawable.addFrame(getDrawable(R.drawable.radar04),250);
animationDrawable.addFrame(getDrawable(R.drawable.radar05),250);
animationDrawable.addFrame(getDrawable(R.drawable.radar06),250);
animationDrawable.addFrame(getDrawable(R.drawable.radar07),250);
animationDrawable.addFrame(getDrawable(R.drawable.radar08),250);
animationDrawable.setOneShot(false);//设置是否只执行一遍(循环)
//如果不是ImageView,也可以使用setBackGround()引用动画
imageView.setImageDrawable(animationDrawable);

直接使用java代码的方式创建AnimationDrawable对象

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一鱼浅游

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值