Android自定义控件码脑的 Android 成长进阶之路Android

Android Gif 控件(android-gif-drawa

2016-07-12  本文已影响2361人  码脑

1. android-gif-drawable 的介绍

2. 配置和简单实例

2.1 XML模式

2.2 代码模式

3. Demo

4. 最后


1.android-gif-drawable 的介绍

Views and Drawable for displaying animated GIFs on Android
官方地址

2.1 配置

Setup
Gradle (Android Studio)
Insert the following dependency to build.gradle
file of your project.

dependencies { 
        compile 'pl.droidsonroids.gif:android-gif-drawable:1.1.17'
}

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

buildscript { 
      repositories { 
              mavenCentral() 
      }
}

allprojects { 
      repositories { 
              mavenCentral() 
      }
}

2. 配置和简单实例

2.1 XML模式

直接将View部分的代码嵌入Layout文件中,添加Gif源,便可以正常工作:

<pl.droidsonroids.gif.GifImageView 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:src="@drawable/src_anim" 
       android:background="@drawable/bg_anim" 
/>

2.2 代码模式

<pl.droidsonroids.gif.GifImageButton    
android:id="@+id/gifImageButton"    
android:layout_width="match_parent"    
android:layout_height="match_parent"   
 />

  gifImageButton =(GifImageButton) findViewById(R.id.gifImageButton);
  gifImageButton.setImageResource(R.drawable.gif1);
  final MediaController mc = new MediaController(this);
  mc.setMediaPlayer( ( GifDrawable ) gifImageButton.getDrawable() );
  mc.setAnchorView( gifImageButton );
  gifImageButton.setOnClickListener( new View.OnClickListener(){    
        @Override    
        public void onClick ( View v )    {        
              mc.show();    
        }  
  }
 );

当然方法不是只有这一种,官方还有更多的方法,有时间再慢慢添加

GifImageView, GifImageButton and GifTextView
have also hooks for setters implemented. So animated GIFs can be set by calling setImageResource(int resId)
and setBackgroundResource(int resId)
GifDrawable
can be constructed directly from various sources:

动画的控制接口:

Animation control
![Uploading GIF-demo_024092.gif . . .]
GifDrawable
implements an Animatable
and MediaPlayerControl
so you can use its methods and more:
stop()

3. Demo

GIF-demo.gif

4.最后

源码下载

上一篇 下一篇

猜你喜欢

热点阅读