GifView 的基本功能:
为了解决android中现在没有直接显示gif的view,只能通过mediaplay来显示这个问题的项目,其用法和 ImageView一样,支持gif图片。可监视GIF是否加载成功。
GifView的功能:
- 播放Gif图片
- Gif动画监听
创建Android工程GifViewDemo来介绍一下:
首先添加GifView库和ImageLoader库的gradle依赖:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:26.+'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
testCompile 'junit:junit:4.12'
compile 'com.github.Cutta:GifView:1.1'
compile 'com.zftlive.android.library:feature-android-imageloader:1.0.0'
}
然后在drawable中添加gif图:
接着进行布局activity_main设置:
<?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"
tools:context="com.example.dpl.gifviewdemo.MainActivity">
<com.cunoraz.gifview.library.GifView
android:id="@+id/gifView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
最后在MainActivity.java中调用:
package com.example.dpl.gifviewdemo;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import com.cunoraz.gifview.library.GifView;
/*
加载本地GIF图片,使用gifView
*/
public class MainActivity extends AppCompatActivity {
private GifView gifView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gifView= (GifView) findViewById(R.id.gifView);
//设置图片源
gifView.setGifResource(R.drawable.wanshengjie);//本地图片
//位置设置
gifView.setX(150);
gifView.setY(300);
}
}
运行结果: