如何显示GIF文件

时间:2016-10-06 06:25:46

标签: android gif

我使用这个library作为指南来展示一个&#39; gif&#39;文件。在gif中使用保存的drawable时,gif我会正确显示<pl.droidsonroids.gif.GifImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/imgView1" android:src="@drawable/gifFile" />

src

但是在删除 GifImageView gifFromFile = (GifImageView) findViewById(R.id.imgView1); gifFromFile.setImageBitmap(Utils.getBitmapImagefromStorage(context, imageHome)); 时尝试像在此一样初始化活动中的GifImageView

getBitmapImagefromStorage

imageHome获取路径并且If given drawable is not a GIF then mentioned Views work like plain ImageView and ImageButton.获取文件名时,gif文件不会像图像一样播放其唯一的显示。有人说gif但我提供的文件是png。我想知道我是否正确使用它。

此外,该文件可以是gifno-shared,因此我需要支持这两个文件。

4 个答案:

答案 0 :(得分:3)

来自XML

最简单的方法是像普通的ImageView一样使用GifImageView(或GifImageButton):

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

如果android:src和/或android:background声明的drawable是GIF文件,那么它们将被自动识别为GifDrawables并设置动画。如果给定的drawable不是GIF,那么提到的Views就像普通的ImageView和ImageButton一样。

GifTextView允许您将GIF用作复合绘图和背景。

<pl.droidsonroids.gif.GifTextView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:drawableTop="@drawable/left_anim"
    android:drawableStart="@drawable/left_anim"
    android:background="@drawable/bg_anim"
    />

来自Java代码 GifImageView,GifImageButton和GifTextView也为实现的setter提供了钩子。因此,可以通过调用setImageResource(int resId)setBackgroundResource(int resId)

来设置动画GIF

GifDrawable可以直接从各种来源构建:

 //asset file
    GifDrawable gifFromAssets = new GifDrawable( getAssets(), "anim.gif" );

    //resource (drawable or raw)
    GifDrawable gifFromResource = new GifDrawable( getResources(), R.drawable.anim );

    //byte array
    byte[] rawGifBytes = ...
    GifDrawable gifFromBytes = new GifDrawable( rawGifBytes );

    //FileDescriptor
    FileDescriptor fd = new RandomAccessFile( "/path/anim.gif", "r" ).getFD();
    GifDrawable gifFromFd = new GifDrawable( fd );

    //file path
    GifDrawable gifFromPath = new GifDrawable( "/path/anim.gif" );

    //file
    File gifFile = new File(getFilesDir(),"anim.gif");
    GifDrawable gifFromFile = new GifDrawable(gifFile);

    //AssetFileDescriptor
    AssetFileDescriptor afd = getAssets().openFd( "anim.gif" );
    GifDrawable gifFromAfd = new GifDrawable( afd );

    //InputStream (it must support marking)
    InputStream sourceIs = ...
    BufferedInputStream bis = new BufferedInputStream( sourceIs, GIF_LENGTH );
    GifDrawable gifFromStream = new GifDrawable( bis );

    //direct ByteBuffer
    ByteBuffer rawGifBytes = ...
    GifDrawable gifFromBytes = new GifDrawable( rawGifBytes );

如果不再需要GifDrawable,则在终结器中自动关闭InputStreams,因此您不需要显式关闭它们。调用recycle()也将关闭基础输入源。

请注意,所有输入源都需要能够回放到开头。需要正确播放动画GIF(动画可重复),因为后续帧是根据需要从源中解码的。

参考:https://github.com/koral--/android-gif-drawable

答案 1 :(得分:1)

GifTextView gifImageView;

在onCreate

gifImageView = (GifTextView) findViewById(R.id.imageView);

在onCreate中调用此方法

public void playGif(){
        Animation fadeout = new AlphaAnimation(1.f, 1.f);
        fadeout.setDuration(2500); // You can modify the duration here
        fadeout.setAnimationListener(new Animation.AnimationListener() {

            @Override
            public void onAnimationStart(Animation animation) {
                gifImageView.setBackgroundResource(R.drawable.gif_image);//your gif file
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
            }

            @Override
            public void onAnimationEnd(Animation animation) {

            }
        });
        gifImageView.startAnimation(fadeout);
    }

在您的布局文件中

<pl.droidsonroids.gif.GifTextView
        android:id="@+id/imageView"
        android:layout_width="fill_parent"
        android:scaleType="fitXY"
        android:layout_height="fill_parent"
        />

答案 2 :(得分:0)

尝试这种方式在您的应用中使用GIF

public class PlayGifView extends View{

     private static final int DEFAULT_MOVIEW_DURATION = 1000;

     private int mMovieResourceId;
     private Movie mMovie;

     private long mMovieStart = 0;
     private int mCurrentAnimationTime = 0;

     @SuppressLint("NewApi")
     public PlayGifView(Context context, AttributeSet attrs) {
         super(context, attrs);

        /**
         * Starting from HONEYCOMB have to turn off HardWare acceleration to draw
         * Movie on Canvas.
         */
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            setLayerType(View.LAYER_TYPE_SOFTWARE, null);
        }
    }

     public void setImageResource(int mvId){
         this.mMovieResourceId = mvId;
    mMovie = Movie.decodeStream(getResources().openRawResource(mMovieResourceId));
    requestLayout();
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    if(mMovie != null){
        setMeasuredDimension(mMovie.width(), mMovie.height());
    }else{
        setMeasuredDimension(getSuggestedMinimumWidth(), getSuggestedMinimumHeight());
    }
}

@Override
protected void onDraw(Canvas canvas) {
    if (mMovie != null){
        updateAnimtionTime();
        drawGif(canvas);
        invalidate();
    }else{
        drawGif(canvas);
    }
}

private void updateAnimtionTime() {
    long now = android.os.SystemClock.uptimeMillis();

    if (mMovieStart == 0) {
        mMovieStart = now;
    }
    int dur = mMovie.duration();
    if (dur == 0) {
        dur = DEFAULT_MOVIEW_DURATION;
    }
    mCurrentAnimationTime = (int) ((now - mMovieStart) % dur);
}

private void drawGif(Canvas canvas) {
    mMovie.setTime(mCurrentAnimationTime);
    mMovie.draw(canvas, 0, 0);
    canvas.restore();
}

}

在您的活动类

PlayGifView pGif = (PlayGifView) findViewById(R.id.viewGif);
pGif.setImageResource(R.drawable.yourgifimage);

然后在您的XML

<YourPackageName.PlayGifView
            android:id="@+id/viewGif"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center" />

答案 3 :(得分:0)

我尝试使用GifDrawable

File gifFile = new File(context.getExternalFilesDir(null)
                    .getAbsolutePath(), imageHome);
GifDrawable gifFromPath  = new GifDrawable(gifFile);

然后我使用setImageBitmap

而不是setImageDrawable
GifImageView gifImageView = (GifImageView) findViewById(R.id.textViewDealFinder);
        gifImageView.setImageDrawable(gifFromPath);