android - 使用 GIF 和 Glide 库创建动画启动

标签 android animation android-glide

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

为了实现这一目标,我做了以下事情:

  1. 创建了splash.xml,其中包含 ImageView 。

    <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'

最佳答案

你可以试试这个方法

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/

enter image description here

关于android - 使用 GIF 和 Glide 库创建动画启动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42974096/

相关文章:

Javascript 函数不打开嵌套可折叠

android - 是否可以使用滑动库制作背景模糊的图像 centerInside ?

java - Glide 4 - 特定调用的 ResourceDecoder

java - 从 RemoteViewsFactory 启动 Activity

java - 更新 android studio 后错误未显示且建议未出现在 xml 文件中

android - android update project --path 命令中的路径是什么

java - 如何使用Parcelable?

animation - 如何使用Python 3.x在控制台应用程序中创建ASCII动画?

jquery - 如果在 jQuery 中动画停止,如何防止回调?

android - 如何使用 Glide v4.0.0RC1 从 Url 加载 Image 到 ImageView