有一段时间没有更新博客了,最近过的也是浑浑噩噩的,不知道每天都在干什么,以后会正常更新博客。
gif在android中还是比较流行,所以最近研究了一下加载Gif动画,看了许多大神的博客和源码,这里就借花献佛,加载gif动画,我使用了三种方式,分别是:
1.自定义控件加载gif动画
2.使用Glide加载gif动画
3.使用android-gif-drawable加载gif动画
下面看一下演示的效果:
下面我就分别来说一下这三种方式:
1.自定义控件加载gif动画
①.首先自定义一个GifView,用于显示Gif图片,具体代码如下:
public class GifView extends View {
private Resources resources;
private Movie mMovie;
private long mMovieStart;
private float ratioWidth;
private float ratioHeight;
public GifView(Context context) {
this(context,null);
}
public GifView(Context context, @Nullable AttributeSet attrs) {
this(context, attrs,0);
}
public GifView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
setLayerType(View.LAYER_TYPE_SOFTWARE, null);
}
resources = context.getResources();
TypedArray ta = context.obtainStyledAttributes(attrs,R.styleable.GifView);
int resourceId = ta.getResourceId(R.styleable.GifView_src, -1);
setGifResource(resourceId);
ta.recycle();
}
public void setGifResource(int resourceId) {
if (resourceId==-1){
return;
}
InputStream is = resources.openRawResource(resourceId);
mMovie = Movie.decodeStream(is);
requestLayout();
}
public void setGifStream(InputStream is){
mMovie = Movie.decodeStream(is);
requestLayout();
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
if (mMovie!=null){
int w = mMovie.width();
int h = mMovie.height();
if (w<=0){
w=1;
}
if (h<=0){
h=1;
}
int pLeft = getPaddingLeft();
int pRight = getPaddingRight();
int pTop = getPaddingTop();
int pBottom = getPaddingBottom();
int widthSize;
int heightSize;
w+=pLeft+pRight;
h+=pTop+pBottom;
w=Math.max(w,getSuggestedMinimumWidth());
h=Math.max(h,getSuggestedMinimumHeight());
widthSize= resolveSizeAndState(w,widthMeasureSpec,0);
heightSize= resolveSizeAndState(h,heightMeasureSpec,0);
ratioWidth = (float) widthSize/w;
ratioHeight = (float) heightSize/h;
setMeasuredDimension(widthSize,heightSize);
}else{
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
long now = SystemClock.uptimeMillis();
if (mMovieStart ==0){ //第一次进入
mMovieStart =now;
}
if (mMovie!=null){
int dur = mMovie.duration();
if (dur==0){
dur=1000;
}
int relTime= (int) ((now-mMovieStart)%dur);
mMovie.setTime(relTime);
// mMovie.draw(canvas,0,0);
float scale=Math.min(ratioWidth,ratioHeight);
canvas.scale(scale,scale);
mMovie.draw(canvas,0,0);
invalidate();
}
}
}
②.在布局文件中添加自定义的控件,我这加了两个,第一个展示assets文件中的gif图片,一个展示drawable中的gif图片
<com.example.wen.adroid.view.GifView
android:id="@+id/gv_photo"
android:layout_width="match_parent"
android:layout_weight="1"
android:layout_gravity="center"
android:layout_height="0dp"
/>
<com.example.wen.adroid.view.GifView
android:id="@+id/gv_local_photo"
android:layout_width="match_parent"
android:layout_weight="1"
android:layout_gravity="center"
android:layout_height="0dp"
app:src="@drawable/gifphoto"/>
③.展示gif图片,具体代码如下:
//方法一 使用自定义gifview播放gif图片
mGvLocalPhoto = (GifView) findViewById(R.id.gv_local_photo);
mGvPhoto = (GifView) findViewById(R.id.gv_photo);
try {
InputStream is = getAssets().open("assetphoto.gif");
mGvPhoto.setGifStream(is);
} catch (IOException e) {
e.printStackTrace();
}
2.使用Glide加载gif图片
Glide 地址: https://github.com/bumptech/glide
①.首先配置build.gradle
compile 'com.github.bumptech.glide:glide:3.7.0'
②.配置好Glide之后,然后通过基本的用法去加载,加载代码如下:
// 方法二 使用Glide播放gif图片
mIvPhoto = (ImageView) findViewById(R.id.iv_photo);
Glide.with(this).load(R.drawable.gifphoto).asGif().diskCacheStrategy(DiskCacheStrategy.SOURCE).into(mIvPhoto);
注意:diskCacheStrategy是为其添加缓存策略,其中缓存策略可以为:Source及None,None及为不缓存,Source缓存原型。
3.使用android-gif-drawable加载gif图片
android-gif-drawable 地址:https://github.com/koral--/android-gif-drawable
①.首先配置build.gradle
compile 'pl.droidsonroids.gif:android-gif-drawable:1.2.7'
②.在xml中添加GifImageView控件
<pl.droidsonroids.gif.GifImageView
android:id="@+id/giv_photo"
android:layout_width="match_parent"
android:layout_weight="1"
android:layout_height="0dp"
android:src="@drawable/gifphoto"
/>
③.调用android-gif-drawable
//方法三 使用android-gif-drawable 库
GifImageView mGifIvPhoto = (GifImageView) findViewById(R.id.giv_photo);
try {
//加载asset文件中的gif图片
GifDrawable gif = new GifDrawable(getAssets(), "assetphoto.gif");
mGifIvPhoto.setImageDrawable(gif);
} catch (IOException e) {
e.printStackTrace();
}
总结
三种方式各有各的优缺点,根据自己去选择适合自己的方式去加载gif图片
下载方式
CSDN
https://download.csdn.net/download/wen_haha/10707972
Github