简体   繁体   English

在imageview中播放动画GIF图像文件

[英]Playing an animated GIF image file in imageview

I am making an Android application, in which I want to play a GIF file in an imageview. 我正在制作一个Android应用程序,我想在imageview中播放一个GIF文件。 I have a tried a lot of ways, made a custom class but haven't succeeded. 我尝试了很多方法,做了一个自定义类但没有成功。

Has anyone implemented this before? 有没有人以前实现过这个?

You can find sample code in the Android SDK samples. 您可以在Android SDK示例中找到示例代码。 The gif image is displayed in activity that extends View gif图像显示在扩展View活动中

The code to use gif image other than ImageView is 使用除ImageView之外的gif图像的代码是

import android.app.Activity;
import android.content.Context;
import android.graphics.*;
import android.graphics.drawable.*;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.*;

import java.io.IOException;
import java.io.InputStream;
import java.io.ByteArrayOutputStream;

public class BitmapDecode extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(new SampleView(this));
    }

    private static class SampleView extends View {
        private Bitmap mBitmap;
        private Bitmap mBitmap2;
        private Bitmap mBitmap3;
        private Bitmap mBitmap4;
        private Drawable mDrawable;

        private Movie mMovie;
        private long mMovieStart;

        private static byte[] streamToBytes(InputStream is) {
            ByteArrayOutputStream os = new ByteArrayOutputStream(1024);
            byte[] buffer = new byte[1024];
            int len;
            try {
                while ((len = is.read(buffer)) >= 0) {
                    os.write(buffer, 0, len);
                }
            } catch (java.io.IOException e) {
            }
            return os.toByteArray();
        }

        public SampleView(Context context) {
            super(context);
            setFocusable(true);

            java.io.InputStream is;
            is = context.getResources().openRawResource(R.drawable.beach);

            BitmapFactory.Options opts = new BitmapFactory.Options();
            Bitmap bm;

            opts.inJustDecodeBounds = true;
            bm = BitmapFactory.decodeStream(is, null, opts);

            // now opts.outWidth and opts.outHeight are the dimension of the
            // bitmap, even though bm is null

            opts.inJustDecodeBounds = false;    // this will request the bm
            opts.inSampleSize = 4;             // scaled down by 4
            bm = BitmapFactory.decodeStream(is, null, opts);

            mBitmap = bm;

            // decode an image with transparency
            is = context.getResources().openRawResource(R.drawable.frog);
            mBitmap2 = BitmapFactory.decodeStream(is);

            // create a deep copy of it using getPixels() into different configs
            int w = mBitmap2.getWidth();
            int h = mBitmap2.getHeight();
            int[] pixels = new int[w*h];
            mBitmap2.getPixels(pixels, 0, w, 0, 0, w, h);
            mBitmap3 = Bitmap.createBitmap(pixels, 0, w, w, h,
                                           Bitmap.Config.ARGB_8888);
            mBitmap4 = Bitmap.createBitmap(pixels, 0, w, w, h,
                                           Bitmap.Config.ARGB_4444);

            mDrawable = context.getResources().getDrawable(R.drawable.button);
            mDrawable.setBounds(150, 20, 300, 100);

            is = context.getResources().openRawResource(R.drawable.animated_gif);
            if (true) {
                mMovie = Movie.decodeStream(is);
            } else {
                byte[] array = streamToBytes(is);
                mMovie = Movie.decodeByteArray(array, 0, array.length);
            }
        }

        @Override protected void onDraw(Canvas canvas) {
            canvas.drawColor(0xFFCCCCCC);            

            Paint p = new Paint();
            p.setAntiAlias(true);

            canvas.drawBitmap(mBitmap, 10, 10, null);
            canvas.drawBitmap(mBitmap2, 10, 170, null);
            canvas.drawBitmap(mBitmap3, 110, 170, null);
            canvas.drawBitmap(mBitmap4, 210, 170, null);

            mDrawable.draw(canvas);

            long now = android.os.SystemClock.uptimeMillis();
            if (mMovieStart == 0) {   // first time
                mMovieStart = now;
            }
            if (mMovie != null) {
                int dur = mMovie.duration();
                if (dur == 0) {
                    dur = 1000;
                }
                int relTime = (int)((now - mMovieStart) % dur);
                mMovie.setTime(relTime);
                mMovie.draw(canvas, getWidth() - mMovie.width(),
                            getHeight() - mMovie.height());
                invalidate();
            }
        }
    }
}

You can use these links as well 您也可以使用这些链接

http://weavora.com/blog/2012/02/07/android-how-to-use-animated-gif/ http://weavora.com/blog/2012/02/07/android-how-to-use-animated-gif/

http://droid-blog.net/2011/10/14/tutorial-how-to-use-animated-gifs-in-android-part-1/ http://droid-blog.net/2011/10/14/tutorial-how-to-use-animated-gifs-in-android-part-1/

You can display Gif in android without using any 3rd party libraries. 您可以在不使用任何第三方库的情况下在Android中显示Gif。 This is how I did that. 这就是我这样做的方式。

Take a web view. 进入网络视图。

<WebView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/imageWebView" />

Most of the examples shows how to open a file from asset folder, but here I show you how to open it from a SD Card/memory. 大多数示例显示了如何从资产文件夹中打开文件,但在这里我将向您展示如何从SD卡/内存中打开它。

    WebView webView = (WebView) findViewById(R.id.imageWebView);
    String  data    = "<body> <img src = \""+ filePath+"\"/></body>";
    // 'filePath' is the path of your .GIF file on SD card.
   webView.loadDataWithBaseURL("file:///android_asset/",data,"text/html","UTF-8",null);

Natively ImageView does not support animated image. Natively ImageView不支持动画图像。 You can use two options to show animated gif file 您可以使用两个选项来显示动画gif文件

  1. Use VideoView 使用VideoView
  2. Use ImageView 使用ImageView

But split the gif file into several parts and then apply animation to it. 但是将gif文件拆分成几个部分,然后将动画应用到它。 Try this link-playing-gif-animation you will get the desired result. 试试这个链接播放-gif动画你会得到想要的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM