要求:有背景音乐,按钮切换图片并带图片说明
效果:

实现:
创建raw目录,将音频文件放入raw目录

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/img1"
android:gravity="center"
android:orientation="vertical"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:id="@+id/btnLast"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="上一张"
android:textSize="20sp" />
<Button
android:id="@+id/btnNext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:text="下一张"
android:textSize="20sp" />
</LinearLayout>
<TextView
android:id="@+id/tvCharacter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:text="图片1"
android:textSize="30dp"
android:textColor="#00FF00"/>
</LinearLayout>
ActivityMain.java
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private int[] imgIds;//图像资源标识符
private int imgIndex;//图像索引,表示在图像资源标识符数组中的位置
private LinearLayout root;//线性根布局
private MediaPlayer mediaParser;//媒体播放器
private static final int IMG_COUNT = 4;//图像总数
private Button btnLast, btnNext; //按钮控件
private List<String> ls = new ArrayList<String>();
private TextView tvCharacter;
private int index;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//利用布局资源文件设置用户界面
setContentView(R.layout.activity_main);
//通过资源标识符获取控件实例
root = findViewById(R.id.root);
btnLast = findViewById(R.id.btnLast);
btnNext = findViewById(R.id.btnNext);
tvCharacter = findViewById(R.id.tvCharacter);
//添加图片说明
ls.add(0,"图片1");
ls.add(1,"图片2");
ls.add(2,"图片3");
ls.add(3,"图片4");
//音乐播放
if (mediaParser == null) {
//创建媒体播放器,指定播放源
mediaParser = MediaPlayer.create(MainActivity.this, R.raw.music);
//设置循环播放方式
mediaParser.setLooping(true);
//播放指定音频文件
mediaParser.start();
}
//初始化图像标识符
imgIds = new int[IMG_COUNT];
for (int i = 0; i < IMG_COUNT; i++) {
imgIds[i] = getResources().getIdentifier(
"img" + (i + 1),//标识符名称
"drawable",//定义类型
"net.lj.personal_images"//定义包名
);
}
//上一张背景图片按钮
btnLast.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (imgIndex >0) {
index=imgIndex--;
tvCharacter.setText(ls.get(index-1));
} else {
imgIndex = IMG_COUNT - 1;
tvCharacter.setText(ls.get(imgIndex));
}
//刷新背景图片
root.setBackgroundResource(imgIds[imgIndex]);
}
});
//下一张背景图按钮
btnNext.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (imgIndex < IMG_COUNT - 1) {
index = imgIndex++;
tvCharacter.setText(ls.get(index+1));
} else {
imgIndex = 0;
tvCharacter.setText(ls.get(0));
}
//刷新背景图片
root.setBackgroundResource(imgIds[imgIndex]);
}
});
}
}
865



被折叠的 条评论
为什么被折叠?



