`
冰绝皇妃
  • 浏览: 2216 次
  • 性别: Icon_minigender_2
最近访客 更多访客>>
文章分类
社区版块
存档分类
最新评论
阅读更多
利用线性布局进行布局,添加<ImageView>并对其设置拉伸,不然在不同API的分辨率下在图片视图下会有缝隙。设置每一个<ImageView>的id。在Activity.java添加如下代码:
private ImageView image_white1;
private ImageView image_white2;
private ImageView image_white3;
private ImageView image_white4;
private ImageView image_white5;
private ImageView image_white6;
private ImageView image_white7;
private ImageView image_white8;
private SoundPool soundpool;

public HashMap<Integer,Integer> soundmap = new HashMap<Integer, Integer>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        soundpool=new SoundPool(10, AudioManager.STREAM_MUSIC, 100);
image_white1 = (ImageView) findViewById(R.id.mPanoClickWhiteOne);
image_white2 = (ImageView) findViewById(R.id.mPanoClickWhiteTwo);
image_white3 = (ImageView) findViewById(R.id.mPanoClickWhiteThree);
image_white4 = (ImageView) findViewById(R.id.mPanoClickWhiteFour);
image_white5 = (ImageView) findViewById(R.id.mPanoClickWhiteFive);
image_white6 = (ImageView) findViewById(R.id.mPanoClickWhiteSix);
image_white7 = (ImageView) findViewById(R.id.mPanoClickWhiteSeven);
image_white8 = (ImageView) findViewById(R.id.mPanoClickWhiteEight);

       // 调用SoundPool对象的load()的方法加载声音,这里用了HashMap
soundmap.put(1, soundpool.load(this, R.raw.white1, 1));
soundmap.put(2, soundpool.load(this, R.raw.white2, 2));
soundmap.put(3, soundpool.load(this, R.raw.white3, 3));
soundmap.put(4, soundpool.load(this, R.raw.white4, 4));
soundmap.put(5, soundpool.load(this, R.raw.white5, 5));
soundmap.put(6, soundpool.load(this, R.raw.white6, 6));
soundmap.put(7, soundpool.load(this, R.raw.white7, 7));
soundmap.put(8, soundpool.load(this, R.raw.white8,);

        image_white1.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN){
soundpool.play(soundmap.get(1), 1, 1, 0, 0, 1);// 播放第一个琴键对应的音频
image_white1.setBackgroundColor(0x33969696);
}else
if(event.getAction()==MotionEvent.ACTION_UP){
image_white1.setBackgroundColor(0x00ffffff);
}
return true;
}
});
        image_white2.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN){
soundpool.play(soundmap.get(2), 1, 1, 0, 0, 1);// 播放第一个琴键对应的音频
image_white2.setBackgroundColor(0x33969696);
}else
if(event.getAction()==MotionEvent.ACTION_UP){
image_white2.setBackgroundColor(0x00ffffff);
}
return true;
}
});
        image_white3.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN){
soundpool.play(soundmap.get(3), 1, 1, 0, 0, 1);// 播放第一个琴键对应的音频
image_white3.setBackgroundColor(0x33969696);
}else
if(event.getAction()==MotionEvent.ACTION_UP){
image_white3.setBackgroundColor(0x00ffffff);
}
return true;
}
});
        image_white4.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN){
soundpool.play(soundmap.get(4), 1, 1, 0, 0, 1);// 播放第一个琴键对应的音频
image_white4.setBackgroundColor(0x33969696);
}else
if(event.getAction()==MotionEvent.ACTION_UP){
image_white4.setBackgroundColor(0x00ffffff);
}
return true;
}
});
        image_white5.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN){
soundpool.play(soundmap.get(5), 1, 1, 0, 0, 1);// 播放第一个琴键对应的音频
image_white5.setBackgroundColor(0x33969696);
}else
if(event.getAction()==MotionEvent.ACTION_UP){
image_white5.setBackgroundColor(0x00ffffff);
}
return true;
}
});
        image_white6.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN){
soundpool.play(soundmap.get(6), 1, 1, 0, 0, 1);// 播放第一个琴键对应的音频
image_white6.setBackgroundColor(0x33969696);
}else
if(event.getAction()==MotionEvent.ACTION_UP){
image_white6.setBackgroundColor(0x00ffffff);
}
return true;
}
});
        image_white7.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN){
soundpool.play(soundmap.get(7), 1, 1, 0, 0, 1);// 播放第一个琴键对应的音频
image_white7.setBackgroundColor(0x33969696);
}else
if(event.getAction()==MotionEvent.ACTION_UP){
image_white7.setBackgroundColor(0x00ffffff);
}
return true;
}
});
        image_white8.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN){
soundpool.play(soundmap.get(8), 1, 1, 0, 0, 1);// 播放第一个琴键对应的音频
image_white8.setBackgroundColor(0x33969696);
}else
if(event.getAction()==MotionEvent.ACTION_UP){
image_white8.setBackgroundColor(0x00ffffff);
}
return true;
}
});
SoundPool主要用于播放一些较短的声音片段,与MediaPlayer相比,SoundPool的优势在于CPU资源占用量低和反应延迟小。另外,SoundPool还支持自行设置声音的品质、音量、 播放比率等参数。

Soundpool的具体用法自己上网查询就会有详细的写法
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics