仅在视图内触摸- android



我正试图这样做,但它给我头痛我有一个绘制颜色条的视图。我想让用户移动它(拖放)

this is on draw method:

int r,g,b;
        for(int i = 0; i < 64; i++)
        {       
            r =  Math.round((float)colorBuffer[i][0]*MAX_SCALING);
            g =  Math.round((float)colorBuffer[i][1]*MAX_SCALING);
            b =  Math.round((float)colorBuffer[i][2]*MAX_SCALING);
            paint.setColor(Color.argb(MAX_SCALING, r, g, b));
            canvas.drawLine(x1, y1, x2, y1, paint);
            y1+=3;
        }

rgb只是颜色。

这是我的ontouch实现。

     public boolean onTouchEvent(MotionEvent e) 
        {
            float x = e.getX();
            float y = e.getY();
            switch (e.getAction()) 
            {
                case MotionEvent.ACTION_DOWN:
                {
                    Toast t = Toast.makeText(getContext(), "Touch", Toast.LENGTH_SHORT);
                    t.show();
                    break;
                }
                case MotionEvent.ACTION_MOVE:
                {
                    x1=(int) x;
                    x2=(int) (x+30);
                    y1=(int) y;
                    break;
                }
                case MotionEvent.ACTION_UP:
                {
                    Toast t = Toast.makeText(getContext(), "Release", Toast.LENGTH_SHORT);
                    t.show();
                    x1=(int) x;
                    x2=(int) (x+30);
                    y1=(int) y;
                    break;
                }
            }
            invalidate();
            return true;  

}

这个可以很好地工作,但问题是它甚至在视图之外也可以工作。如果我点击视图外部,我的栏也会移到那里甚至当我点击按钮时,我的工具条也会移动我想只在用户触摸视图

时才拖放组件。

任何帮助都将是感激的由于

点击获取视图的x, y,宽度和高度。然后每次当你移动栏,正如我在MotionEvent.ACTION_MOVE的理解,检查当前坐标是否在视图的边界内,如果这是真的,然后允许移动栏。

最新更新