Change the background image when the audio file is running in the background in Android
•
Android
When the audio file is running in the background, I try to change the image in the Android application, but there is a problem. Here is my code:
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);
}
}
Please let me know if I can modify it or if it needs to be overhauled. Thank you in advance
resolvent:
Add 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>
In code, just replace
final RelativeLayout rel = (RelativeLayout)findViewById(R.id.activity_song);
And
final RelativeLayout rel = (RelativeLayout)findViewById(R.id.linear_layout);
Resources res = getResources();
Drawable drawable = res.getDrawable(R.drawable.image);
rel.setBackground(drawable);
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码