Android的 - 等待触摸事件 [英] Android - wait for touch event

查看:215
本文介绍了Android的 - 等待触摸事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Andr​​oid应用程序,我想按顺序在屏幕上显示多个图像,等待触摸事件(一个水龙头)转到下一个。我看到这里要做到这一点应该是单向的:

In my Android app, I'd like to display several images on the screen in sequence, waiting for a touch event (a single tap) to go to the next one. I saw here that one way to do this should be:

public class LoadImage extends Activity {
    private Thread thread; //defined inside the activity

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_load_image);

        [get an image and create a bitmap from it]

        ImageView imageView = (ImageView) findViewById(R.id.imageView);
        imageView.setImageBitmap(bitmap);

        thread = new Thread() {
            @Override
            public void run() {
                try {
                    synchronized(this) {
                        wait(100000000); //large number
                    }
                }
                catch(InterruptedException ex) {                    
                }         
            }
        };
        thread.start();    
   }

    @Override
    public boolean onTouchEvent(MotionEvent evt) {
        if (evt.getAction() == MotionEvent.ACTION_DOWN) {
            synchronized (thread) {
                thread.notifyAll();
            }
        }
        return true;
    }
}

不过,这code似乎只是跳过等待,直接跳到最后一张图像。有什么不对的地方,和/或是否有更好的方法来做到这一点?

However, this code appears to just skip the waiting and immediately jump to the last image. What's wrong with it, and/or is there a better way to do this?

推荐答案

默认情况下,在Android中的事件监听器是在等待 - 您不必提供任何延迟

By default, event listeners in Android are for waiting - you don't have to provide any delay.

只需设置上的ImageView的 onTouchEvent(...)监听器,并显示第一个位图。当ImageView的被触摸时,显示下一个位图等。所有你需要做的就是保持多少接触已经有才能知道要显​​示的图像(图像1,2,3,4等)的计数。

Simply set the onTouchEvent(...) listener on the ImageView and show the first bitmap. When the ImageView is touched, show the next bitmap and so on. All you have to do is keep a count of how many touches there have been in order to know which image to show (image 1, 2, 3, 4 etc).

示例...

public class LoadImage extends Activity {

    int imageNumber = 1;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_load_image);

        //get an image and create a bitmap from it

        ImageView imageView = (ImageView) findViewById(R.id.imageView);
        imageView.setImageBitmap(bitmap);

   }

    @Override
    public boolean onTouchEvent(MotionEvent evt) {
        if (evt.getAction() == MotionEvent.ACTION_DOWN) {
            imageNumber++;
            switch (imageNumber) {
                case 2:
                    // show image 2
                    break;
                case 3:
                    // show image 3
                    break;
                ...
            }
            return true;
        }
        return false;
    }
}

这篇关于Android的 - 等待触摸事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