Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I was having the exact same issue.<br> At the moment I’m playing animation with my custom class that derives from ImageView in the onDraw method I read one image from assets folder and assign that image to canvas, every time I check if specific time (60ms in my case) has elapsed and decide if I have to show new image at the end I call postInvalidate() so that onDraw get called again. This method is working fine on my HTC Desire but I haven’t got a chance to test on devices with lower specs.<br> If no one offers a better solution I can’t post my code so other can comment it.</p> <p>EDIT:</p> <pre><code> public class AnimationView extends ImageView { private static final String TAG = "AnimView"; //public fields public String STRING_FORMAT_FOR_PADDING = "%04d"; private String FILE_TYPE = "png"; public boolean STOP_AT_LAST_FRAME = true; public int DELAY = 60; //delay between frames in milliseconds //private fields private boolean mIsPlaying = false; private boolean mStartPlaying = false; private Context mContext = null; private int play_frame = 0; private long last_tick = 0; private Bitmap curentFrame = null; private String dir; private String prefix; private int numberOfFrames; private AnimationViewAnimFinished finishListener; public AnimationView(Context context, AttributeSet attrs) { super(context, attrs); mContext = context; } public void setUpAnimation(String dir, String prefix, int numberOfFrames) { this.dir = dir; this.prefix = prefix; this.numberOfFrames = numberOfFrames; curentFrame = loadImage(1); //set first frame of animation to curent frame } public void setAnimationFinishListener(AnimationViewAnimFinished finishListener) { this.finishListener = finishListener; } @Override protected void onDraw(Canvas c) { //Log.d(TAG, "onDraw called"); if (mStartPlaying) { Log.d(TAG, "starting animation..."); play_frame = 1; mStartPlaying = false; mIsPlaying = true; c.drawBitmap(curentFrame, 0, 0, null); // blink fix postInvalidate(); } else if (mIsPlaying) { if (play_frame &gt;= numberOfFrames) { mIsPlaying = false; if (STOP_AT_LAST_FRAME) { c.drawBitmap(curentFrame, 0, 0, null); } if (finishListener != null) finishListener.animationFinished(); //finish callback called } else { long time = (System.currentTimeMillis() - last_tick); int draw_x = 0; int draw_y = 0; if (time &gt;= DELAY) //the delay time has passed. set next frame { Log.d(TAG, "drawing frame number:"+play_frame+" signature:"+curentFrame.toString()); last_tick = System.currentTimeMillis(); play_frame++; curentFrame = loadImage(play_frame); } c.drawBitmap(curentFrame, draw_x, draw_y, null); postInvalidate(); } } else //before animation starts ... drawing first frame { c.drawBitmap(curentFrame, 0, 0, null); } } private Bitmap loadImage(int numFrame) { long startOne = android.os.SystemClock.uptimeMillis(); String name =dir+ prefix + String.format(STRING_FORMAT_FOR_PADDING, numFrame)+"."+FILE_TYPE; InputStream ins = null; try { ins = mContext.getAssets().open(name); } catch (IOException e) { e.printStackTrace(); } Bitmap d = BitmapFactory.decodeStream(ins); long endOne = android.os.SystemClock.uptimeMillis(); Log.d(TAG,name+" frame decoded in: "+String.valueOf(endOne-startOne)+" ms"); return d; } //set flag for starting animation to true and inicicate redraw public void playAnimation() { Log.d(TAG,"Play animation + invalidate()"); mStartPlaying = true; postInvalidate(); } //callack class for method restaurants public static abstract class AnimationViewAnimFinished{ public abstract void animationFinished(); } </code></pre> <p>}</p> <p>In onCreate method of activity I have this:</p> <pre><code> animationHolder = (AnimationView)findViewById(R.id.imageViewAnimationHolderService); animationHolder.setUpAnimation("animation1/", "frame_", 212); animationHolder.setAnimationFinishListener(myAnimationFinishListener); </code></pre> <p>In assets I have folder “animation1” in that folder I have 212 .png images named (frame_0001.png, frame_0002.png, frame_0003.png, …, frame_0212.png)</p> <p>When I what to start animation I call:</p> <pre><code> animationHolder.playAnimation(); </code></pre> <p>And this is code for animation finish listener:</p> <pre><code> AnimationViewAnimFinished myAnimationFinishListener = new AnimationViewAnimFinished() { @Override public void animationFinished() { Log.d(TAG,"animation finished"); } }; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload