游戏示图
布局activity_main.xml文件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity">
<RelativeLayout
android:id="@+id/relative"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:background="#E3E3E3" >
<TextView
android:id="@+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginLeft="80dp"
android:layout_marginTop="30dp"
android:text="分数:" />
<TextView
android:id="@+id/score"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:layout_toRightOf="@id/tv1"
/>
<TextView
android:id="@+id/tv2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginRight="90dp"
android:layout_marginTop="30dp"
android:text="时间:" />
<TextView
android:id="@+id/time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/tv2"
android:layout_alignBottom="@+id/tv2"
android:layout_alignLeft="@+id/tv2"
android:layout_marginLeft="42dp"
/>
</RelativeLayout>
<RelativeLayout
android:id="@+id/gamearea"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_below="@+id/relative" >
</RelativeLayout>
</RelativeLayout>
一个描绘小圆图形的MyCircle类
package com.example.testing;
import java.util.Random;
import android.R.bool;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
public class MyCircle extends View {
private Paint paint;
private Random r;
private DisplayMetrics dm;//获取屏幕分辨率
private Context context;
private Canvas canvas;
private int width;
private int height;
private float centerX;
private float centerY;
private String tag = "SampleView";
public float getCenterX() {
return centerX;
}
public void setCenterX(float centerX) {
this.centerX = centerX;
}
public float getCenterY() {
return centerY;
}
public void setCenterY(float centerY) {
this.centerY = centerY;
}
public MyCircle(Context context) {
super(context);
this.context = context;
r = new Random();
// 创建Paint
paint = new Paint();
// 设置抗锯齿效果
paint.setAntiAlias(true);
// 设置画刷的颜色
paint.setColor(Color.BLUE);
dm = new DisplayMetrics();// DisplayMetrics()函数获取屏幕分辨率
((Activity) context).getWindowManager().getDefaultDisplay()
.getMetrics(dm);
width = getW();
height = getH();
// Log.i(tag, "width is" + width);
// Log.i(tag, "hight is" + hight);
/*
* getWindowManager()函数只能在Activity中运用或在 this.context =
* context;和((Activity) context) 下使用
*/
}
int getW() {
return dm.widthPixels;
}
int getH() {
return dm.heightPixels;
}
public void onDraw(Canvas canvas) {
// Log.i(tag, "onDraw");
do {
centerX = r.nextInt(width);
} while (centerX < 60 || centerX > width - 60);
// Log.i(tag, "new x is" + centerX);
do {
centerY = r.nextInt(height);
} while (centerY < 100 + 60 || centerY > height - 60 - 60 - 100);/*这里的加加减减是为消去屏幕非屏幕区的上边栏*/
// Log.i(tag, "new y is" + centerY);
paint.setStyle(Paint.Style.FILL); // Paint.Style.FILL用颜色填充图形
canvas.drawCircle(centerX, centerY, 60, paint); // 圆
// canvas.drawText("Gameover", width / 2, hight / 2, paint);
}
}
MainActivity文件
package com.example.testing;
import java.util.Currency;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
private RelativeLayout gameLayout;
private MyCircle gameView;
private TextView score;
private TextView time;
private Intent intent;
private String tag = "MainActivity";
private int times = 0;
private TimeCount timer;
private int drawTsOpened=1;
private Activity activity;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
score = (TextView) this.findViewById(R.id.score);
time = (TextView) this.findViewById(R.id.time);
timer = new TimeCount(10000, 1000);// 构造CountDownTimer对象
timer.start();// 开始计时
gameLayout = (RelativeLayout) findViewById(R.id.gamearea); // 将game区域当做一个id进行引用
gameView = new MyCircle(MainActivity.this);// getApplicationContext()返回应用的上下文,生命周期是整个应用,应用摧毁它才摧毁
gameLayout.addView(gameView); // 自定义一个View
score.setText(times + "");//没点击之前用于显示0次
gameLayout.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View arg0, MotionEvent arg1) {
int x1 = (int) arg1.getX();
int y1 = (int) arg1.getY();
// Log.i(tag, "touch x is" + x1);
// Log.i(tag, "touch y is" + y1);
int action = arg1.getAction();
if(drawTsOpened==1){
if (action == MotionEvent.ACTION_DOWN) {
if ((x1 - gameView.getCenterX())
* (x1 - gameView.getCenterX())
+ (y1 - gameView.getCenterY())
* (y1 - gameView.getCenterY()) < 3600) {//半径设为60
gameView.postInvalidate();// 调用重绘函数postInvalidate()
times++;
score.setText(times + "");
}
}
}
return false;
}
});
}
/* 定义一个倒计时的内部类 */
class TimeCount extends CountDownTimer {
public TimeCount(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
public void onFinish() {// 计时完毕时触发
dialog_timeisover();
drawTsOpened=0;
}
public void onTick(long millisUntilFinished) {
time.setText(millisUntilFinished / 1000 + "秒");
}
}
private void dialog_timeisover() {//定义对话框函数
AlertDialog.Builder dialog = new AlertDialog.Builder(MainActivity.this);
dialog.setTitle("提示").setIcon(android.R.drawable.ic_dialog_info)
.setMessage("本次怒点" + times + "次,是否退出或继续")
.setPositiveButton("退出游戏", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
exit_apk.getInstance().exit();
}
})
.setNegativeButton("继续挑战", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
MainActivity.this.onResume();
timer.start();// 开始计时
times=0;
score.setText(times+"");
}
}).create().show();
}
}
大功告成!!!