Android touchevent— need help/suggestion according to scenario

女生的网名这么多〃 提交于 2019-12-13 04:58:33

问题


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

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)

Here is my 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;
        }
    });

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

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

view1.setAlpha(0);

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


回答1:


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

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)



来源:https://stackoverflow.com/questions/6676846/android-touchevent-need-help-suggestion-according-to-scenario

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!