使用 GIF 和 Glide 库创建动画飞溅

Creating animated splash using GIF and Glide library

我想使用 gif 图像创建动画启动画面。我使用了 Glide 库,因为它支持 gif 图像。

我做了以下事情来实现这一点:

  1. 已创建 splash.xml,其中包含图像视图。

    <ImageView
        android:id="@+id/iv_gif_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
    

  2. 里面SplashActivity.java


        @Override
        protected void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_splash);

            ImageView ivImage = (ImageView) findViewById(R.id.iv_gif_view);
            Glide.with(this).load(R.drawable.splash_xxhdpi_2)
                    .asGif().into(ivImage);
        }

但是当我 运行 应用程序屏幕变黑时,屏幕上什么也没有显示。我正在使用 Glide compile 'com.github.bumptech.glide:glide:3.6.1'

试试这个:

ImageView ivImage= (ImageView) findViewById(R.id.iv_gif_view);
GlideDrawableImageViewTarget imageViewTarget = new GlideDrawableImageViewTarget(ivImage);
Glide.with(this)
     .load(R.drawable.splash_xxhdpi_2)
     .into(imageViewTarget);

Android 不支持 GIF,因此请避免使用它,因为它会占用大量内存。 Glide 是一个很好的图像缓存库,幸运的是它支持 gif 图像。

我正在分享实现动画启动画面的另一种有效方法。

  1. 像往常一样使用图像视图创建splash.xml。
  2. 从你的 gif 图像中取出每一帧。例如,我使用了 splash_hdpi_1、splash_hdpi_2、splash_hdpi_3。
  3. 使用以下代码在可绘制对象中创建 splash_gif_animation.xml
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:oneshot="false" >
    <item
        android:drawable="@drawable/splash_hdpi_1"
        android:duration="150"/>
    <item
        android:drawable="@drawable/splash_hdpi_2"
        android:duration="150"/>
    <item
        android:drawable="@drawable/splash_hdpi_3"
        android:duration="150"/>
</animation-list>
  1. 在您的 SplashActivity.java 中输入下面的代码
ImageView ivImage = (ImageView) findViewById(R.id.your_image_view);
ivImage.setBackgroundResource(R.drawable.splash_gif_animation);

AnimationDrawable splashAnimation = (AnimationDrawable) ivImage.getBackground();
splashAnimation.start();
  1. 大功告成:)

你可以这样试试

public class GifImageView extends View {

    private InputStream mInputStream;
    private Movie mMovie;
    private int mWidth, mHeight;
    private long mStart;
    private Context mContext;

    public GifImageView(Context context) {
        super(context);
        this.mContext = context;
    }

    public GifImageView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public GifImageView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.mContext = context;
        if (attrs.getAttributeName(1).equals("background")) {
            int id = Integer.parseInt(attrs.getAttributeValue(1).substring(1));
            setGifImageResource(id);
        }
    }


    private void init() {
        setFocusable(true);
        mMovie = Movie.decodeStream(mInputStream);
        mWidth = mMovie.width();
        mHeight = mMovie.height();

        requestLayout();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        setMeasuredDimension(mWidth, mHeight);
    }

    @Override
    protected void onDraw(Canvas canvas) {

        long now = SystemClock.uptimeMillis();

        if (mStart == 0) {
            mStart = now;
        }

        if (mMovie != null) {

            int duration = mMovie.duration();
            if (duration == 0) {
                duration = 1000;
            }

            int relTime = (int) ((now - mStart) % duration);

            mMovie.setTime(relTime);

            mMovie.draw(canvas, 0, 0);
            invalidate();
        }
    }

    public void setGifImageResource(int id) {
        mInputStream = mContext.getResources().openRawResource(id);
        init();
    }

    public void setGifImageUri(Uri uri) {
        try {
            mInputStream = mContext.getContentResolver().openInputStream(uri);
            init();
        } catch (FileNotFoundException e) {
            Log.e("GIfImageView", "File not found");
        }
    }
}

MainActivity.java

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        GifImageView gifImageView = (GifImageView) findViewById(R.id.GifImageView);
        gifImageView.setGifImageResource(R.drawable.android);
    }
}

http://www.mavengang.com/2016/05/02/gif-animation-android/

http://www.geeks.gallery/how-to-display-the-animated-gif-image-in-android/