我如何从代码而不是从xml将图像加载到动画,我有以下示例:

<!-- Animation frames are wheel0.png -- wheel5.png files inside the
res/drawable/ folder -->
<animation-list android:id="@+id/selected" android:oneshot="false">
<item android:drawable="@drawable/wheel0" android:duration="50" />
<item android:drawable="@drawable/wheel1" android:duration="50" />
<item android:drawable="@drawable/wheel2" android:duration="50" />
<item android:drawable="@drawable/wheel3" android:duration="50" />
<item android:drawable="@drawable/wheel4" android:duration="50" />
<item android:drawable="@drawable/wheel5" android:duration="50" />
</animation-list>


Java文件:

 ImageView img = (ImageView)findViewById(R.id.spinning_wheel_image);
 img.setBackgroundResource(R.drawable.spin_animation);
 AnimationDrawable frameAnimation = (AnimationDrawable) img.getBackground();
 frameAnimation.start();


我需要像这样加载图像:

for (int j = 0; j < 5; j++)
     animation.addFrame(getResources().getDrawable(getResources().getIdentifier("wheel" + j,"drawable", getPackageName())), i);


我该如何结合呢?

最佳答案

int []imageArray={R.drawable.img1,R.drawable.img2,R.drawable.img3};


final Handler handler = new Handler();
         Runnable runnable = new Runnable() {
            int i=0;
            public void run() {
                imageView.setImageResource(imageArray[i]);
                i++;
                if(i>imageArray.length-1)
                {
                i=0;
                }
                handler.postDelayed(this, 50);  //for interval...
            }
        };
        handler.postDelayed(runnable, 2000); //for initial delay..
    }

09-08 07:42