我最近开始进行android编程,这对我来说是全新的。我想在一段时间后更新声音ID和可绘制ID,但没有更新@。
我正在使用的时间:
updatetime = mediaPlayer.getDuration();
这是活动代码:
public class Alpha_Loop extends AppCompatActivity {
ImageView abc,img1;
int i = 0;
int playsound;
Runnable Rn;
int s = 0;
long updatetime;
MediaPlayer mediaPlayer;
//Array of Image1
final int[] ImageArray = {R.drawable.a, R.drawable.a, R.drawable.b, R.drawable.c, R.drawable.d, R.drawable.e,
R.drawable.f, R.drawable.g, R.drawable.h, R.drawable.i, R.drawable.j, R.drawable.k,
R.drawable.l, R.drawable.m, R.drawable.n, R.drawable.o, R.drawable.p, R.drawable.q,
R.drawable.r, R.drawable.s, R.drawable.t, R.drawable.u, R.drawable.v, R.drawable.w,
R.drawable.x, R.drawable.y, R.drawable.z};
//Array of Image1
final int[] img1Array = {R.drawable.apple, R.drawable.ball, R.drawable.cat, R.drawable.dog, R.drawable.egg,
R.drawable.fish, R.drawable.goat, R.drawable.hat, R.drawable.insect, R.drawable.jar,
R.drawable.king, R.drawable.leaf, R.drawable.monkey, R.drawable.nose, R.drawable.ocean,
R.drawable.parrot, R.drawable.queen, R.drawable.rat, R.drawable.sun, R.drawable.tap,
R.drawable.umbrella, R.drawable.violin, R.drawable.well, R.drawable.box, R.drawable.yatch,
R.drawable.zebra};
//Array of wav files
final int[] Soundid = {R.raw.a, R.raw.a, R.raw.b, R.raw.c, R.raw.d, R.raw.e,
R.raw.f, R.raw.g, R.raw.h, R.raw.i, R.raw.j, R.raw.k,
R.raw.l, R.raw.m, R.raw.n, R.raw.o, R.raw.p, R.raw.q,
R.raw.r, R.raw.s, R.raw.t, R.raw.u, R.raw.v, R.raw.w,
R.raw.x, R.raw.y, R.raw.z};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.alpha__loop);
abc = findViewById(R.id.abc);
img1 = findViewById(R.id.img1);
playsound = Soundid[0];
abc.setImageResource(R.drawable.a);
img1.setImageResource(R.drawable.apple);
mediaPlayer = MediaPlayer.create(Alpha_Loop.this, playsound);
mediaPlayer.start();
mediaPlayer.setLooping(false);
final Handler handler = new Handler();
Rn = new Runnable() {
@Override
public void run() {
abc.setImageResource(ImageArray[i]);
i++;
if (i >= ImageArray.length) {
i = 0;
}
img1.setImageResource(img1Array[i]);
i++;
if (i >= img1Array.length) {
i = 0;
}
playsound = Soundid[s];
s++;
updatetime = mediaPlayer.getDuration();
if (s >= Soundid.length) {
s = 0;
}
handler.postDelayed(Rn, updatetime);
}
};
}
}
任何帮助都会对我有所帮助。
答案 0 :(得分:0)
处理程序的postDelayed方法中经过的时间,显示了处理程序在调用可运行对象之前需要延迟多长时间(延迟-以毫秒为单位-直到将运行可运行对象为止)。因此,在代码中需要运行可运行对象的部分,请调用handler.post(Rn);
以启动您的可运行对象。
答案 1 :(得分:0)
我敢肯定,如果您这样做的话,它会起作用:
final Handler handler = new Handler();
Rn = new Runnable() {
@Override
public void run() {
abc.setImageResource(ImageArray[i]);
i++;
if (i >= ImageArray.length) {
i = 0;
}
img1.setImageResource(img1Array[i]);
i++;
if (i >= img1Array.length) {
i = 0;
}
playsound = Soundid[s];
s++;
updatetime = mediaPlayer.getDuration();
if (s >= Soundid.length) {
s = 0;
}
}
};
handler.postDelayed(Rn, updatetime);
您的处理程序正在runnable中执行postDelayed u,因此runnable从未执行。
编辑:不要使用首字母大写这样的缩写,因为它违反Java命名约定。