Android 实战开发 帧动画


Android 动画分类

1.帧动画

2.补间动画

3.属性动画


一.帧动画

帧动画由动画图形AnimationDrawable生成。 主要常用常有方法

  addFrame:添加一幅图片帧,并指定该帧的持续时间(单位毫秒)

  setOneShot:设置是否只播放一次。为true 表示只播放一次。为false表示循环播放。

  start:开始播放。注意,设置宿主视图后才能进行播放

  stop:停止播放

  isRunning:判断是否正在播放

有了动画图形,还得又一个宿主视图显示该图形,一般使用图像视图ImageView承载AnimationDrawable。即调用ImageView对象的setImageDrawable方法将动画图形加载到图像视图中。

 实现:

  1.添加 图片

  

 


创建及配置frame_anim.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/hb_1" android:duration="50"/>
    <item android:drawable="@drawable/hb_2" android:duration="50"/>
    <item android:drawable="@drawable/hb_3" android:duration="50"/>
    <item android:drawable="@drawable/hb_4" android:duration="50"/>
</animation-list>

2.在MainActivity.java中调用动画
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ImageView frameAnim = findViewById(R.id.frameAnim);
    frameAnim.setImageResource(R.drawable.frame_anim);
    AnimationDrawable  ad_frame = (AnimationDrawable)frameAnim.getDrawable();
    ad_frame.start();
}

 
实现效果

动画已经跑起来了!

显示GIF动画
1.添加loading.gif

2.编写代码
private void showGifAnimation(){
    ImageView iv_gif = findViewById(R.id.frameAnim);
    InputStream is = getResources().openRawResource(R.raw.loading);
    GifImage gifImage = new GifImage();
    int code = gifImage.read(is);
    if (code == GifImage.STATUS_OK) {
        GifImage.GifFrame[] frameList = gifImage.getFrames();
        AnimationDrawable ad_gif = new AnimationDrawable();
        for (int i=0; i<frameList.length; i++) {
            //BitmapDrawable用于把Bitmap格式转换为Drawable格式
            BitmapDrawable bitmapDrawable = new BitmapDrawable(getResources(), frameList[i].image);
            ad_gif.addFrame(bitmapDrawable, frameList[i].delay);
        }
        ad_gif.setOneShot(false);
        iv_gif.setImageDrawable(ad_gif);
        ad_gif.start();
    } else if (code == GifImage.STATUS_FORMAT_ERROR) {
        Toast.makeText(this, "该图片不是gif格式", Toast.LENGTH_LONG).show();
    } else {
        Toast.makeText(this, "gif图片读取失败:" + code, Toast.LENGTH_LONG).show();
    }
运行效果


 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值