In Android, images in GIF format cannot be directly loaded, but can only be implemented through the auxiliary class movie. The specific method is as follows;
public class DrawMusicNote extends View {private Movie mMovie;private long mMovieStart;//Set to false to use decodeByteArrayprivate static final boolean DECODE_STREAM = true;public DrawMusicNote(Context context, AttributeSet attrs) {super(context, attrs);//setWillNotDraw(false);InputStream is = context.getResources().openRawResource(R.drawable.musical_note);Log.e("load", "loading"); if (DECODE_STREAM) { mMovie = Movie.decodeStream(is); } else { byte[] array = streamToBytes(is); mMovie = Movie.decodeByteArray(array, 0, array.length); }}@Overrideprotected void onDraw(Canvas canvas) {Log.e("ondraw", "drawing");long now = android.os.SystemClock.uptimeMillis(); if (mMovieStart == 0) { // first time mMovieStart = now; } if (mMovie != null) {Log.e("ondraw", "noving"); int dur = mMovie.duration(); if (dur == 0) { dur = 1000; } int relTime = (int)((now - mMovieStart) % dur); mMovie.setTime(relTime); mMovie.draw(canvas, getWidth() - mMovie.width(), getHeight() - mMovie.height()); invalidate(); }//super.onDraw(canvas);}private byte[] streamToBytes(InputStream is) { ByteArrayOutputStream os = new ByteArrayOutputStream(1024); byte[] buffer = new byte[1024]; int len; try { while ((len = is.read(buffer)) >= 0) { os.write(buffer, 0, len); } } catch (java.io.IOException e) { } return os.toByteArray(); }}
OK, that's simple.