作者:mobiledu2502909113 | 来源:互联网 | 2022-10-19 14:50
当在自定义View或者做游戏的时候,我们常常会用到键盘触发事件和屏幕触发事件!在自定义的View里的键盘触发事件和屏幕触发事件和activity里的键盘触发事件和屏幕触发事件是怎么样交互的呢,接下来为您详细介绍,感兴趣的朋友可以了解下哦
当在自定义View或者做游戏的时候,我们常常会用到键盘触发事件和屏幕触发事件!在自定义的View里的键盘触发事件(比如:onKeyDown(int keyCode, KeyEvent event))和屏幕触发事件(onTouchEvent(MotionEvent event))和activity里的键盘触发事件(比如:onKeyDown(int keyCode, KeyEvent event))和屏幕触发事件(onTouchEvent(MotionEvent event))是怎么样交互的呢?是怎样的一个运行原理呢?下面来看看:
1、屏幕触发事件:哪个activity或者视图,控件在最上层就最先触发这个控件里的屏幕触发事件,返回值如果是默认或者false就会一层一层的往下传递!如果返回值是true,则在执行完动作之后不会往下传递!
2、键盘触发事件:这个原理和屏幕触发事件差不多,但是不同的时,在没有设置控件处于焦点时,一般按键盘里的按键触发的是activity里的键盘触发事件(比如:onKeyDown(int keyCode, KeyEvent event))。
下面来举个例子:
比如在做游戏的时候,我们经常自定义视图,我们一般都喜欢和使用触发本视图(GameView)里的键盘触发事件和屏幕触发事件,但是比如:onKeyDown(int keyCode, KeyEvent event)),一般我们按键盘的时候直接触发的是activity里的键盘触发事件,怎样让它直接触发(GameView)里的键盘触发事件呢?
第一种方法:
例子1:
代码如下:
View Code
package net.loonggg.project;
import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.MotionEvent;
public class MainActivity extends Activity {
private GameView gameView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
gameView = new GameView(this);
setContentView(gameView);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
System.out.println("MainActivity:" + " keyCode:" + keyCode + " evnet:"
+ event);
return super.onKeyDown(keyCode, event);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
System.out.println("MainActivity:" + " event:" + event + " x:"
+ event.getX() + " y:" + event.getY());
return super.onTouchEvent(event);
}
}
package net.loonggg.project;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
public class GameView extends View {
private Paint paint;
private int pointX;
private int pointY;
public GameView(Context context) {
super(context);
// 设置GameView获得焦点
this.setFocusable(true);
paint = new Paint();
paint.setColor(Color.YELLOW);
Thread t = new Thread(new MyThread());
t.start();
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawCircle(pointX, pointY, 15, paint);
}
class MyThread implements Runnable {
@Override
public void run() {
while (true) {
try {
Thread.sleep(3000);
pointY = pointY + 10;
pointX = pointX + 10;
if (pointX > 200) {
break;
}
} catch (InterruptedException e) {
e.printStackTrace();
}
GameView.this.postInvalidate();
}
}
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
System.out.println("GameView:" + " keyCode:" + keyCode + " evnet:"
+ event);
//此事件不会传播出去
return true;
}
@Override
public boolean onTouchEvent(MotionEvent event) {
System.out.println("GameView:" + " event:" + event + " x:"
+ event.getX() + " y:" + event.getY());
//此事件不会传播出去
return true;
}
}
第二种方法:
代码如下:
View Code
package net.loonggg.project;
import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.MotionEvent;
public class MainActivity extends Activity {
private GameView gameView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
gameView = new GameView(this);
setContentView(gameView);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
System.out.println("MainActivity:" + " keyCode:" + keyCode + " evnet:"
+ event);
//把事件传递给gameView
gameView.onKeyDown(keyCode, event);
return super.onKeyDown(keyCode, event);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
System.out.println("MainActivity:" + " event:" + event + " x:"
+ event.getX() + " y:" + event.getY());
//把事件传递给gameView
gameView.onTouchEvent(event);
return super.onTouchEvent(event);
}
}
package net.loonggg.project;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
public class GameView extends View {
private Paint paint;
private int pointX;
private int pointY;
public GameView(Context context) {
super(context);
paint = new Paint();
paint.setColor(Color.YELLOW);
Thread t = new Thread(new MyThread());
t.start();
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawCircle(pointX, pointY, 15, paint);
}
class MyThread implements Runnable {
@Override
public void run() {
while (true) {
try {
Thread.sleep(3000);
pointY = pointY + 10;
pointX = pointX + 10;
if (pointX > 200) {
break;
}
} catch (InterruptedException e) {
e.printStackTrace();
}
GameView.this.postInvalidate();
}
}
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
System.out.println("GameView:" + " keyCode:" + keyCode + " evnet:"
+ event);
return super.onKeyDown(keyCode, event);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
System.out.println("GameView:" + " event:" + event + " x:"
+ event.getX() + " y:" + event.getY());
return super.onTouchEvent(event);
}
}
在这里,推荐使用第二种方法,因为第一种方法有的模拟器不能够使用!
囧神的世界你不懂,虫哥的生活你没有,只有程序猿的世界大家才知道。程序猿们,为了自己的精彩世界奋斗吧,努力吧!加油……