`

弹钢琴的一个简单程序(UI很重要)

阅读更多
效果图:




package com.example;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ImageButton;
import android.widget.Toast;
import android.view.View;
import android.view.MotionEvent;
import android.media.MediaPlayer;

/**
 * @author: ZhangFL
 */
public class Piano extends Activity {

    private ImageButton imageButton_white1;
    private ImageButton imageButton_white2;
    private ImageButton imageButton_white3;
    private ImageButton imageButton_white4;
    private ImageButton imageButton_white5;
    private ImageButton imageButton_white6;
    private ImageButton imageButton_white7;
    private ImageButton imageButton_white8;

    private ImageButton imageButton_black1;
    private ImageButton imageButton_black2;
    private ImageButton imageButton_black3;
    private ImageButton imageButton_black4;
    private ImageButton imageButton_black5;
    private MediaPlayer mediaPlayer01;

    @Override
    protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setContentView(R.layout.main);

        mediaPlayer01 = new MediaPlayer();
//        mediaPlayer01 = MediaPlayer.create(Piano.this, R.raw.white1);

        imageButton_white1 = (ImageButton) findViewById(R.id.white1);
        imageButton_white2 = (ImageButton) findViewById(R.id.white2);
        imageButton_white3 = (ImageButton) findViewById(R.id.white3);
        imageButton_white4 = (ImageButton) findViewById(R.id.white4);
        imageButton_white5 = (ImageButton) findViewById(R.id.white5);
        imageButton_white6 = (ImageButton) findViewById(R.id.white6);
        imageButton_white7 = (ImageButton) findViewById(R.id.white7);
        imageButton_white8 = (ImageButton) findViewById(R.id.white8);

        imageButton_black1 = (ImageButton) findViewById(R.id.black1);
        imageButton_black2 = (ImageButton) findViewById(R.id.black2);
        imageButton_black3 = (ImageButton) findViewById(R.id.black3);
        imageButton_black4 = (ImageButton) findViewById(R.id.black4);
        imageButton_black5 = (ImageButton) findViewById(R.id.black5);

        imageButton_white1.setOnTouchListener(new View.OnTouchListener() {
            public boolean onTouch(View view, MotionEvent motionEvent) {

                if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
                    play(R.raw.white1);
                    imageButton_white1.setImageResource(R.drawable.whiteback1);
                }
                if (motionEvent.getAction() == MotionEvent.ACTION_UP) {
                    imageButton_white1.setImageResource(R.drawable.white1);
                }
                return false;
            }
        });


        imageButton_white2.setOnTouchListener(new View.OnTouchListener() {
            public boolean onTouch(View view, MotionEvent motionEvent) {
                if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {

                    play(R.raw.white2);
                    imageButton_white2.setImageResource(R.drawable.whiteback2);
                }
                if (motionEvent.getAction() == MotionEvent.ACTION_UP) {
                    imageButton_white2.setImageResource(R.drawable.white2);
                }
                return false;
            }
        });
//
        imageButton_white3.setOnTouchListener(new View.OnTouchListener() {
            public boolean onTouch(View view, MotionEvent motionEvent) {
                if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
                    play(R.raw.white3);
                    imageButton_white3.setImageResource(R.drawable.whiteback3);
                }
                if (motionEvent.getAction() == MotionEvent.ACTION_UP) {
                    imageButton_white3.setImageResource(R.drawable.white3);
                }
                return false;
            }
        });

        imageButton_white4.setOnTouchListener(new View.OnTouchListener() {
            public boolean onTouch(View view, MotionEvent motionEvent) {
                if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
                    play(R.raw.white4);
                    imageButton_white4.setImageResource(R.drawable.whiteback4);
                }
                if (motionEvent.getAction() == MotionEvent.ACTION_UP) {
                    imageButton_white4.setImageResource(R.drawable.white4);
                }
                return false;
            }
        });

        imageButton_white5.setOnTouchListener(new View.OnTouchListener() {
            public boolean onTouch(View view, MotionEvent motionEvent) {
                if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
                    play(R.raw.white5);
                    imageButton_white5.setImageResource(R.drawable.whiteback5);
                }
                if (motionEvent.getAction() == MotionEvent.ACTION_UP) {
                    imageButton_white5.setImageResource(R.drawable.white5);
                }
                return false;
            }
        });

        imageButton_white6.setOnTouchListener(new View.OnTouchListener() {
            public boolean onTouch(View view, MotionEvent motionEvent) {
                if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
                    play(R.raw.white6);
                    imageButton_white6.setImageResource(R.drawable.whiteback6);
                }
                if (motionEvent.getAction() == MotionEvent.ACTION_UP) {
                    imageButton_white6.setImageResource(R.drawable.white6);
                }
                return false;
            }
        });

        imageButton_white7.setOnTouchListener(new View.OnTouchListener() {
            public boolean onTouch(View view, MotionEvent motionEvent) {
                if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
                    play(R.raw.white7);
                    imageButton_white7.setImageResource(R.drawable.whiteback7);
                }
                if (motionEvent.getAction() == MotionEvent.ACTION_UP) {
                    imageButton_white7.setImageResource(R.drawable.white7);
                }
                return false;
            }
        });

        imageButton_white8.setOnTouchListener(new View.OnTouchListener() {
            public boolean onTouch(View view, MotionEvent motionEvent) {
                if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
                    play(R.raw.white8);
                    imageButton_white8.setImageResource(R.drawable.whiteback8);
                }
                if (motionEvent.getAction() == MotionEvent.ACTION_UP) {
                    imageButton_white8.setImageResource(R.drawable.white8);
                }
                return false;
            }
        });

        imageButton_black1.setOnTouchListener(new View.OnTouchListener() {
            public boolean onTouch(View view, MotionEvent motionEvent) {
                if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
                    play(R.raw.black1);
                }
                if (motionEvent.getAction() == MotionEvent.ACTION_UP) {
                    imageButton_black1.setImageResource(R.drawable.black1);
                }
                return false;
            }
        });

        imageButton_black2.setOnTouchListener(new View.OnTouchListener() {
            public boolean onTouch(View view, MotionEvent motionEvent) {
                if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
                    play(R.raw.black2);
                }
                if (motionEvent.getAction() == MotionEvent.ACTION_UP) {
                    imageButton_black2.setImageResource(R.drawable.black2);
                }
                return false;
            }
        });

        imageButton_black3.setOnTouchListener(new View.OnTouchListener() {
            public boolean onTouch(View view, MotionEvent motionEvent) {
                if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
                    play(R.raw.black3);
                }
                if (motionEvent.getAction() == MotionEvent.ACTION_UP) {
                    imageButton_black3.setImageResource(R.drawable.black3);
                }
                return false;
            }
        });

        imageButton_black4.setOnTouchListener(new View.OnTouchListener() {
            public boolean onTouch(View view, MotionEvent motionEvent) {
                if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
                    play(R.raw.black4);
                }
                if (motionEvent.getAction() == MotionEvent.ACTION_UP) {
                    imageButton_black4.setImageResource(R.drawable.black4);
                }
                return false;
            }
        });

        imageButton_black5.setOnTouchListener(new View.OnTouchListener() {
            public boolean onTouch(View view, MotionEvent motionEvent) {
                if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
                    play(R.raw.black5);
                }
                if (motionEvent.getAction() == MotionEvent.ACTION_UP) {
                    imageButton_black5.setImageResource(R.drawable.black5);
                }
                return false;
            }
        });
//
//
        mediaPlayer01.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            public void onCompletion(MediaPlayer arg0) {
                mediaPlayer01.release();
                mediaPlayer01 = null;
                Toast.makeText(Piano.this, "资源释放了!", Toast.LENGTH_SHORT).show();
            }
        });

        mediaPlayer01.setOnErrorListener(new MediaPlayer.OnErrorListener() {
            public boolean onError(MediaPlayer arg0, int i, int i1) {
                try {
                    mediaPlayer01.release();
                    Toast.makeText(Piano.this, "发生错误了!", Toast.LENGTH_SHORT).show();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return false;
            }
        });


    }

    //-------------------------------------------------------------------------------------
    private void play(int resource) {
        try {

            mediaPlayer01.release();
            mediaPlayer01 = MediaPlayer.create(Piano.this, resource);
            mediaPlayer01.start();
        } catch (Exception e) {
            Toast.makeText(Piano.this, "发生错误了:" + e.getMessage(), Toast.LENGTH_SHORT).show();
        }
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mediaPlayer01 != null) {
            mediaPlayer01.release();
            mediaPlayer01 = null;
        }

    }

}



声音文件我附上了:)
  • 大小: 54.6 KB
  • raw.rar (83.5 KB)
  • 下载次数: 74
分享到:
评论
11 楼 xudongjhdd 2011-07-31  
求一份 每一个按键的图片文件 及布局文件 最好给份学有源代码 
99799543@qq.com  thanks
10 楼 邱顯鈞 2011-05-26  
想請問一下介面的配置方法是如何配置的?! R.java又該如何修改?!
teddychiu79@yahoo.com.tw
thanks
9 楼 bzhao 2011-03-31  
bzhao 写道
露个脸!给个声音文件吧!

szbzhao@gmail.com
8 楼 bzhao 2011-03-31  
露个脸!给个声音文件吧!
7 楼 bzhao 2011-03-28  
ok, give the sound files, thanks for your sharing!
szbzhao@gmail.com
6 楼 Teok 2010-12-10  
单从代码的角度来看,你可以尝试改进一下,代码很冗余
5 楼 dwq1988 2010-10-11  
求一份声音文件,多谢楼主。
dongwenjjj@qq.com
4 楼 384444165 2010-10-05  
请教一下这个是否还需要图片呢,只有那个图片就可以了吗。另外求一下音乐。希望能把工程压缩一下发一下,非常感谢了,邮箱:vb.wbw@hotmail.com
3 楼 linpeng 2010-09-27  
我需要声音文件。感谢楼主!
EMAIL:497005910@qq.com
2 楼 crazyman1314 2010-08-19  
感谢楼主.EMAIL:index.crazyman@gmail.com
1 楼 aduo_vip 2010-08-16  
了解一下,lz说得很对,在Android中用户界面始终是第一位的,其次才是数据库设计,实现,程序页面的跳转,细节完善等,不知你这个程序是怎么个用法,wgh290424389@163.com

相关推荐

Global site tag (gtag.js) - Google Analytics