Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save AndrewWang1993/4f3fd5cd14abbaa43a03 to your computer and use it in GitHub Desktop.
Save AndrewWang1993/4f3fd5cd14abbaa43a03 to your computer and use it in GitHub Desktop.
select some frames from a animation-list by using reflection(使用反射从动画列表里选择几个图片来显示)
imageView = (ImageView) findViewById(R.id.myImageview);
small_imageView = (ImageView) findViewById(R.id.small_image);
imageView.setBackgroundResource(R.anim.m_anim);
animationDrawable = (AnimationDrawable) imageView.getBackground();
public void start(View view) { //A button click
if (!animationDrawable.isRunning()) {
animationDrawable.start();
controlFrame = new Thread(new Runnable() {
@Override
public void run() {
try {
Field field = AnimationDrawable.class.getDeclaredField("mCurFrame");
field.setAccessible(true);
while (true) {
int currentFrame = field.getInt(animationDrawable);
if (currentFrame == 2 || currentFrame == 3) {
field.setInt(animationDrawable, 4);
}
}
} catch (Exception e) {
Log.e("ERROR", e.getMessage());
}
}
});
controlFrame.start();
} else {
try {
controlFrame.interrupt();
} catch (Exception e) {
Log.i("Thread Stop Exception", e.getMessage());
}
animationDrawable.stop();
//// int number = animationDrawable.getNumberOfFrames();
//// int duration = animationDrawable.getDuration(1);
// Drawable drawable = animationDrawable.getCurrent();
// int len = animationDrawable.getNumberOfFrames();
// long currentTime1=System.nanoTime();
// for (int i = 0; i < len; i++) { //7ms ~ 12ms 运行时间
// if (animationDrawable.getFrame(i).equals(animationDrawable.getCurrent())) {
// Toast.makeText(this, i+"", Toast.LENGTH_LONG).show();
// small_imageView.setImageDrawable(animationDrawable.getCurrent());
// }
// }
// long currentTime2=System.nanoTime();
// Log.i("eclipseTime",currentTime2-currentTime1+"");
//// small_imageView.setImageDrawable(drawable);
//// Toast.makeText(this, number + " " + duration, Toast.LENGTH_LONG).show();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment