求大神帮我代码大概注释一下

求大神帮我代码大概注释一下

问题描述:

public class MainActivity extends Activity {
private int i = 0;
private Handler handler;
private ImageView mouse;
private TextView textView ;
private ImageView integral; //整体图片
private Chronometer ch ; //计时器
private SoundPool pool; //播放音效
private static MediaPlayer mp = null;
private HashMap soundmap = new HashMap();
public int[][] position = new int[][] { { 150, 100 }, { 250, 100 }, //地鼠出现的位置
{ 350, 100 }, { 450, 100 }, { 550, 100 }, { 650, 100 },
{ 50, 100 }, { 150, 220 }, { 250, 220 }, { 350, 510 },
{ 450, 220 }, { 550, 220 }, { 650, 220 }, { 50, 220 } };

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //音乐
    //如果音乐不为空,释放
    if(mp != null){
        mp.release();
    }
    mp = MediaPlayer.create(MainActivity.this, R.raw.dalaoshu);
    mp.start();

    //计时器
    ch = (Chronometer) findViewById(R.id.chronometer1);
    ch.setBase(SystemClock.elapsedRealtime());//复位计时器,停止计时
    ch.setFormat("已用时间:%s");
    ch.start();
    //设置监听
    ch.setOnChronometerTickListener(new OnChronometerTickListener() {

        @Override
        public void onChronometerTick(Chronometer chronometer) {
            //如果从开始到现在超过了30s
            if(SystemClock.elapsedRealtime() - ch.getBase() >= 30000){
                Intent intent = new Intent(MainActivity.this,Finish.class);
                Bundle bundle = new Bundle();
                bundle.putInt("num", i);
                intent.putExtras(bundle);
                startActivity(intent);
                finish();
            }
        }
    });

    textView = (TextView) findViewById(R.id.textView1);
    mouse = (ImageView) findViewById(R.id.imageView1);
    integral=(ImageView) findViewById(R.id.imageView2);
    integral.setVisibility(View.INVISIBLE);
    pool = new SoundPool(2, AudioManager.STREAM_SYSTEM, 0);
    soundmap.put(1, pool.load(MainActivity.this, R.raw.dalaoshu,1));
    soundmap.put(2, pool.load(MainActivity.this, R.raw.enter,1));
    pool.play(soundmap.get(1), 1, 1, 0, -1, 1);
    final Animation translate = AnimationUtils.loadAnimation(MainActivity.this, R.anim.anim_translate);
    mouse.setOnTouchListener(new OnTouchListener() {

        @Override
        public  boolean onTouch(View v, MotionEvent event) {
            v.setVisibility(View.INVISIBLE);// 设置地老鼠不显示
            i++;
            pool.play(soundmap.get(2), 1, 1, 0, 0, 1);
            integral.startAnimation(translate);
            integral.setVisibility(View.INVISIBLE);
            textView.setText("积分:"+i+"0");
            return false;
        }
    });
    handler = new Handler(){

        @Override
        public void handleMessage(Message msg) {
            int index = 0;
            if(msg.what == 0x111){
                index = msg.arg1;
                mouse.setX(position[index][0]);
                mouse.setY(position[index][1]);
                mouse.setVisibility(View.VISIBLE);

            }
            super.handleMessage(msg);
        }
    };
    Thread t = new Thread(new Runnable() {

        @Override
        public void run() {
            int index = 0;
            while(!Thread.currentThread().isInterrupted()){
                index = new Random().nextInt(position.length);
                Message m = handler.obtainMessage();
                m.what = 0x111;
                m.arg1 = index;
                handler.sendMessage(m);             
                try {
                    Thread.sleep(new Random().nextInt(240)+200);

                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

        }
    });
    t.start();
}
@Override
protected void onDestroy() {
    if(mp != null){
        mp.stop();
        mp.release();
        mp=null;
    }
    super.onDestroy();
}

}

textView = (TextView) findViewById(R.id.textView1); 文本
mouse = (ImageView) findViewById(R.id.imageView1); 老鼠
integral=(ImageView) findViewById(R.id.imageView2); 积分
integral.setVisibility(View.INVISIBLE); 设置积分不可见
pool = new SoundPool(2, AudioManager.STREAM_SYSTEM, 0); 声音
soundmap.put(1, pool.load(MainActivity.this, R.raw.dalaoshu,1)); 放入大老鼠
soundmap.put(2, pool.load(MainActivity.this, R.raw.enter,1));放入进入
pool.play(soundmap.get(1), 1, 1, 0, -1, 1); 获取声音
final Animation translate = AnimationUtils.loadAnimation(MainActivity.this, R.anim.anim_translate); 动画
mouse.setOnTouchListener(new OnTouchListener() { 触摸事件

@Override 重写
public  boolean onTouch(View v, MotionEvent event) { 触摸的时候触发
    v.setVisibility(View.INVISIBLE);// 设置地老鼠不显示 
    i++; 变量+1
    pool.play(soundmap.get(2), 1, 1, 0, 0, 1); 播放
    integral.startAnimation(translate); 开始动画
    integral.setVisibility(View.INVISIBLE); 设置不可见
    textView.setText("积分:"+i+"0"); 显示积分
    return false; 返回
}

});
handler = new Handler(){ 定义新的handler

@Override
public void handleMessage(Message msg) {
    int index = 0;
    if(msg.what == 0x111){
        index = msg.arg1;
        mouse.setX(position[index][0]);
        mouse.setY(position[index][1]);
        mouse.setVisibility(View.VISIBLE);

    }
    super.handleMessage(msg);
}

};
Thread t = new Thread(new Runnable() {

@Override
public void run() {
    int index = 0;
    while(!Thread.currentThread().isInterrupted()){
        index = new Random().nextInt(position.length);
        Message m = handler.obtainMessage();
        m.what = 0x111;
        m.arg1 = index;
        handler.sendMessage(m);             
        try {
            Thread.sleep(new Random().nextInt(240)+200);

        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

}

});
t.start();

public class MainActivity extends Activity {
private int i = 0;
private Handler handler;
private ImageView mouse; //老鼠
private TextView textView ; //文字 不知道是什么文字
private ImageView integral; //整体图片
private Chronometer ch ; //计时器
private SoundPool pool; //播放音效
private static MediaPlayer mp = null; //初始化 音乐播放器
private HashMap soundmap = new HashMap(); //用hashmap 装出现过的声音
public int[][] position = new int[][] { { 150, 100 }, { 250, 100 }, //地鼠出现的位置

{ 350, 100 }, { 450, 100 }, { 550, 100 }, { 650, 100 },
{ 50, 100 }, { 150, 220 }, { 250, 220 }, { 350, 510 },
{ 450, 220 }, { 550, 220 }, { 650, 220 }, { 50, 220 } };
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); //布局文件

//音乐
//如果音乐不为空,释放
if(mp != null){
    mp.release();
}
mp = MediaPlayer.create(MainActivity.this, R.raw.dalaoshu);//设置背景音乐
mp.start();//开始播放

//计时器
ch = (Chronometer) findViewById(R.id.chronometer1);
ch.setBase(SystemClock.elapsedRealtime());//复位计时器,停止计时
ch.setFormat("已用时间:%s");
ch.start();
//设置监听
ch.setOnChronometerTickListener(new OnChronometerTickListener() {

    @Override
    public void onChronometerTick(Chronometer chronometer) {
        //如果从开始到现在超过了30s
        if(SystemClock.elapsedRealtime() - ch.getBase() >= 30000){
            Intent intent = new Intent(MainActivity.this,Finish.class);//页面的跳转
            Bundle bundle = new Bundle();//使用bundle来传参数
            bundle.putInt("num", i);
            intent.putExtras(bundle);
            startActivity(intent);
            finish();//跳转关闭当前页面
        }
    }
});

textView = (TextView) findViewById(R.id.textView1);//找ID
mouse = (ImageView) findViewById(R.id.imageView1);//找ID
integral=(ImageView) findViewById(R.id.imageView2);//找ID
integral.setVisibility(View.INVISIBLE);//设置 图片不可见
pool = new SoundPool(2, AudioManager.STREAM_SYSTEM, 0);
soundmap.put(1, pool.load(MainActivity.this, R.raw.dalaoshu,1));
soundmap.put(2, pool.load(MainActivity.this, R.raw.enter,1));
pool.play(soundmap.get(1), 1, 1, 0, -1, 1);//设置播放什么背景声音
final Animation translate = AnimationUtils.loadAnimation(MainActivity.this, R.anim.anim_translate);
mouse.setOnTouchListener(new OnTouchListener() {

    @Override
    public  boolean onTouch(View v, MotionEvent event) {
        v.setVisibility(View.INVISIBLE);// 设置地老鼠不显示
        i++;
        pool.play(soundmap.get(2), 1, 1, 0, 0, 1);
        integral.startAnimation(translate);
        integral.setVisibility(View.INVISIBLE);
        textView.setText("积分:"+i+"0");
        return false;
    }
});

public class MainActivity extends Activity { 主界面,继承
private int i = 0; 定义变量
private Handler handler; 定义消息处理
private ImageView mouse; 定义老鼠
private TextView textView ; 显示文本
private ImageView integral; //整体图片
private Chronometer ch ; //计时器
private SoundPool pool; //播放音效
private static MediaPlayer mp = null 播放音乐的播放器
private HashMap soundmap = new HashMap(); 哈希表
public int[][] position = new int[][] { { 150, 100 }, { 250, 100 }, //地鼠出现的位置
{ 350, 100 }, { 450, 100 }, { 550, 100 }, { 650, 100 }, 位置数组
{ 50, 100 }, { 150, 220 }, { 250, 220 }, { 350, 510 }, 位置数组
{ 450, 220 }, { 550, 220 }, { 650, 220 }, { 50, 220 } }; 位置数组
@Override 重写
protected void onCreate(Bundle savedInstanceState) { 创建
super.onCreate(savedInstanceState); 调用基类
setContentView(R.layout.activity_main); 设置视图

//音乐
//如果音乐不为空,释放
if(mp != null){ 判断是否为null
    mp.release(); 释放
}
mp = MediaPlayer.create(MainActivity.this, R.raw.dalaoshu); 创建播放,打老鼠资源
mp.start(); 开始播放

//计时器
ch = (Chronometer) findViewById(R.id.chronometer1); 创建
ch.setBase(SystemClock.elapsedRealtime());//复位计时器,停止计时
ch.setFormat("已用时间:%s"); 格式化字符
ch.start(); 开始
//设置监听
ch.setOnChronometerTickListener(new OnChronometerTickListener() { 调用监听器

    @Override 重写
    public void onChronometerTick(Chronometer chronometer) { 定时器
        //如果从开始到现在超过了30s
        if(SystemClock.elapsedRealtime() - ch.getBase() >= 30000){ 判断时间是否结束
            Intent intent = new Intent(MainActivity.this,Finish.class); 创建intent
            Bundle bundle = new Bundle();创建boudle
            bundle.putInt("num", i); 输出
            intent.putExtras(bundle); 插入
            startActivity(intent); 设置intent
            finish(); 结束
        }
    }
});

textView = (TextView) findViewById(R.id.textView1);
mouse = (ImageView) findViewById(R.id.imageView1);
integral=(ImageView) findViewById(R.id.imageView2);
integral.setVisibility(View.INVISIBLE);
pool = new SoundPool(2, AudioManager.STREAM_SYSTEM, 0);
soundmap.put(1, pool.load(MainActivity.this, R.raw.dalaoshu,1));
soundmap.put(2, pool.load(MainActivity.this, R.raw.enter,1));
pool.play(soundmap.get(1), 1, 1, 0, -1, 1);
final Animation translate = AnimationUtils.loadAnimation(MainActivity.this, R.anim.anim_translate);
mouse.setOnTouchListener(new OnTouchListener() {

    @Override
    public  boolean onTouch(View v, MotionEvent event) {
        v.setVisibility(View.INVISIBLE);// 设置地老鼠不显示
        i++;
        pool.play(soundmap.get(2), 1, 1, 0, 0, 1);
        integral.startAnimation(translate);
        integral.setVisibility(View.INVISIBLE);
        textView.setText("积分:"+i+"0");
        return false;
    }
});
handler = new Handler(){

    @Override
    public void handleMessage(Message msg) {
        int index = 0;
        if(msg.what == 0x111){
            index = msg.arg1;
            mouse.setX(position[index][0]);
            mouse.setY(position[index][1]);
            mouse.setVisibility(View.VISIBLE);

        }
        super.handleMessage(msg);
    }
};
Thread t = new Thread(new Runnable() {

    @Override
    public void run() {
        int index = 0;
        while(!Thread.currentThread().isInterrupted()){
            index = new Random().nextInt(position.length);
            Message m = handler.obtainMessage();
            m.what = 0x111;
            m.arg1 = index;
            handler.sendMessage(m);             
            try {
                Thread.sleep(new Random().nextInt(240)+200);

            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

    }
});
t.start();

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

其余代码采纳了回答再写给你哦

@Override
public void handleMessage(Message msg) { 处理消息
    int index = 0;
    if(msg.what == 0x111){ 如果消息是0x111
        index = msg.arg1; 获得索引
        mouse.setX(position[index][0]); 设置老鼠位置x
        mouse.setY(position[index][1]); 设置老鼠位置y
        mouse.setVisibility(View.VISIBLE); 让老鼠出现

    }
    super.handleMessage(msg); 其它消息让基类处理
}

};
Thread t = new Thread(new Runnable() { 开一个线程

@Override
public void run() { 运行线程
    int index = 0; 索引为0
    while(!Thread.currentThread().isInterrupted()){ 判断线程是否中断
        index = new Random().nextInt(position.length); 产生一个随机数
        Message m = handler.obtainMessage(); 获取消息
        m.what = 0x111; 消息id=0x111
        m.arg1 = index; 
        handler.sendMessage(m);             发送消息
        try {
            Thread.sleep(new Random().nextInt(240)+200); 随机延迟,使得老鼠出现的时间不确定

        } catch (InterruptedException e) { 异常处理
            e.printStackTrace();输出异常
        }
    }

}

});
t.start(); 运行
}
@Override
protected void onDestroy() { 退出程序销毁
if(mp != null){
mp.stop(); 停止mp
mp.release(); 释放
mp=null;
}
super.onDestroy() 调用基类;
}
}