我正在创建一个非常简单的音箱应用程序。我想要做的是在声音期间改变ImageView,然后放回第一张图像。事情是我不知道该怎么做。
这是我的代码:
public class SoundAdaptateur extends ArrayAdapter<Sound>{
private Sound[] items;
private Context context = null;
private MediaPlayer mp = null;
public SoundAdaptateur(Context context, int textViewResourceId, Sound[] items) {
super(context, textViewResourceId, items);
this.items = items;
this.context = context;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View row = convertView;
SoundHolder holder = null;
if (row == null) {
LayoutInflater vi = ((Activity)context).getLayoutInflater();
row = vi.inflate(R.layout.list_row, null);
final ImageView iv = (ImageView)row.findViewById(R.id.icon);
holder = new SoundHolder();
holder.imgIcon = (ImageView)row.findViewById(R.id.icon);
holder.txtDescription = (TextView)row.findViewById(R.id.description);
row.setTag(holder);
iv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(mp != null)
mp.reset();
mp = MediaPlayer.create(context, items[position].getSoundResourceId());
mp.start();
}
});
}else {
holder = (SoundHolder)convertView.getTag();
}
Sound sound = items[position];
if(sound != null){
holder.txtDescription.setText(sound.getDescription());
holder.imgIcon.setImageResource(R.drawable.muscle1);
}
return row;
}
static class SoundHolder
{
ImageView imgIcon;
TextView txtDescription;
}
}
public class MainActivity extends AppCompatActivity {
private Sound[] mSounds = null;
private SoundAdaptateur mAdapter = null;
private GridView mListView = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mListView=(GridView)findViewById(R.id.list1);
mSounds = new Sound[]{
new Sound("test 1", R.raw.test1,R.drawable.muscle1),
new Sound("test 2", R.raw.test2,R.drawable.muscle1),
new Sound("test 3", R.raw.test3,R.drawable.muscle1),
new Sound("test 4", R.raw.test4,R.drawable.muscle1),
new Sound("test 5", R.raw.test5,R.drawable.muscle1),
new Sound("test 6", R.raw.test6,R.drawable.muscle1),
new Sound("test 7", R.raw.test7,R.drawable.muscle1),
new Sound("test 8", R.raw.test8,R.drawable.muscle1),
};
mAdapter = new SoundAdaptateur(MainActivity.this, R.layout.list_row, mSounds);
mListView.setAdapter(mAdapter);
}
}
我尝试在声音开始时更改图像,然后执行一段时间循环直到声音结束并放回第一张图像但它不起作用并且它不是一个干净的代码。
这是我试过的:
v.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
iv.setImageResource(R.drawable.muscle1);
if(mp != null)
mp.reset();
mp = MediaPlayer.create(context, items[position].getSoundResourceId());
mp.start();
try {
while (mp.isPlaying()) {
wait();
}
}catch (Exception e){
}
iv.setImageResource(R.drawable.muscle);
}
});
你知道我该怎么做吗?
提前谢谢。
答案 0 :(得分:1)
改变你的形象:
iv.setImageResource(R.drawable.muscle1);
....
mp.start();
mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
//change image here
iv.setImageResource(R.drawable.muscle);
}
});