Java Android,无法在emulator中基于触摸或MotionEvent的视图上绘制
我正在尝试学习多人游戏的Android图形和事件处理 作为一个构建块,我正在开发一些代码,这些代码将根据触摸/运动事件绘制点的路径,但是我无法绘制,在模拟器上不会绘制任何东西 我有3个简单的类(MyPoint、GameCanvas、Game): 1) MyPoint类封装x&y位置,draw方法基于这些位置绘制点Java Android,无法在emulator中基于触摸或MotionEvent的视图上绘制,java,android,Java,Android,我正在尝试学习多人游戏的Android图形和事件处理 作为一个构建块,我正在开发一些代码,这些代码将根据触摸/运动事件绘制点的路径,但是我无法绘制,在模拟器上不会绘制任何东西 我有3个简单的类(MyPoint、GameCanvas、Game): 1) MyPoint类封装x&y位置,draw方法基于这些位置绘制点 public class MyPoint { private float x; private float y; Paint pWhite = new
public class MyPoint {
private float x;
private float y;
Paint pWhite = new Paint(R.color.white);
public MyPoint(float x, float y) {
this.x = x;
this.y = y;
}//end const
public void draw(Canvas canvas) {
canvas.drawPoint(this.x, this.y, pWhite);
}//end method
}//end MyPoint Class
2) GameCanvas是将在其上绘制的视图,该类负责
它通过实现OnTouchListener、OnTouch()方法来实现事件处理
这是实际的工作代码。我最多只修改了几行&还将视图作为活动的一个内部类,因此在测试/调试时,我不必查看不同的屏幕
public class Game extends Activity {
private gameView newGameView = null;
private MyPoint newMyPoint = null;
private java.util.List<MyPoint> pointsQ = new ArrayList<MyPoint>();
@Override
public void onCreate(Bundle savedInstanceState) {
try {
newGameView = new gameView(this);
newGameView.setBackgroundColor(Color.WHITE);
super.onCreate(savedInstanceState);
setContentView(newGameView);
//initially requestFocus
newGameView.requestFocus();
} catch (NullPointerException exc) {
Log.d(this.getClass().getName().toString(), exc.getMessage());
}
}
//inner class that represents View
public class gameView extends View {
private Paint wPaint = new Paint(Color.BLACK);
public gameView(Context context) {
super(context);
setFocusable(true);
setFocusableInTouchMode(true);
//
wPaint.setStyle(Paint.Style.FILL);
wPaint.setStrokeWidth(3);
}
//implemented in order to handle touch events
@Override
public boolean onTouchEvent(MotionEvent event) {
int action = event.getAction();
float currentX = event.getX();
float currentY = event.getY();
//log x and y coordinates
Log.d(this.getClass().getName().toString(), "X=" + currentX);
//log x and y coordinates
Log.d(this.getClass().getName().toString(), "Y=" + currentY);
if (action == MotionEvent.ACTION_DOWN) {
// log action down
Log.d(this.getClass().getName().toString(), "MotionEvent = ACTION_DOWN");
newMyPoint = new MyPoint(currentX, currentY);
pointsQ.add(newMyPoint);
} else if (action == MotionEvent.ACTION_MOVE) {
//log action move
Log.d(this.getClass().getName().toString(), "MotionEvent = ACTION_MOVE");
newMyPoint = new MyPoint(currentX, currentY);
pointsQ.add(newMyPoint);
} else if (action == MotionEvent.ACTION_UP) {
//log action move
Log.d(this.getClass().getName().toString(), "MotionEvent = ACTION_UP");
newMyPoint = new MyPoint(currentX, currentY);
pointsQ.add(newMyPoint);
}
//I moved the invalidate to the end of the method se=ince all action called it anyways
this.invalidate();
return true;
}//end onTouch()
@Override
protected void onDraw(Canvas canvas) {
MyPoint p = null;
for (int i = 0; i < pointsQ.size(); i++) {
//get MyPoint & draw based on its x,y attributes
p = (MyPoint) pointsQ.get(i);
//
canvas.drawCircle(p.getX(),p.getY(),2,wPaint);
//
Log.d(this.getClass().getName().toString(), "onDraw X= "+p.getX()+" Y= "+p.getY());
}
}
}//end inner class gameView
}//end Game activity class
公共类游戏扩展活动{
private gameView newGameView=null;
私有MyPoint newMyPoint=null;
private java.util.List pointsQ=new ArrayList();
@凌驾
创建时的公共void(Bundle savedInstanceState){
试一试{
newGameView=新gameView(此);
newGameView.setBackgroundColor(颜色:白色);
super.onCreate(savedInstanceState);
setContentView(newGameView);
//最初请求焦点
newGameView.requestFocus();
}捕获(NullPointerException exc){
Log.d(this.getClass().getName().toString(),exc.getMessage());
}
}
//表示视图的内部类
公共类gameView扩展视图{
私人油漆wPaint=新油漆(颜色:黑色);
公共游戏视图(上下文){
超级(上下文);
设置聚焦(真);
setFocusableInTouchMode(真);
//
wPaint.setStyle(油漆、样式、填充);
W油漆设置行程宽度(3);
}
//为了处理触摸事件而实现
@凌驾
公共布尔onTouchEvent(运动事件){
int action=event.getAction();
float currentX=event.getX();
float currentY=event.getY();
//对数x和y坐标
Log.d(this.getClass().getName().toString(),“X=“+currentX”);
//对数x和y坐标
Log.d(this.getClass().getName().toString(),“Y=“+currentY”);
if(action==MotionEvent.action\u DOWN){
//记录操作
Log.d(this.getClass().getName().toString(),“MotionEvent=ACTION\u DOWN”);
newMyPoint=新MyPoint(currentX,currentY);
pointsQ.add(newMyPoint);
}else if(action==MotionEvent.action\u MOVE){
//日志动作移动
Log.d(this.getClass().getName().toString(),“MotionEvent=ACTION\u MOVE”);
newMyPoint=新MyPoint(currentX,currentY);
pointsQ.add(newMyPoint);
}else if(action==MotionEvent.action\u UP){
//日志动作移动
Log.d(this.getClass().getName().toString(),“MotionEvent=ACTION\u UP”);
newMyPoint=新MyPoint(currentX,currentY);
pointsQ.add(newMyPoint);
}
//我将invalidate移到了方法se=ince的末尾,因为所有操作都调用了它
这个。使无效();
返回true;
}//结束onTouch()
@凌驾
受保护的void onDraw(画布){
MyPoint p=null;
对于(int i=0;i
这是实际的工作代码。我最多只修改了几行&还将视图作为活动的一个内部类,因此在测试/调试时,我不必查看不同的屏幕
public class Game extends Activity {
private gameView newGameView = null;
private MyPoint newMyPoint = null;
private java.util.List<MyPoint> pointsQ = new ArrayList<MyPoint>();
@Override
public void onCreate(Bundle savedInstanceState) {
try {
newGameView = new gameView(this);
newGameView.setBackgroundColor(Color.WHITE);
super.onCreate(savedInstanceState);
setContentView(newGameView);
//initially requestFocus
newGameView.requestFocus();
} catch (NullPointerException exc) {
Log.d(this.getClass().getName().toString(), exc.getMessage());
}
}
//inner class that represents View
public class gameView extends View {
private Paint wPaint = new Paint(Color.BLACK);
public gameView(Context context) {
super(context);
setFocusable(true);
setFocusableInTouchMode(true);
//
wPaint.setStyle(Paint.Style.FILL);
wPaint.setStrokeWidth(3);
}
//implemented in order to handle touch events
@Override
public boolean onTouchEvent(MotionEvent event) {
int action = event.getAction();
float currentX = event.getX();
float currentY = event.getY();
//log x and y coordinates
Log.d(this.getClass().getName().toString(), "X=" + currentX);
//log x and y coordinates
Log.d(this.getClass().getName().toString(), "Y=" + currentY);
if (action == MotionEvent.ACTION_DOWN) {
// log action down
Log.d(this.getClass().getName().toString(), "MotionEvent = ACTION_DOWN");
newMyPoint = new MyPoint(currentX, currentY);
pointsQ.add(newMyPoint);
} else if (action == MotionEvent.ACTION_MOVE) {
//log action move
Log.d(this.getClass().getName().toString(), "MotionEvent = ACTION_MOVE");
newMyPoint = new MyPoint(currentX, currentY);
pointsQ.add(newMyPoint);
} else if (action == MotionEvent.ACTION_UP) {
//log action move
Log.d(this.getClass().getName().toString(), "MotionEvent = ACTION_UP");
newMyPoint = new MyPoint(currentX, currentY);
pointsQ.add(newMyPoint);
}
//I moved the invalidate to the end of the method se=ince all action called it anyways
this.invalidate();
return true;
}//end onTouch()
@Override
protected void onDraw(Canvas canvas) {
MyPoint p = null;
for (int i = 0; i < pointsQ.size(); i++) {
//get MyPoint & draw based on its x,y attributes
p = (MyPoint) pointsQ.get(i);
//
canvas.drawCircle(p.getX(),p.getY(),2,wPaint);
//
Log.d(this.getClass().getName().toString(), "onDraw X= "+p.getX()+" Y= "+p.getY());
}
}
}//end inner class gameView
}//end Game activity class
公共类游戏扩展活动{
private gameView newGameView=null;
私有MyPoint newMyPoint=null;
private java.util.List pointsQ=new ArrayList();
@凌驾
创建时的公共void(Bundle savedInstanceState){
试一试{
newGameView=新gameView(此);
newGameView.setBackgroundColor(颜色:白色);
super.onCreate(savedInstanceState);
setContentView(newGameView);
//最初请求焦点
newGameView.requestFocus();
}捕获(NullPointerException exc){
Log.d(this.getClass().getName().toString(),exc.getMessage());
}
}
//表示视图的内部类
公共类gameView扩展视图{
私人油漆wPaint=新油漆(颜色:黑色);
公共游戏视图(上下文){
超级(上下文);
设置聚焦(真);
setFocusableInTouchMode(真);
//
wPaint.setStyle(油漆、样式、填充);
W油漆设置行程宽度(3);
}
//为了处理触摸事件而实现
@凌驾
公共布尔onTouchEvent(运动事件){
int action=event.getAction();
float currentX=event.getX();
float currentY=event.getY();
//对数x和y坐标
Log.d(this.getClass().getName().toString(),“X=“+currentX”);
//对数x和y坐标
Log.d(this.getClass().getName().toString(),“Y=“+currentY”);
if(action==MotionEvent.action\u DOWN){
//记录操作
Log.d(this.getClass().getName().toString(),“MotionEvent=ACTION\u DOWN”);
public class Game extends Activity {
private gameView newGameView = null;
private MyPoint newMyPoint = null;
private java.util.List<MyPoint> pointsQ = new ArrayList<MyPoint>();
@Override
public void onCreate(Bundle savedInstanceState) {
try {
newGameView = new gameView(this);
newGameView.setBackgroundColor(Color.WHITE);
super.onCreate(savedInstanceState);
setContentView(newGameView);
//initially requestFocus
newGameView.requestFocus();
} catch (NullPointerException exc) {
Log.d(this.getClass().getName().toString(), exc.getMessage());
}
}
//inner class that represents View
public class gameView extends View {
private Paint wPaint = new Paint(Color.BLACK);
public gameView(Context context) {
super(context);
setFocusable(true);
setFocusableInTouchMode(true);
//
wPaint.setStyle(Paint.Style.FILL);
wPaint.setStrokeWidth(3);
}
//implemented in order to handle touch events
@Override
public boolean onTouchEvent(MotionEvent event) {
int action = event.getAction();
float currentX = event.getX();
float currentY = event.getY();
//log x and y coordinates
Log.d(this.getClass().getName().toString(), "X=" + currentX);
//log x and y coordinates
Log.d(this.getClass().getName().toString(), "Y=" + currentY);
if (action == MotionEvent.ACTION_DOWN) {
// log action down
Log.d(this.getClass().getName().toString(), "MotionEvent = ACTION_DOWN");
newMyPoint = new MyPoint(currentX, currentY);
pointsQ.add(newMyPoint);
} else if (action == MotionEvent.ACTION_MOVE) {
//log action move
Log.d(this.getClass().getName().toString(), "MotionEvent = ACTION_MOVE");
newMyPoint = new MyPoint(currentX, currentY);
pointsQ.add(newMyPoint);
} else if (action == MotionEvent.ACTION_UP) {
//log action move
Log.d(this.getClass().getName().toString(), "MotionEvent = ACTION_UP");
newMyPoint = new MyPoint(currentX, currentY);
pointsQ.add(newMyPoint);
}
//I moved the invalidate to the end of the method se=ince all action called it anyways
this.invalidate();
return true;
}//end onTouch()
@Override
protected void onDraw(Canvas canvas) {
MyPoint p = null;
for (int i = 0; i < pointsQ.size(); i++) {
//get MyPoint & draw based on its x,y attributes
p = (MyPoint) pointsQ.get(i);
//
canvas.drawCircle(p.getX(),p.getY(),2,wPaint);
//
Log.d(this.getClass().getName().toString(), "onDraw X= "+p.getX()+" Y= "+p.getY());
}
}
}//end inner class gameView
}//end Game activity class