Android原生实现显示gif

Android原生实现显示gif_第1张图片

使用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 {
        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);
            }
        }
    }
}

 




    

 

你可能感兴趣的:(开发需求,gif)