0

我刚开始使用触摸事件。所以我搜索了一个例子,发现了这个http://www.vogella.com/articles/AndroidTouch/article.html .. 该教程很棒,但有一件事是当我运行代码时。我向各个方向移动手指,但它没有显示在卡瓦斯中。我的意思是它没有画出什么东西。

这是我的代码:

public class SingleTouchEventView extends View {
    private Paint paint = new Paint();
    private Path path = new Path();
    public SingleTouchEventView(Context context, AttributeSet attrs) {
        super(context, attrs);
        paint.setAntiAlias(true);
        paint.setStrokeWidth(6f);
        paint.setColor(Color.BLACK);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeJoin(Paint.Join.ROUND);

    }


    @Override
    protected void onDraw(Canvas canvas) {
       canvas.drawPath(path,paint);

    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        float eventX = event.getX();
        float eventY = event.getY();

        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                path.moveTo(eventX, eventY);
                return true;
            case MotionEvent.ACTION_MOVE:
                path.lineTo(eventX, eventY);
                break;
            case MotionEvent.ACTION_UP:
                // nothing to do
                break;
            default:
                return false;
        }

        // Schedules a repaint.
        invalidate();
        return true;
    }
}

和我的主要活动

public class SingleTouchActivity extends Activity {
    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(new SingleTouchEventView(this,null));
    }
}

我在这里想念什么?谢谢

4

2 回答 2

1

尝试如下:

private void touch_up()
{
    path.lineTo(m_X, m_Y);
    // commit the path to our offscreen
    canvas.drawPath(m_path, m_paint);
    // kill this so we don't double draw
    path.reset();
}
private void touch_start(float p_x, float p_y)
{
    path.reset();
    path.moveTo(p_x, p_y);
    m_X = p_x;
    m_Y = p_y;
}   
private void touch_move(float p_x, float p_y)
{
    float m_dx = Math.abs(p_x - m_X);
    float m_dy = Math.abs(p_y - m_Y);
    if (m_dx >= TOUCH_TOLERANCE || m_dy >= TOUCH_TOLERANCE)
    {
        m_path.quadTo(m_X, m_Y, (p_x + m_X) / 2, (p_y + m_Y) / 2);
        m_X = p_x;
        m_Y = p_y;          
        m_pathDrawn = true;
    }       
}

对于onTouchEvent

@Override
public boolean onTouchEvent(MotionEvent p_event)
{
    float m_x = p_event.getX();
    float m_y = p_event.getY();     
    switch (p_event.getAction())
    {
        case MotionEvent.ACTION_DOWN:
            touch_start(m_x, m_y);
            invalidate();                   
            break;
        case MotionEvent.ACTION_MOVE:
            touch_move(m_x, m_y);
            invalidate();
            break;
        case MotionEvent.ACTION_UP:
            touch_up();
            invalidate();                   
            break;
    }
    return true;
}

我希望它会帮助你。

于 2013-01-24T06:01:21.093 回答
1

试试这样

@Override
public boolean onTouchEvent(MotionEvent ev) {
    // Let the ScaleGestureDetector inspect all events.
    mScaleDetector.onTouchEvent(ev);

    final int action = ev.getAction();
    switch (action & MotionEvent.ACTION_MASK) {
        case MotionEvent.ACTION_DOWN: {
            final float x = ev.getX();
            final float y = ev.getY();
            mLastTouchX = x;
            mLastTouchY = y;
            mActivePointerId = ev.getPointerId(0);
            break;
        }

        case MotionEvent.ACTION_MOVE: {
            final int pointerIndex = ev.findPointerIndex(mActivePointerId);
            final float x = ev.getX(pointerIndex);
            final float y = ev.getY(pointerIndex);

            // Only move if the ScaleGestureDetector isn't processing a gesture.
            if (!mScaleDetector.isInProgress()) {
                final float dx = x - mLastTouchX;
                final float dy = y - mLastTouchY;

                mPosX += dx;
                mPosY += dy;

                invalidate();
            }

            mLastTouchX = x;
            mLastTouchY = y;

            break;
        }

        case MotionEvent.ACTION_UP: {
            mActivePointerId = INVALID_POINTER_ID;
            break;
        }

        case MotionEvent.ACTION_CANCEL: {
            mActivePointerId = INVALID_POINTER_ID;
            break;
        }

        case MotionEvent.ACTION_POINTER_UP: {
            final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) 
                    >> MotionEvent.ACTION_POINTER_INDEX_SHIFT;
            final int pointerId = ev.getPointerId(pointerIndex);
            if (pointerId == mActivePointerId) {
                // This was our active pointer going up. Choose a new
                // active pointer and adjust accordingly.
                final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
                mLastTouchX = ev.getX(newPointerIndex);
                mLastTouchY = ev.getY(newPointerIndex);
                mActivePointerId = ev.getPointerId(newPointerIndex);
            }
            break;
        }
    }    
    return true;
}

有关更多信息,请查看此链接

于 2013-01-24T06:00:51.907 回答