start.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { iv.setImageResource(R.drawable.anim); AnimationDrawable drawable = (AnimationDrawable) iv.getDrawable(); drawable.start(); } }); stop.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { AnimationDrawable drawable = (AnimationDrawable) iv.getDrawable(); drawable.stop(); } });
本人设置了两个按钮
开始按钮,让帧动画开始执行,
停止按钮,帧动画会停止,显示当前跳到的图片,
本人想问有没有办法可以获取到停止时显示的图片?
解决方案
40
可以的,本人做了个小Demo,关键方法是
drawable.getCurrent()
,获取当前显示的帧。
public class MainActivity extends AppCompatActivity { @BindView(R.id.start_btn) Button startBtn; @BindView(R.id.stop_btn) Button stopBtn; @BindView(R.id.anim_image) ImageView animImage; //进行帧动画的ImageView @BindView(R.id.pick_image) ImageView pickImage; //以动画停止的那一帧作为内容 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ButterKnife.bind(this); } @OnClick({R.id.start_btn, R.id.stop_btn}) public void onClick(View view) { AnimationDrawable drawable; switch (view.getId()) { case R.id.start_btn: animImage.setImageResource(R.drawable.loading_anim); drawable = (AnimationDrawable) animImage.getDrawable(); drawable.start(); break; case R.id.stop_btn: drawable = (AnimationDrawable) animImage.getDrawable(); drawable.stop(); //关键:drawable.getCurrent() pickImage.setImageDrawable(drawable.getCurrent()); break; } } }