gpt4 book ai didi

java - 切换背景图像

转载 作者:行者123 更新时间:2023-12-01 11:44:14 24 4
gpt4 key购买 nike

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

我用过这个:

1-在可绘制文件夹中使用我的图像创建一个 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-为我的 Activity 创建一个 View 切换器布局;

和 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();
}

谁能帮忙吗????

最佳答案

您可以将 ViewSwitcher 替换为 ImageView,如下所示(也在您的布局中替换它):

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

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

关于java - 切换背景图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29283959/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com