Java Android动画逐帧失败

Java Android动画逐帧失败,java,android,animation,drawable,scale,Java,Android,Animation,Drawable,Scale,我刚接触安卓系统,只想开始一帧一帧的动画,每一帧的图像都进行了适当的缩放。我从类型化数组资源中获取帧,然后将它们添加到AnimationDrawable对象中。看起来帧已成功添加,但动画未在emulator中启动-发生异常并返回null。这是我的密码 SecondActivity.java package mypackage; import android.app.Activity; import android.content.res.Resources; import android.co

我刚接触安卓系统,只想开始一帧一帧的动画,每一帧的图像都进行了适当的缩放。我从类型化数组资源中获取帧,然后将它们添加到AnimationDrawable对象中。看起来帧已成功添加,但动画未在emulator中启动-发生异常并返回null。这是我的密码

SecondActivity.java

package mypackage;

import android.app.Activity;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.util.Log;
import android.view.MenuItem;
import android.widget.ImageView;
import android.widget.Toast;


public class SecondActivity extends Activity {


    AnimationDrawable xAnimation;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
        ImageView vImage = (ImageView) findViewById(R.id.imageView1);
        Resources res = getResources();
        TypedArray pics_Res = res.obtainTypedArray(R.array.pics_res);
        AnimationDrawable xAnimation = new AnimationDrawable();
        xAnimation.setOneShot(false);
        Drawable Mypic;

        try {
        for(int i = 1; i<4; i++)
        {

            Mypic = pics_Res.getDrawable(i);
            vImage.setImageDrawable(Mypic);
            vImage.setAdjustViewBounds(true);
            xAnimation.addFrame(vImage.getDrawable(), 2000);
            Mypic = null;

        }


        }
        catch (Exception e){
            Log.e("activity_second", "exception occured = " + e.getMessage());
        }
        finally{
            /*System.exit(1);*/ 
        }
    }


      public void onWindowFocusChanged(boolean hasFocus) {

        super.onWindowFocusChanged(hasFocus);

      if(hasFocus) 
          try {
              Log.e("second_activity", "animation started");

              if (xAnimation != null) {
                  xAnimation.start();
              }
              else
              Log.e("second_activity", "animation is null");
          }
          catch (Exception e){
            Log.e("activity_second", "exception occured = " + e.getMessage());
          }
          finally{
              /*System.exit(1); */
          }
      }
package-mypackage;
导入android.app.Activity;
导入android.content.res.Resources;
导入android.content.res.TypedArray;
导入android.graphics.drawable.AnimationDrawable;
导入android.graphics.drawable.drawable;
导入android.os.Bundle;
导入android.support.v4.app.NavUtils;
导入android.util.Log;
导入android.view.MenuItem;
导入android.widget.ImageView;
导入android.widget.Toast;
公共类SecondActivity扩展了活动{
动画可绘制动画;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_秒);
ImageView vImage=(ImageView)findViewById(R.id.imageView1);
Resources res=getResources();
TypedArray pics_Res=Res.obtainTypedArray(R.array.pics_Res);
AnimationDrawable xAnimation=新的AnimationDrawable();
xAnimation.setOneShot(假);
可牵引性近视;
试一试{
对于(int i=1;i
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <array name="pics_res">
        <item>@drawable/pic1</item>
        <item>@drawable/pic2</item>
        <item>@drawable/pic3</item>
        <item>@drawable/pic4</item>
        <item>@drawable/pic5</item>
    </array>

</resources>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >


<ImageView
    android:id="@+id/imageView1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"

/>

</LinearLayout>