使用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>