android action_up 不工作
我想做的是,当有人将手指向前按住屏幕时等于 true,但当他们将其拿开时等于 false 所以我尝试使用 get_actions() 方法 但只有 action_down 被调用 这是我的代码
public class zombView extends SurfaceView{
private Bitmap bmp, grass, joystick;
private SurfaceHolder holder;
Timer t = new Timer();
float x = 0, y = 0;
boolean forward;
public zombView(Context context) {
super(context);
holder = getHolder();
holder.addCallback(new SurfaceHolder.Callback() {
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
}
@Override
public void surfaceCreated(final SurfaceHolder holder) {
t.scheduleAtFixedRate(new TimerTask(){
public void run(){
Canvas c = holder.lockCanvas(null);
onDraw(c);
holder.unlockCanvasAndPost(c);
if(forward){
x = x + 5;
}
onTouchEvent(null);
}
},200,100);
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format,
int width, int height) {
}
});
bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
grass = BitmapFactory.decodeResource(getResources(), R.drawable.grassland);
joystick = BitmapFactory.decodeResource(getResources(), R.drawable.joystic);
}
@Override
protected void onDraw(Canvas canvas) {
canvas.drawColor(Color.BLACK);
canvas.drawBitmap(grass, getWidth() - getWidth(), getHeight() - getHeight(), null);
canvas.drawBitmap(joystick, getWidth() - getWidth(),joystick.getHeight(), null);
canvas.drawBitmap(bmp, x, y, null);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
/*switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN: forward = true;
case MotionEvent.ACTION_POINTER_DOWN: forward = true;
case MotionEvent.ACTION_UP: forward = false;
case MotionEvent.ACTION_POINTER_UP:forward = false;
case MotionEvent.ACTION_MOVE: forward = true;
}*/
if(event.getAction()== MotionEvent.ACTION_DOWN){
forward = true;
}if(event.getAction()== MotionEvent.ACTION_UP){
forward = false;
}
return super.onTouchEvent(event);
}
}
what im trying to do is when someone hold there finger down on the screen forward equals true but when they take it off it equals false
so i tryed using the get_actions() methods
but only the action_down gets called
heres my code
public class zombView extends SurfaceView{
private Bitmap bmp, grass, joystick;
private SurfaceHolder holder;
Timer t = new Timer();
float x = 0, y = 0;
boolean forward;
public zombView(Context context) {
super(context);
holder = getHolder();
holder.addCallback(new SurfaceHolder.Callback() {
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
}
@Override
public void surfaceCreated(final SurfaceHolder holder) {
t.scheduleAtFixedRate(new TimerTask(){
public void run(){
Canvas c = holder.lockCanvas(null);
onDraw(c);
holder.unlockCanvasAndPost(c);
if(forward){
x = x + 5;
}
onTouchEvent(null);
}
},200,100);
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format,
int width, int height) {
}
});
bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
grass = BitmapFactory.decodeResource(getResources(), R.drawable.grassland);
joystick = BitmapFactory.decodeResource(getResources(), R.drawable.joystic);
}
@Override
protected void onDraw(Canvas canvas) {
canvas.drawColor(Color.BLACK);
canvas.drawBitmap(grass, getWidth() - getWidth(), getHeight() - getHeight(), null);
canvas.drawBitmap(joystick, getWidth() - getWidth(),joystick.getHeight(), null);
canvas.drawBitmap(bmp, x, y, null);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
/*switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN: forward = true;
case MotionEvent.ACTION_POINTER_DOWN: forward = true;
case MotionEvent.ACTION_UP: forward = false;
case MotionEvent.ACTION_POINTER_UP:forward = false;
case MotionEvent.ACTION_MOVE: forward = true;
}*/
if(event.getAction()== MotionEvent.ACTION_DOWN){
forward = true;
}if(event.getAction()== MotionEvent.ACTION_UP){
forward = false;
}
return super.onTouchEvent(event);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
包含此 SurfaceView 的布局未将事件传递到此 SurfaceView。你需要做的是覆盖 ontouch 方法并返回 false 。我希望这会有所帮助。
The layout containing this SurfaceView is not passing event to this SurfaceView. What you need to do is override ontouch menthod and return false in that. I hope this would be helpful.
用这个
use this
UI 中的计时器不安全。我可以提出一个我自己的类,目的是保留一些时间和日期,在某些视图中展示它们自己。
Timer in UI is unsafe. I could propose a class of my own purposed to keep some times and dates, showing themselves in some views.