AndroidStudio中使用开源框架android-gif-drawable

来源:互联网 发布:股票实时行情数据接口 编辑:程序博客网 时间:2024/05/21 09:00
 一般情况下,我们在Android项目中只能使用.png,.jpg等格式的静态图片,诸如.gif格式的动态图片是加载不了的。

有了android-gif-drawable这个开源框架,在项目中使用gif动态图片不再是问题。 android-gif-drawable的
开源地址是:https://github.com/koral–/android-gif-drawable。其主要作用就是可以展示gif格式的图片及动画,还可以对动画进行监听操作。开源库中封闭了GifImageView,GifImageButton,GifTextView等控件。和我们普通的ImageView,ImageButton,TextView使用方法基本一样,只要在布局文件中添加这些控件,然后就可以直接设置gif图片作为背景等。
首先在AndroidStudio中配置相关依赖库代码,在配置文件build.gradle文件中添加:

   dependencies {    compile fileTree(dir: 'libs', include: ['*.jar'])    compile 'pl.droidsonroids.gif:android-gif-drawable:1.1.+'} 

布局文件,以GifImageView和GifTextView为例:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical">    <pl.droidsonroids.gif.GifImageView        android:id="@+id/test_gif"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_margin="15dp" />    <pl.droidsonroids.gif.GifTextView        android:layout_margin="15dp"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:background="@drawable/test"        android:text="这个TextView的背景好酷"        android:textColor="#FFFFFF" /></LinearLayout>

代码:

public class MainActivity extends AppCompatActivity {    private GifImageView test_gif;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        test_gif = (GifImageView) findViewById(R.id.test_gif);        //设置图片数据        test_gif.setImageResource(R.drawable.test);        final android.widget.MediaController mediaController = new android.widget.MediaController(this);        mediaController.setMediaPlayer((GifDrawable) test_gif.getDrawable());        test_gif.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                mediaController.show();                Snackbar.make(test_gif, "可以点击哦", Snackbar.LENGTH_LONG).show();            }        });    }}

其中项目中的图片是我在百度上随便找的一张gif格式 的图片,运行效果就是手机上有两张动态图片,其中一张上有文字“这个TextView的背景好酷”。

0 1
原创粉丝点击