切换背景图像

时间:2015-03-26 16:32:44

标签: java android eclipse animation background

拜托,我尝试切换应用程序主要活动的背景图片。我在编译期间没有错误(带有eclipse的API21),但它总是在模拟器上崩溃。

我用过这个:

1 - 在drawable文件夹中创建一个带有我的图像的xml文件,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="@+id/selected" android:oneshot="false"/>
<item android:drawable="@drawable/drink" android:duration="400" />
<item android:drawable="@drawable/food" android:duration="400" />
<item android:drawable="@drawable/restaurant" android:duration="400" />
<item android:drawable="@drawable/hair" android:duration="400" />
<item android:drawable="@drawable/shop" android:duration="400" />
</animation-list> 

2-为我的活动创建一个viewswitcher布局;

和3-用这个函数创建一个主java文件:

protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ViewSwitcher img = (ViewSwitcher)findViewById(R.id.backimage);
    img.setBackgroundResource(R.drawable.backanim);

     // Get the AnimationDrawable object.
     AnimationDrawable frameAnimation = (AnimationDrawable) img.getBackground();

     // Start the animation (looped playback by default).
     frameAnimation.start(); 
}

任何人都可以帮忙吗????

1 个答案:

答案 0 :(得分:1)

您可以将ImageView替换为ViewSwitcher(在布局中也替换它):

ImageView img = (ImageView)findViewById(R.id.backimage);
img.setImageResource(R.drawable.backanim);
AnimationDrawable animation = (AnimationDrawable) imageViewAnimation.getDrawable();
animation.start();

ViewSwitcher用于在两个视图之间切换,而不是在drawable之间切换。