【问题标题】:Touch within view only- android仅在视图内触摸 - android
【发布时间】:2013-01-05 21:50:47
【问题描述】:

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

这是绘制方法:

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;  



}

这工作得很好,但问题是它甚至在视图之外也能工作。 所以如果我触摸外面的视图,我的酒吧也会移到那里 即使我点击一个按钮,我的栏也会尝试移动 只有当用户在视图中触摸时,我才想拖放组件

任何帮助将不胜感激 谢谢

【问题讨论】:

    标签: java android user-interface


    【解决方案1】:

    一键获取视图的 x、y、宽度和高度。然后每次移动栏时,正如我在MotionEvent.ACTION_MOVE 中所理解的那样,检查当前坐标是否在视图的范围内,如果是这样,则允许移动栏。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多