使用GIF和Glide库创建动画启动画面

时间:2017-03-23 10:59:10

标签: android animation android-glide

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

我已经做了以下事情来实现这个目标:

  1. 创建了包含imageview的splash.xml。

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

  2. SplashActivity.java内容

  3. 
            @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'

3 个答案:

答案 0 :(得分:2)

你可以试试这种方式

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

答案 1 :(得分:0)

试试这个:

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);

答案 2 :(得分:0)

Android不支持GIF,因此请避免使用它,因为它耗费内存。 Glide是很好的图像缓存库,幸运的是它支持gif图像。

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

  1. 像往常一样使用图片视图创建splash.xml。
  2. 从你的gif图片中取出每一帧。我使用了splash_hdpi_1,splash_hdpi_2,splash_hdpi_3等。
  3. 使用以下代码在drawable中创建<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>
  4. SplashActivity.java
    1. ImageView ivImage = (ImageView) findViewById(R.id.your_image_view); ivImage.setBackgroundResource(R.drawable.splash_gif_animation); AnimationDrawable splashAnimation = (AnimationDrawable) ivImage.getBackground(); splashAnimation.start();下面的代码
    2. ITextHover
      1. 你完成了:)