Android版的TouchEvent ---需要帮助/根据塞纳里奥建议 [英] Android touchevent--- need help/suggestion according to senario

查看:230
本文介绍了Android版的TouchEvent ---需要帮助/根据塞纳里奥建议的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在我有一个ImageView的和一个圆,处于稍低于比中心位置(不过这不应该的问题)。

Now I have an ImageView and its a circle which is at the slightly below than center position (But this should not matter).

我已经写了code ImageView的为ACTION_DOWN,ACTION_UP,现在考虑为用户onTouch已经把手指圈,并移....我想主动约code当用户移动手指和超越CIRCLE图像的区域(只要用户超过code应该昂立一次ececuted地区)

I have written code onTouch of ImageView for ACTION_DOWN,ACTION_UP ,now consider as user have put finger on circle and move and move.... I want to active some code when user move finger and exceed the region of CIRCLE image(As soon as user exceed the region the code should be ececuted onlly once)

下面是我的code

ImageView view1 = (ImageView) findViewById(R.id.fccircledetectionarea);

      view1.setOnTouchListener(new OnTouchListener()
      {

        @Override
        public boolean onTouch(View v, MotionEvent event) 
        {
            ImageView imageView=(ImageView) findViewById(R.id.fccircledetectionarea);
            switch(event.getAction())
            {
            case MotionEvent.ACTION_DOWN:
                final float x=imageView.getTop();


                Toast.makeText(PlayScreen.this, "Top Position:"+x, Toast.LENGTH_SHORT).show();
                break;
            case MotionEvent.ACTION_UP:
                Toast.makeText(PlayScreen.this, "Over", Toast.LENGTH_SHORT).show();
                break;

            case MotionEvent.ACTION_MOVE:


                Toast.makeText(PlayScreen.this, "Over", Toast.LENGTH_SHORT).show();
                break;

            }
            return true;
        }
    });

我无法通过ACTION_MOVE存档我的目标:它开始工作,如果圆圈内用户手指移动

I cannot archive my goal through ACTION_MOVE: as it start to work if user move finger within the circle

我的第二个查询是如何设置APLHA这个ImageView的我已经使用了

And my second query is that How to set aplha of this imageview I have used

view1.setAlpha(0);

但不工作,我也做这个的ImageView看不见但比我onTouch code不能正常工作

But is not working and I have also made this imageview invisible but than my onTouch code is not working

推荐答案

在你的 onTouch 方法记录一个布尔值来检查,如果用户是圆的范围内接触

Within your onTouch method record a boolean to check if the user is touching within the bounds of the circle.

boolean inCircle = java.lang.Math.sqrt(x*x + y*y) <= circleRadius

其中x是从 event.getX()拍摄 - circleCenterY y是 event.getY()拍摄$ C>(触摸位置是从圆的中心偏移)

Where x is taken from event.getX() - circleCenterX and y is taken from event.getY() - circleCenterY (touch position's offset from center of the circle)

这篇关于Android版的TouchEvent ---需要帮助/根据塞纳里奥建议的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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