- import java.io.IOException;
- import android.content.Context;
- import android.graphics.Bitmap;
- import android.graphics.Bitmap.Config;
- import android.graphics.BitmapFactory;
- import android.graphics.Canvas;
- import android.graphics.ColorFilter;
- import android.graphics.Paint;
- import android.graphics.PixelFormat;
- import android.graphics.Rect;
- import android.graphics.drawable.Drawable;
- import android.os.SystemClock;
- /**
- * AssetAnimationDrawable
- *
- * @author Alexei Garbuzenko
- *
- */
- public class AssetAnimationDrawable extends Drawable implements Runnable {
- private final Bitmap m_bitmap;
- private final Paint m_bitmapPaint;
- private final int m_width;
- private final int m_height;
- private final int m_duration;
- private final int m_frames;
- private long m_lastUpdate;
- private int m_frame;
- private Rect m_frameRect;
- public AssetAnimationDrawable(Context context, String asset, int frames,
- int fps) throws IOException {
- BitmapFactory.Options options = new BitmapFactory.Options();
- options.inPreferredConfig = Config.RGB_565; // A.G.: use 16-bit mode
- // without alpha for
- // animations
- m_bitmap = BitmapFactory.decodeStream(context.getResources()
- .getAssets().open(asset), null, options);
- m_width = m_bitmap.getWidth() / frames;
- m_height = m_bitmap.getHeight();
- m_duration = 1000 / fps;
- m_frames = frames; // A.G.: note the little gap cause of integer
- // division.
- // i.e. duration would be 33 for 30fps, meaning
- // 990ms for 30 frames.
- m_bitmapPaint = new Paint();
- m_bitmapPaint.setFilterBitmap(true);
- m_frame = 0;
- m_frameRect = new Rect(0, 0, m_width, m_height); // first frame
- m_lastUpdate = SystemClock.uptimeMillis();
- }
- @Override
- protected void finalize() throws Throwable {
- super.finalize();
- recycle();
- }
- @Override
- public void draw(Canvas canvas) {
- canvas.drawBitmap(m_bitmap, m_frameRect, copyBounds(), m_bitmapPaint);
- }
- @Override
- public int getOpacity() {
- return PixelFormat.OPAQUE;
- }
- @Override
- public void setAlpha(int a) {
- m_bitmapPaint.setAlpha(a);
- }
- @Override
- public void setColorFilter(ColorFilter filter) {
- m_bitmapPaint.setColorFilter(filter);
- }
- @Override
- public int getIntrinsicWidth() {
- return m_width;
- }
- @Override
- public int getIntrinsicHeight() {
- return m_height;
- }
- @Override
- public void run() {
- long tick = SystemClock.uptimeMillis();
- if (tick - m_lastUpdate >= m_duration) {
- m_frame = (int) (m_frame + (tick - m_lastUpdate) / m_duration)
- % m_frames;
- m_lastUpdate = tick; // TODO: time shift for incomplete frames
- m_frameRect = new Rect(m_frame * m_width, 0, (m_frame + 1)
- * m_width, m_height);
- invalidateSelf();
- }
- scheduleSelf(this, tick + m_duration);
- }
- public void start() {
- run();
- }
- public void stop() {
- unscheduleSelf(this);
- }
- public void recycle() {
- stop();
- if (m_bitmap != null && !m_bitmap.isRecycled())
- m_bitmap.recycle();
- }
- }
AnimationDrawable class that takes sprite sheet and displays it with given fps