android action_up 不工作

发布于 2024-12-29 03:45:03 字数 2625 浏览 1 评论 0原文

我想做的是,当有人将手指向前按住屏幕时等于 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

睫毛溺水了 2025-01-05 03:45:03

包含此 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.

如歌彻婉言 2025-01-05 03:45:03

用这个

          case MotionEvent.ACTION_DOWN:
                    touch_start(x, y);
                    invalidate();
                    break;
                case MotionEvent.ACTION_MOVE:                       
                    touch_move(x, y);
                    invalidate();
                    break;
                case MotionEvent.ACTION_UP:
                    touch_up();
                    invalidate();
                    break;
                }

use this

          case MotionEvent.ACTION_DOWN:
                    touch_start(x, y);
                    invalidate();
                    break;
                case MotionEvent.ACTION_MOVE:                       
                    touch_move(x, y);
                    invalidate();
                    break;
                case MotionEvent.ACTION_UP:
                    touch_up();
                    invalidate();
                    break;
                }
天邊彩虹 2025-01-05 03:45:03

UI 中的计时器不安全。我可以提出一个我自己的类,目的是保留一些时间和日期,在某些视图中展示它们自己。

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import android.os.Handler;
import android.widget.TextView;
/**
 * The class for creating and refreshing many different fields on different layouts,
 * that can hold actual time and/or date in different formats 
 * The formats should be as in http://developer.android.com/reference/java/text/SimpleDateFormat.html. 
 * Only present and visible fields are being actualized, so there is no need to clean the clock list after closing an activity
 * 
 * Examples of use:
 * 
 *      Clock.registerClock((TextView) findViewById(R.id.TimeField), "HH:mm");
 *      Clock.registerClock((TextView) findViewById(R.id.DateField), "d.M.yyyy EEE");
 *      Clock.start(10000L);
 *
 * @author Petr Gangnus
 */
public final class Clock {
    /**
     * the handler that works instead of timer and supports UI
     */
    static private Handler handler = new Handler();
    /**
     * the interval of the time refreshing
     */
    static private long refreshStep;

    /**
     * pairs TextView timer+time/date format
     */
    private TextView clockFace;
    private String format;
    private Clock(TextView clockFace, String format){
        this.clockFace=clockFace;
        this.format=format;
    }
    // here is the list of views containing the visual timers that should be held actual
    static private ArrayList<Clock> clocks=new ArrayList<Clock>();
    /**
     * fills all timer fields by actual time value, according to their formats.
     */
    static private Runnable mUpdateTimeTask = new Runnable() {
       public void run() {
           for(Clock clock:clocks){
               showActualTimeDate(clock);
           }
           handler.postDelayed(this,refreshStep);
       }
    };

