Android原生实现显示gif

使用android原生Api实现gif的显示,记录一下

 

public class GifActivity extends AppCompatActivity {
    private ImageView gif_iv;

    private Task task;

    private Handler handler = new Handler(new Handler.Callback() {
        @SuppressLint("NewApi")
        @Override
        public boolean handleMessage(Message msg) {
            Object obj = msg.obj;
            if (obj != null && gif_iv != null) {
                if (obj instanceof Bitmap) {
                    Bitmap bitmap = (Bitmap) obj;
                    gif_iv.setImageBitmap(bitmap);
                } else if (obj instanceof AnimatedImageDrawable) {
                    AnimatedImageDrawable animatedImageDrawable = (AnimatedImageDrawable) obj;
                    animatedImageDrawable.start();
                    gif_iv.setImageDrawable(animatedImageDrawable);
                }
            }
            return false;
        }
    });
    private Runnable runnable;


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

        gif_iv = findViewById(R.id.gif_iv);
        //资源id
        int rawResId = R.raw.waving_bear;

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
            task = new Task(handler, rawResId);
            task.execute();
        } else {
            // Use the RefreshImageRunnable class and runnable to quickly display images for a GIF/video UI experience
            InputStream gifInputStream = getResources().openRawResource(rawResId);
            runnable = new RefreshImageRunnable(Movie.decodeStream(gifInputStream), handler);
            handler.postDelayed(runnable, 100);
        }
    }

    @RequiresApi(api = Build.VERSION_CODES.P)
    private static class Task extends AsyncTask<Void, Void, AnimatedImageDrawable> {
        private Handler handler;
        private int rawResId;

        Task(Handler handler, int rawResId) {
            this.handler = handler;
            this.rawResId = rawResId;
        }

        @Override
        protected AnimatedImageDrawable doInBackground(Void... voids) {
            AnimatedImageDrawable animatedImageDrawable = null;
            try {
                ImageDecoder.Source source = ImageDecoder.createSource(MyApplication.getInstance().getApplicationContext().getResources(), rawResId);
                Drawable drawable = ImageDecoder.decodeDrawable(source);
                animatedImageDrawable = (AnimatedImageDrawable) drawable;
            } catch (IOException e) {
                e.printStackTrace();
            }
            return animatedImageDrawable;
        }

        @Override
        protected void onPostExecute(AnimatedImageDrawable animatedImageDrawable) {
            super.onPostExecute(animatedImageDrawable);
            handler.obtainMessage(0, animatedImageDrawable).sendToTarget();
        }
    }

    private static class RefreshImageRunnable implements Runnable {
        private Movie movie;
        private Handler handler;
        private long movieStart;
        private Bitmap bitmap;
        private Canvas canvas;

        RefreshImageRunnable(Movie movie, Handler handler) {
            this.movie = movie;
            this.handler = handler;
            bitmap = Bitmap.createBitmap(movie.width(), movie.height(), Bitmap.Config.ARGB_8888);
            canvas = new Canvas(bitmap);
        }

        @Override
        public void run() {
            long now = android.os.SystemClock.uptimeMillis();
            if (movieStart == 0) {
                movieStart = now;
            }

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

            movie.setTime((int) ((now - movieStart) % dur));
            movie.draw(canvas, 0, 0);
            handler.obtainMessage(0, bitmap).sendToTarget();
            handler.postDelayed(this, 50);
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
        if (handler != null) {
            handler.removeCallbacks(runnable);
        }

        if (task != null) {
            boolean cancelled = task.isCancelled();
            if (!cancelled) {
                task.cancel(true);
            }
        }
    }
}

 

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:mapbox="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ui.map.AnimatedImageGifActivity">

    <ImageView
        android:id="@+id/gif_iv"
        android:layout_width="200dp"
        android:layout_height="200dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>

 

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值