Android touchevent ---根据情况需要帮助/建议 [英] Android touchevent--- need help/suggestion according to scenario

查看:87
本文介绍了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).

我已经为Action_DOWN,ACTION_UP编写了ImageView的onTouch代码,现在考虑到用户将手指放在圆圈上并移动并移动....我想在用户移动手指并超出CIRCLE图像区域时激活一些代码(一旦用户超出该区域,该代码应仅被执行一次)

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)

这是我的代码

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

第二个查询 是如何设置我使用过的该图像视图的Alpha

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

view1.setAlpha(0);

但是不起作用,并且我也使该图像视图不可见,但是我的onTouch代码不起作用

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() - circleCenterX,y取自event.getY() - circleCenterY(触摸位置相对于圆心的偏移)

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天全站免登陆