首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >安卓AnimationDrawable

安卓AnimationDrawable
EN

Stack Overflow用户
提问于 2013-03-04 18:36:10
回答 1查看 1.9K关注 0票数 1

如何从代码而不是从xml加载图像到动画,我有这个例子:

代码语言:javascript
复制
<!-- 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文件:

代码语言:javascript
复制
 ImageView img = (ImageView)findViewById(R.id.spinning_wheel_image);
 img.setBackgroundResource(R.drawable.spin_animation);
 AnimationDrawable frameAnimation = (AnimationDrawable) img.getBackground();
 frameAnimation.start();

我需要像这样加载图像:

代码语言:javascript
复制
for (int j = 0; j < 5; j++) 
     animation.addFrame(getResources().getDrawable(getResources().getIdentifier("wheel" + j,"drawable", getPackageName())), i); 

我怎样才能把它组合起来呢?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-03-04 19:29:28

代码语言:javascript
复制
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..
    }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15199560

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档