帧动画在第一个activity中不能播放

帧动画在第一个activity中不能播放,必须要通过事件触发才行,解决:

 

 

package com.lik;

 

import android.app.Activity;

import android.graphics.drawable.AnimationDrawable;

import android.os.Bundle;

import android.view.KeyEvent;

import android.view.MotionEvent;

import android.view.View;

import android.view.ViewTreeObserver.OnPreDrawListener;

import android.widget.ImageView;

 

public class lik extends Activity {

    AnimationDrawable ad;

 

    @Override

    public void onCreate(Bundle savedInstanceState) {

            super.onCreate(savedInstanceState);

            setContentView(R.layout.main);

            ImageView iv = (ImageView) findViewById(R.id.animationImage);

            iv.setBackgroundResource(R.drawable.frame_animation);

            ad = (AnimationDrawable) iv.getBackground();

            iv.getViewTreeObserver().addOnPreDrawListener(opdl);

    }

    OnPreDrawListener opdl=new OnPreDrawListener(){

 

            @Override

            public boolean onPreDraw() {

                    ad.start();

                    return true;

            }

 

    };

 

}



<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false" >
 
    <item android:drawable="@drawable/a1" android:duration="1500" />
    <item android:drawable="@drawable/a2" android:duration="1500" />
    <item android:drawable="@drawable/a3" android:duration="1500" />
 
</animation-list>





<ImageView 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:id="@+id/animationImage"></ImageView>



你可能感兴趣的:(Activity)