讲到图形处理,给学生做的一个小游戏案例!
井字棋
MainActivity
package com.example.three_chess;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TestView tView=new TestView(this);
setContentView(tView);
//CwjView tView2=new CwjView(this,null);
//setContentView(tView2);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
TestView
package com.example.three_chess;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Point;
import android.graphics.Rect;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Toast;
public class TestView extends View
{
private List
private List
int chess[][]=new int[3][3];
private boolean isWhite = true; //判断是否是白棋先手,或当前为白棋下子
private Bitmap Blackpic = null;
private Bitmap Whitepic = null;
private Context mycontext = null;
public TestView(Context context)
{
super(context);
mycontext=context;
Blackpic=BitmapFactory.decodeResource(getResources(), R.drawable.stone_b1);
Whitepic=BitmapFactory.decodeResource(getResources(), R.drawable.stone_w2);
}
/*重写onDraw()*/
@Override
protected void onDraw(Canvas canvas)
{
/*设置背景为青色*/
canvas.drawColor(Color.CYAN);
Paint paint=new Paint();
/*设置画笔宽度*/
paint.setStrokeWidth(3);
canvas.drawLine(0, 0, 300, 0, paint);
canvas.drawLine(0, 100, 300, 100, paint);
canvas.drawLine(0, 200, 300, 200, paint);
canvas.drawLine(0, 300, 300, 300, paint);
canvas.drawLine(0, 0, 0, 300, paint);
canvas.drawLine(100, 0, 100, 300, paint);
canvas.drawLine(200, 0 , 200, 300, paint);
canvas.drawLine(300, 0 , 300, 300, paint);
paint.setStyle(Paint.Style.FILL);
//canvas.drawCircle(50,50, 50, paint);
drawPiece(canvas);
}
//画棋子
private void drawPiece(Canvas canvas) {
int n1 = myWhiteArray.size();
int n2 = myBlackArray.size();
Paint paint1=new Paint();
/*设置画笔宽度*/
paint1.setStrokeWidth(3);
paint1.setColor(Color.WHITE);
for(int i =0; i< n1 ;i++){
Point whitePoint = myWhiteArray.get(i);
canvas.drawCircle(50+100*(whitePoint.x),50+100*(whitePoint.y), 50, paint1);
//canvas.drawBitmap(Bitmap bitmap, float left, float top, Paint paint);Bitmap:图片对象,left:偏移左边的位置,top: 偏移顶部的位置
// canvas.drawBitmap(Whitepic, 100*(whitePoint.x),100*(whitePoint.y),paint1);
}
Paint paint2=new Paint();
/*设置画笔宽度*/
paint2.setStrokeWidth(3);
paint2.setColor(Color.BLACK);
for(int i =0; i< n2 ;i++){
Point blackPoint = myBlackArray.get(i);
canvas.drawCircle(50+100*(blackPoint.x),50+100*(blackPoint.y), 50, paint2);
// 指定图片绘制区域
Rect src2 = new Rect(0,0,Blackpic.getWidth(),Blackpic.getHeight());
Rect dst2 = new Rect(100*(blackPoint.x),100*(blackPoint.y),100*(blackPoint.x)+100,100*(blackPoint.y)+100);
canvas.drawBitmap(Blackpic,src2, dst2, null);
System.out.print(Blackpic.getWidth());
}
}
protected void onDraw2(Canvas canvas)
{
/*设置背景为青色*/
canvas.drawColor(Color.CYAN);
Paint paint=new Paint();
/*设置画笔宽度*/
paint.setStrokeWidth(3);
/*设置画空心图形*/
paint.setStyle(Paint.Style.STROKE);
/*去锯齿*/
paint.setAntiAlias(true);
/*画空心矩形(正方形)*/
canvas.drawRect(10,10,70,70,paint);
/*设置画实心图形*/
paint.setStyle(Paint.Style.FILL);
/*画实心矩形(正方形)*/
canvas.drawRect(100,10,170,70,paint);
/*设置画笔颜色为蓝色*/
paint.setColor(Color.BLUE);
/*画圆心为(100,120),半径为30的实心圆*/
canvas.drawCircle(100,120,30,paint);
/*在上面的实心圆上画一个小白点*/
paint.setColor(Color.WHITE);
canvas.drawCircle(91,111,6,paint);
/*设置画笔颜色为红色*/
paint.setColor(Color.RED);
/*画三角形*/
Path path=new Path();
path.moveTo(100, 170);
path.lineTo(70, 230);
path.lineTo(130,230);
path.close();
canvas.drawPath(path,paint);
/*文字*/
paint.setTextSize(28);
paint.setColor(Color.BLUE);
canvas.drawText(getResources().getString(R.string. hello_world),
30,270,paint);
}
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
//Touch事件的相关细节(发生触摸的位置、时间等)被封装成MotionEvent对象
int x1,y1;
if (event.getAction() == MotionEvent.ACTION_DOWN)
{
x1 = (int) (event.getX()/100);
y1 = (int) (event.getY()/100);
Toast.makeText(mycontext,"单击了"+x1+","+y1,Toast.LENGTH_SHORT ).show();
//Point p = getVaLidPiont( (int) event.getX(), (int) event.getY());
Point p = new Point(x1,y1);
if (myWhiteArray.contains(p)|| myBlackArray.contains(p)) {
return false;
}
if (isWhite) {
myWhiteArray.add(p);
chess[y1][x1]=1;
}else {
myBlackArray.add(p);
chess[y1][x1]=2;
}
invalidate(); //invalidate()是用来刷新View的,必须在UI线程中使用
isWhite = !isWhite;
if (isGemOver()) {
Toast.makeText(mycontext,"游戏结束",Toast.LENGTH_SHORT ).show();
return false;
}
}
return true;
}
private boolean isGemOver()
{
if(chess[0][0]==chess[0][1] &&chess[0][1]==chess[0][2] &&chess[0][1]!=0)
return true;
if(chess[1][0]==chess[1][1] &&chess[1][1]==chess[0][2] &&chess[1][1]!=0)
return true;
if(chess[2][0]==chess[2][1] &&chess[2][1]==chess[2][2] &&chess[2][1]!=0)
return true;
if(chess[0][0]==chess[1][0] &&chess[1][0]==chess[2][0] &&chess[1][0]!=0)
return true;
if(chess[0][1]==chess[1][1] &&chess[1][1]==chess[2][1] &&chess[1][1]!=0)
return true;
if(chess[0][2]==chess[1][2] &&chess[1][2]==chess[2][2] &&chess[1][2]!=0)
return true;
if(chess[0][0]==chess[1][1] &&chess[1][1]==chess[2][2] &&chess[1][1]!=0)
return true;
if(chess[0][2]==chess[1][1] &&chess[1][1]==chess[2][0] &&chess[1][1]!=0)
return true;
return false;
}
private Point getVaLidPiont(int x , int y){
return new Point((int)(x/100),(int)(y/100));
}
}
手势识别
package com.example.three_chess;
import android.content.Context;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Toast;
class CwjView extends View {
private GestureDetector mGD; //手势识别器
int MAJOR_MOVE=20;
Context mycontext;
public CwjView(Context context, AttributeSet attrs) {
super(context, attrs);
mycontext=context;
mGD = new GestureDetector(context, new GestureDetector. OnGestureListener() {
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
int dx = (int) (e2.getX() - e1.getX()); //计算滑动的距离
if (Math.abs(dx) > MAJOR_MOVE && Math.abs(velocityX) > Math.abs(velocityY)) { //必须大于MAJOR_MOVE的动作才识别
if (velocityX > 0) {
//向右边
Toast.makeText(mycontext,"向右边",Toast.LENGTH_SHORT ).show();
} else {
//向左边
Toast.makeText(mycontext,"向左边",Toast.LENGTH_SHORT ).show();
}
return true;
} else {
return false; //当然可以处理velocityY处理向上和向下的动作
}
}
public boolean onDown(MotionEvent e) {
// TODO Auto-generated method stub
return false;
}
public void onLongPress(MotionEvent e) {
// TODO Auto-generated method stub
}
public boolean onScroll(MotionEvent e1, MotionEvent e2,
float distanceX, float distanceY) {
// TODO Auto-generated method stub
return false;
}
public void onShowPress(MotionEvent e) {
// TODO Auto-generated method stub
}
public boolean onSingleTapUp(MotionEvent e) {
// TODO Auto-generated method stub
return false;
}
});
}
@Override
public boolean onTouchEvent(MotionEvent event) {
mGD.onTouchEvent(event);
return true;
}
}