    //============================================ public members ====================================================================
    /**
     * add a clock to the list of updating clocks
     * @param clockFace - the place where the time or date will be shown 
     * @param format - the format of the time/date 
     * @return
     */
    public static boolean registerClock(TextView clockFace, String format){
        if (clockFace==null) return false;
        if(clocks.contains(clockFace)){
            // old clockFace
            clocks.get(clocks.indexOf(clockFace)).format=format;
        } else {
            // new clockFace
            clocks.add(new Clock(clockFace, format));
        }
        return true;
    }
    /**
     * remove a clock from the updating list
     * @param clockFace
     * @return
     */
    public static boolean unRegisterClock(TextView clockFace){
        if (clockFace==null) return false;
        if(clocks.contains(clockFace)){
            // found clockFace
            clocks.remove(clocks.indexOf(clockFace));
        } else {
            // not found clockFace
            return false;
        }
        return true;
    }
    /**
     * put in the "place" the actual date/time in the appropriate "format"
     * @param place
     * @param format
     */
    public static void showActualTimeDate(Clock clock){
        if (clock.clockFace==null) return;
        if (clock.clockFace.getVisibility()!=TextView.VISIBLE) return;
        Date thisDate=new Date();
        SimpleDateFormat df=new SimpleDateFormat(clock.format);
        clock.clockFace.setText(df.format(thisDate));
    }
    /**
     * start the ticking for all clocks
     * @param step the tick interval
     */
    public static void start(long step) { 
        refreshStep=step;
        handler.removeCallbacks(mUpdateTimeTask);
        handler.postDelayed(mUpdateTimeTask, 0);
    }
    /**
     * Stopping ticking all clocks (not removing them)
     * the calling could be put somewhere in onStop
     */
    public static void stop() { 
        handler.removeCallbacks(mUpdateTimeTask);
    }
}

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.

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import android.os.Handler;
import android.widget.TextView;
/**
 * The class for creating and refreshing many different fields on different layouts,
 * that can hold actual time and/or date in different formats 
 * The formats should be as in http://developer.android.com/reference/java/text/SimpleDateFormat.html. 
 * Only present and visible fields are being actualized, so there is no need to clean the clock list after closing an activity
 * 
 * Examples of use:
 * 
 *      Clock.registerClock((TextView) findViewById(R.id.TimeField), "HH:mm");
 *      Clock.registerClock((TextView) findViewById(R.id.DateField), "d.M.yyyy EEE");
 *      Clock.start(10000L);
 *
 * @author Petr Gangnus
 */
public final class Clock {
    /**
     * the handler that works instead of timer and supports UI
     */
    static private Handler handler = new Handler();
    /**
     * the interval of the time refreshing
     */
    static private long refreshStep;

    /**
     * pairs TextView timer+time/date format
     */
    private TextView clockFace;
    private String format;
    private Clock(TextView clockFace, String format){
        this.clockFace=clockFace;
        this.format=format;
    }
    // here is the list of views containing the visual timers that should be held actual
    static private ArrayList<Clock> clocks=new ArrayList<Clock>();
    /**
     * fills all timer fields by actual time value, according to their formats.
     */
    static private Runnable mUpdateTimeTask = new Runnable() {
       public void run() {
           for(Clock clock:clocks){
               showActualTimeDate(clock);
           }
           handler.postDelayed(this,refreshStep);
       }
    };

    //============================================ public members ====================================================================
    /**
     * add a clock to the list of updating clocks
     * @param clockFace - the place where the time or date will be shown 
     * @param format - the format of the time/date 
     * @return
     */
    public static boolean registerClock(TextView clockFace, String format){
        if (clockFace==null) return false;
        if(clocks.contains(clockFace)){
            // old clockFace
            clocks.get(clocks.indexOf(clockFace)).format=format;
        } else {
            // new clockFace
            clocks.add(new Clock(clockFace, format));
        }
        return true;
    }
    /**
     * remove a clock from the updating list
     * @param clockFace
     * @return
     */
    public static boolean unRegisterClock(TextView clockFace){
        if (clockFace==null) return false;
        if(clocks.contains(clockFace)){
            // found clockFace
            clocks.remove(clocks.indexOf(clockFace));
        } else {
            // not found clockFace
            return false;
        }
        return true;
    }
    /**
     * put in the "place" the actual date/time in the appropriate "format"
     * @param place
     * @param format
     */
    public static void showActualTimeDate(Clock clock){
        if (clock.clockFace==null) return;
        if (clock.clockFace.getVisibility()!=TextView.VISIBLE) return;
        Date thisDate=new Date();
        SimpleDateFormat df=new SimpleDateFormat(clock.format);
        clock.clockFace.setText(df.format(thisDate));
    }
    /**
     * start the ticking for all clocks
     * @param step the tick interval
     */
    public static void start(long step) { 
        refreshStep=step;
        handler.removeCallbacks(mUpdateTimeTask);
        handler.postDelayed(mUpdateTimeTask, 0);
    }
    /**
     * Stopping ticking all clocks (not removing them)
     * the calling could be put somewhere in onStop
     */
    public static void stop() { 
        handler.removeCallbacks(mUpdateTimeTask);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文