Android Gif 控件(android-gif-drawa

作者: 码脑 | 来源:发表于2016-07-12 23:22 被阅读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 代码模式

    • 编写XML:
    <pl.droidsonroids.gif.GifImageButton    
    android:id="@+id/gifImageButton"    
    android:layout_width="match_parent"    
    android:layout_height="match_parent"   
     />
    
    • code:
    
      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()

    • stops the animation, can be called from any thread
      start()
    • starts the animation, can be called from any thread
      isRunning()
    • returns whether animation is currently running or not
      reset()
    • rewinds the animation, does not restart stopped one
      setSpeed(float factor)
    • sets new animation speed factor, eg. passing 2.0f will double the animation speed
      seekTo(int position)
    • seeks animation (within current loop) to given position
      (in milliseconds)
      getDuration()
    • returns duration of one loop of the animation
      getCurrentPosition()
    • returns elapsed time from the beginning of a current loop of animation

    3. Demo

    GIF-demo.gif

    4.最后

    源码下载

    相关文章

      网友评论

      本文标题:Android Gif 控件(android-gif-drawa

      本文链接:https://www.haomeiwen.com/subject/zjljjttx.html