gpt4 book ai didi

android - 当音频文件在 android 后台运行时更改背景图像

转载 作者:太空宇宙 更新时间:2023-11-03 13:46:18 25 4
gpt4 key购买 nike

当音频文件在后台运行时,我正在尝试更改 Android 应用程序中的图像,但我遇到了问题。下面是我的代码:

public class SongActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_song);
final RelativeLayout rel = (RelativeLayout)findViewById(R.id.activity_song);
MediaPlayer mp = MediaPlayer.create(SongActivity.this,R.raw.thesong);
mp.start();
rel.setBackgroundResource(R.drawable.bday);
Runnable r = new Runnable() {
@Override
public void run() {
synchronized (this){
try {
wait(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
};
Thread test = new Thread(r);
test.start();
rel.setBackgroundResource(R.drawable.bday2);
}
}

请告知我是否可以修改它或者它需要彻底检修。提前致谢。

最佳答案

在您的 xml 中添加 ID。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/linear_layout">

</LinearLayout>

在代码中替换

final RelativeLayout rel = (RelativeLayout)findViewById(R.id.activity_song);

      final RelativeLayout rel = (RelativeLayout)findViewById(R.id.linear_layout);
Resources res = getResources();
Drawable drawable = res.getDrawable(R.drawable.image);
rel.setBackground(drawable);

关于android - 当音频文件在 android 后台运行时更改背景图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41627480/

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