Android アプリケーションでアニメーション GIF を再生したいと考えています。
WebView がこのコードで正常に動作することに気付きました:
webview.loadUrl("file:///android_asset/gif.gif");
残念ながら、webView はメモリを大量に消費するため、アプリが頻繁にクラッシュします。
このようなムービーを使用した多くのチュートリアルを見てきました。
public class GIFView extends View {
private Movie movie;
private long moviestart;
public GIFView(Context context) throws IOException {
super(context);
movie = Movie.decodeStream(getResources().getAssets().open("gif.gif"));
}
public GIFView(Context context, AttributeSet attrs) throws IOException {
super(context, attrs);
movie = Movie.decodeStream(getResources().getAssets().open("gif.gif"));
}
public GIFView(Context context, AttributeSet attrs, int defStyle)
throws IOException {
super(context, attrs, defStyle);
movie = Movie.decodeStream(getResources().getAssets().open("gif.gif"));
}
private long _start_time;
@Override
protected void onDraw(Canvas canvas) {
canvas.drawColor(Color.RED);
super.onDraw(canvas);
long _current_time = android.os.SystemClock.uptimeMillis();
if (0 == _start_time) {
_start_time = _current_time;
}
if (null != movie) {
final int _relatif_time = (int) ((_current_time - _start_time) % movie
.duration());
movie.setTime(_relatif_time);
Log.i("",""+_relatif_time);
double scalex = (double) this.getWidth() / (double) movie.width();
double scaley = (double) this.getHeight() / (double) movie.height();
canvas.scale((float) scalex, (float) scaley);
movie.draw(canvas, (float) scalex, (float) scaley)
}
this.invalidate();
}
}
大きな赤いウィンドウが表示されると、Canvas が描画されていることがわかります。ログを追加すると、ムービーが null ではなく、長さがあることがわかります。
残念ながら、キャンバスには大きな赤い長方形以外は何も表示されません。別の gif ファイルでテストしました。
なぜこれが機能しないのですか?