Android 当用户触摸屏幕时调用的事件?
欢迎大家 我正在开发一个游戏。我希望当用户触摸屏幕时,必须每隔100毫秒从屏幕底部向触摸到的x,y坐标持续发射激光,直到用户停止触摸屏幕。我有激光工作,但现在我需要每100毫秒持续发射一次 我正在写这篇文章,问题是我不知道如何满足我的需求。如果用户触摸屏幕,我想每100毫秒发射一次激光 如果我将激光动画置于onTouchAndroid 当用户触摸屏幕时调用的事件?,android,ontouchlistener,touch-event,motionevent,Android,Ontouchlistener,Touch Event,Motionevent,欢迎大家 我正在开发一个游戏。我希望当用户触摸屏幕时,必须每隔100毫秒从屏幕底部向触摸到的x,y坐标持续发射激光,直到用户停止触摸屏幕。我有激光工作,但现在我需要每100毫秒持续发射一次 我正在写这篇文章,问题是我不知道如何满足我的需求。如果用户触摸屏幕,我想每100毫秒发射一次激光 如果我将激光动画置于onTouchMotionEvent.ACTION\u MOVE事件中,则仅当手指移动时,激光才会被抛出。但我希望激光每100毫秒发射一次,而不移动手指 此外,MotionEvent.ACTI
MotionEvent.ACTION\u MOVE
事件中,则仅当手指移动时,激光才会被抛出。但我希望激光每100毫秒发射一次,而不移动手指
此外,MotionEvent.ACTION\u DOWN不起作用,因为当用户触摸屏幕时,它只被调用一次,但只有一次
如何才能满足我的需求?没有一种简单的方法可以让您每100毫秒举办一次活动 但你可以做到:
class TouchStarted {
AtomicBoolean actionDownFlag = new AtomicBoolean(true);
Thread loggingThread = new Thread(new Runnable(){
public void run(){
while(actionDownFlag.get()){
Log.d("event", "Touching Down");
try {
Thread.sleep(100, 0);
} catch (InterruptedException e) {
}
//maybe sleep some times to not polute your logcat
}
Log.d("event", "Not Touching");
}
});
public void stop() {
actionDownFlag.set(false);
}
public void start() {
actionDownFlag.set(true);
loggingThread.start();
}
}
TouchStarted last = null;
@Override
public boolean onTouchEvent(MotionEvent event) {
if(event.getAction()==MotionEvent.ACTION_DOWN){
if(last != null) last.stop();
last = new TouchStarted();
last.start();
}
if(event.getAction()==MotionEvent.ACTION_UP){
last.stop();
}
}
没有一种简单的方法可以让你的活动每100毫秒进行一次 但你可以做到:
class TouchStarted {
AtomicBoolean actionDownFlag = new AtomicBoolean(true);
Thread loggingThread = new Thread(new Runnable(){
public void run(){
while(actionDownFlag.get()){
Log.d("event", "Touching Down");
try {
Thread.sleep(100, 0);
} catch (InterruptedException e) {
}
//maybe sleep some times to not polute your logcat
}
Log.d("event", "Not Touching");
}
});
public void stop() {
actionDownFlag.set(false);
}
public void start() {
actionDownFlag.set(true);
loggingThread.start();
}
}
TouchStarted last = null;
@Override
public boolean onTouchEvent(MotionEvent event) {
if(event.getAction()==MotionEvent.ACTION_DOWN){
if(last != null) last.stop();
last = new TouchStarted();
last.start();
}
if(event.getAction()==MotionEvent.ACTION_UP){
last.stop();
}
}
没有一种简单的方法可以让你的活动每100毫秒进行一次 但你可以做到:
class TouchStarted {
AtomicBoolean actionDownFlag = new AtomicBoolean(true);
Thread loggingThread = new Thread(new Runnable(){
public void run(){
while(actionDownFlag.get()){
Log.d("event", "Touching Down");
try {
Thread.sleep(100, 0);
} catch (InterruptedException e) {
}
//maybe sleep some times to not polute your logcat
}
Log.d("event", "Not Touching");
}
});
public void stop() {
actionDownFlag.set(false);
}
public void start() {
actionDownFlag.set(true);
loggingThread.start();
}
}
TouchStarted last = null;
@Override
public boolean onTouchEvent(MotionEvent event) {
if(event.getAction()==MotionEvent.ACTION_DOWN){
if(last != null) last.stop();
last = new TouchStarted();
last.start();
}
if(event.getAction()==MotionEvent.ACTION_UP){
last.stop();
}
}
没有一种简单的方法可以让你的活动每100毫秒进行一次 但你可以做到:
class TouchStarted {
AtomicBoolean actionDownFlag = new AtomicBoolean(true);
Thread loggingThread = new Thread(new Runnable(){
public void run(){
while(actionDownFlag.get()){
Log.d("event", "Touching Down");
try {
Thread.sleep(100, 0);
} catch (InterruptedException e) {
}
//maybe sleep some times to not polute your logcat
}
Log.d("event", "Not Touching");
}
});
public void stop() {
actionDownFlag.set(false);
}
public void start() {
actionDownFlag.set(true);
loggingThread.start();
}
}
TouchStarted last = null;
@Override
public boolean onTouchEvent(MotionEvent event) {
if(event.getAction()==MotionEvent.ACTION_DOWN){
if(last != null) last.stop();
last = new TouchStarted();
last.start();
}
if(event.getAction()==MotionEvent.ACTION_UP){
last.stop();
}
}
你可以在你的行动中尝试这样的东西
Thread thread = new Thread(new Runnable){
@Override
public void run(){
while(yourFlag) //figure out something to set up a loop
try{
yourfirelasermethod();
Thread.sleep(100); // this waits 100ms until firing
// again
}catch(Exception e){
}
}
}).start();
你可以在你的行动中尝试这样的东西
Thread thread = new Thread(new Runnable){
@Override
public void run(){
while(yourFlag) //figure out something to set up a loop
try{
yourfirelasermethod();
Thread.sleep(100); // this waits 100ms until firing
// again
}catch(Exception e){
}
}
}).start();
你可以在你的行动中尝试这样的东西
Thread thread = new Thread(new Runnable){
@Override
public void run(){
while(yourFlag) //figure out something to set up a loop
try{
yourfirelasermethod();
Thread.sleep(100); // this waits 100ms until firing
// again
}catch(Exception e){
}
}
}).start();
你可以在你的行动中尝试这样的东西
Thread thread = new Thread(new Runnable){
@Override
public void run(){
while(yourFlag) //figure out something to set up a loop
try{
yourfirelasermethod();
Thread.sleep(100); // this waits 100ms until firing
// again
}catch(Exception e){
}
}
}).start();