Oce*_*lue 17 animation android
我有一个关于开始逐帧动画的基本问题.
当我直接从我的代码调用AnimationDrawable.start()方法时,它似乎不起作用.
public void onCreate(Bundle savedInstanceState) {
...
mAnimation.start();
...
}
Run Code Online (Sandbox Code Playgroud)
但是如果我把这一行放在按钮的onClick()回调方法中,按下按钮就会启动动画.
为什么这行不在代码中?
谢谢!
public class MyAnimation extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
AnimationDrawable mframeAnimation = null;
super.onCreate(savedInstanceState);
setContentView(R.layout.my_animation);
ImageView img = (ImageView) findViewById(R.id.imgMain);
BitmapDrawable frame1 = (BitmapDrawable) getResources().getDrawable(
R.drawable.splash1);
BitmapDrawable frame2 = (BitmapDrawable) getResources().getDrawable(
R.drawable.splash2);
int reasonableDuration = 250;
mframeAnimation = new AnimationDrawable();
mframeAnimation.setOneShot(false);
mframeAnimation.addFrame(frame1, reasonableDuration);
mframeAnimation.addFrame(frame2, reasonableDuration);
img.setBackgroundDrawable(mframeAnimation);
mframeAnimation.setVisible(true, true);
//If this line is inside onClick(...) method of a button, animation works!!
mframeAnimation.start();
}
Run Code Online (Sandbox Code Playgroud)
}
Ale*_*voy 36
重要的是要注意,在Activity的onCreate()方法中,无法调用在AnimationDrawable上调用的start()方法,因为AnimationDrawable尚未完全附加到窗口.如果您想立即播放动画而不需要交互,那么您可能希望从Activity中的onWindowFocusChanged()方法调用它,当Android将窗口置于焦点时将调用它.页面的最后 http://developer.android.com/guide/topics/graphics/2d-graphics.html
ImageView img = (ImageView)findViewById(R.id.some layout);
AnimationDrawable frameAnimation = (AnimationDrawable)img.getDrawable();
frameAnimation.setCallback(img);
frameAnimation.setVisible(true, true);
frameAnimation.start();
Run Code Online (Sandbox Code Playgroud)
并添加动画,你可以做类似的事情
<animation-list android:id="@+id/my_animation" android:oneshot="false"
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/frame1" android:duration="150" />
<item android:drawable="@drawable/frame2" android:duration="150" />
</animation-list>
Run Code Online (Sandbox Code Playgroud)
小智 22
使用Runnable将start()消息插入消息队列,只需添加此LOC即可替换mFrameAnimation.start();
img.post(new Starter());
Run Code Online (Sandbox Code Playgroud)
助手内班:
class Starter implements Runnable {
public void run() {
mFrameAnimation.start();
}
}
Run Code Online (Sandbox Code Playgroud)
小智 5
在onCreate(...)中播放动画添加:
ImageView mImageView=(ImageView) findViewById(R.id.image);
mImageView.setBackgroundResource(R.anim.film);
mFrameAnimation = (AnimationDrawable) mImageView.getBackground();
mImageView.post(new Runnable(){
public void run(){
mFrameAnimation.start();
}
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
32378 次 |
| 最近记录: |