两个Android开源项目:Android显示GIF动画

GifFrame.java

里面三个成员:当前图片、延时、下张Frame的链接。

package com.ant.liao;

import android.graphics.Bitmap;

public class GifFrame {

/**

  • 构造函数

  • @param im 图片

  • @param del 延时

*/

public GifFrame(Bitmap im, int del) {

image = im;

delay = del;

}

public GifFrame(String name,int del){

imageName = name;

delay = del;

}

/*图片/

public Bitmap image;

/*延时/

public int delay;

/*当图片存成文件时的文件名/

public String imageName = null;

/*下一帧/

public GifFrame nextFrame = null;

}

GifDecoder.java

解码线程类

http://code.google.com/p/gifview/source/browse/trunk/src/com/ant/liao/GifDecoder.java

GifView.java

主类,包括常用方法,如GifView构造方法、设置图片源、延迟、绘制等。

http://code.google.com/p/gifview/source/browse/trunk/src/com/ant/liao/GifView.java

android-gif-drawable

android-gif-drawable

Views and Drawable for animated GIFs in Android.

项目地址:https://github.com/koral–/android-gif-drawable

Overview

Bundled GIFLib via JNI is used to render frames. This way should be more efficient thanWebView or Movie classes.

Animation starts automatically and run only if View with attached GifDrawable is visible.

Download

Latest release downloads

Setup

Gradle (Android Studio)

Insert the following dependency to build.gradle file of your project.

view source print ?

1. dependencies {

2. compile 'pl.droidsonroids.gif:android-gif-drawable:1.0.+'

3. }

Note that Maven central repository should be defined eg. in top-level build.gradle like this:

view source print ?

01. buildscript {

02. repositories {

03. mavenCentral()

04. }

05. }

06. allprojects {

07. repositories {

08. mavenCentral()

09. }

10. }

Maven dependency

SDK with API level 19 is needed. If you don’t have it in your local repository, downloadmaven-android-sdk-deployer and install SDK level 19:mvn install -P 4.4 (from maven-android-sdk-deployer directory). Then add dependency inpom.xml of your project:

view source print ?

1. <dependency>

2. <groupId>pl.droidsonroids.gif</groupId>

3. <artifactId>android-gif-drawable</artifactId>

4. <version>insert latest version here</version>

5. <type>aar</type>

6. </dependency>

Requirements

Android 1.6+ (API level 4+)

Building from source

Android NDK needed to compile native sources

Usage

From XML

The simplest way is to use GifImageView (or GifImageButton) like a normalImageView:

view source print ?

1. <pl.droidsonroids.gif.GifImageView

2. android:layout_width=``"match_parent"

3. android:layout_height=``"match_parent"

4. android:src=``"@drawable/src_anim"

5. android:background=``"@drawable/bg_anim"

6. />

If drawables declared by android:src and/or android:background are GIF files then they will be automatically recognized asGifDrawables and animated. If given drawable is not a GIF then mentioned Views work like plainImageView and ImageButton.

GifTextView allows you to use GIFs as compound drawables and background.

view source print ?

1. <pl.droidsonroids.gif.GifTextView

2. android:layout_width=``"match_parent"

3. android:layout_height=``"match_parent"

4. android:drawableTop=``"@drawable/left_anim"

5. android:drawableStart=``"@drawable/left_anim"

6. android:background=``"@drawable/bg_anim"

7. />

From Java code

GifImageView, GifImageButton and GifTextView have also hooks for setters implemented. So animated GIFs can be set by callingsetImageResource(int resId) and setBackgroundResource(int resId)

GifDrawable can be constructed directly from various sources:

view source print ?

01. //asset file

02. GifDrawable gifFromAssets = new GifDrawable( getAssets(), "anim.gif" );

03.

04. //resource (drawable or raw)

05. GifDrawable gifFromResource = new GifDrawable( getResources(), R.drawable.anim );

06.

07. //byte array

08. byte``[] rawGifBytes = ...

09. GifDrawable gifFromBytes = new GifDrawable( rawGifBytes );

10.

11. //FileDescriptor

12. FileDescriptor fd = new RandomAccessFile( "/path/anim.gif"``, "r" ).getFD();

13. GifDrawable gifFromFd = new GifDrawable( fd );

14.

15. //file path

16. GifDrawable gifFromPath = new GifDrawable( "/path/anim.gif" );

17.

18. //file

19. File gifFile = new File(getFilesDir(),``"anim.gif"``);

20. GifDrawable gifFromFile = new GifDrawable(gifFile);

21.

22. //AssetFileDescriptor

23. AssetFileDescriptor afd = getAssets().openFd( "anim.gif" );

24. GifDrawable gifFromAfd = new GifDrawable( afd );

25.

26. //InputStream (it must support marking)

27. InputStream sourceIs = ...

28. BufferedInputStream bis = new BufferedInputStream( sourceIs, GIF_LENGTH );

29. GifDrawable gifFromStream = new GifDrawable( bis );

30.

31. //direct ByteBuffer

32. ByteBuffer rawGifBytes = ...

33. GifDrawable gifFromBytes = new GifDrawable( rawGifBytes );

InputStreams are closed automatically in finalizer if GifDrawable is no longer needed so you don’t need to explicitly close them. Callingrecycle() will also close underlaying input source.

Note that all input sources need to have ability to rewind to the begining. It is required to correctly play animated GIFs (where animation is repeatable) since subsequent frames are decoded on demand from source.

Animation control

GifDrawable implements an Animatable and MediaPlayerControl so you can use its methods and more:

stop() - stops the animation, can be called from any threadstart() - starts the animation, can be called from any threadisRunning() - returns whether animation is currently running or notreset() - rewinds the animation, does not restart stopped onesetSpeed(float factor) - sets new animation speed factor, eg. passing 2.0f will double the animation speedseekTo(int position) - seeks animation (within current loop) to givenposition (in milliseconds) Only seeking forward is supportedgetDuration() - returns duration of one loop of the animationgetCurrentPosition() - returns elapsed time from the beginning of a current loop of animation

UsingMediaPlayerControl

Standard controls for a MediaPlayer (like in VideoView) can be used to control GIF animation and show its current progress.

Just set GifDrawable as MediaPlayer on your MediaController like this:

view source print ?

01. @Override

最后

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数初中级Android工程师,想要提升技能,往往是自己摸索成长,自己不成体系的自学效果低效漫长且无助

因此我收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点!不论你是刚入门Android开发的新手,还是希望在技术上不断提升的资深开发者,这些资料都将为你打开新的学习之门

如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!
助**。

因此我收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。

[外链图片转存中…(img-YTzLm5L3-1715814125691)]

[外链图片转存中…(img-rMr0QcPO-1715814125693)]

[外链图片转存中…(img-JxsuhmFK-1715814125694)]

[外链图片转存中…(img-LUse59h6-1715814125695)]

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点!不论你是刚入门Android开发的新手,还是希望在技术上不断提升的资深开发者,这些资料都将为你打开新的学习之门

如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值