Android 尝试创建可移动视图时,使用位图而不是布局

Android 尝试创建可移动视图时,使用位图而不是布局,android,android-view,android-custom-view,Android,Android View,Android Custom View,我有一门课,我想当你触摸的时候能够在屏幕上移动。当我将它用作imageview而不是布局时,它就工作了。与imageview相比,onDraw在常规布局中的行为是否有所不同 public class MovableView extends RelativeLayout { private static final int INVALID_POINTER_ID = -1; private float posX, posY, lastPosY, lastPosX, lastTouchX, lastT

我有一门课,我想当你触摸的时候能够在屏幕上移动。当我将它用作imageview而不是布局时,它就工作了。与imageview相比,onDraw在常规布局中的行为是否有所不同

public class MovableView extends RelativeLayout {
private static final int INVALID_POINTER_ID = -1;
private float posX, posY, lastPosY, lastPosX, lastTouchX, lastTouchY;
private int activePointerId = INVALID_POINTER_ID;

public MovableView(Context context) {
    super(context);
    initView(context);
}

public MovableView(Context context, AttributeSet attrs) {
    super(context, attrs);
    initView(context);

}

public MovableView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);

    initView(context);
}

void initView(Context context) {

}

public void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    this.setTranslationY(posY);
    this.setTranslationX(posX); 

}

@Override
public boolean onTouchEvent(MotionEvent 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();

        lastTouchX = x;
        lastTouchY = y;

        activePointerId = ev.getPointerId(0);
        break;
    }

    // if the motion event falls within the bounds either vertically or
    // horizontally invalidate the view to draw it at the new position on
    // the canvas
    case MotionEvent.ACTION_MOVE: {

        final int pointerIndex = ev.findPointerIndex(activePointerId);
        float x = 0;
        float y = 0;

        try {
            x = ev.getX(pointerIndex);
            y = ev.getY(pointerIndex);
        }
        catch (IllegalArgumentException e) {
            e.printStackTrace();
        }

        final float dx = x - lastTouchX;
        final float dy = y - lastTouchY;

        posX += dx;
        posY += dy;

        boolean isValidVertical = true;
        boolean isValidHorizontal = true;

        if (isValidVertical) {
            lastPosY = posY;

        }
        else {
            posY = lastPosY; // hit the bounding limit set to whatever was
                                // last valid
        }

        if (isValidHorizontal) {
            lastPosX = posX;

        }
        else {
            posX = lastPosX;// hit the bounding limit set to whatever was
                            // last valid

        }

        Log.d(APP.TAG, "y " + y);

        lastTouchX = x;
        lastTouchY = y;

        invalidate();

        break;
    }

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

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

    case MotionEvent.ACTION_POINTER_UP: {
        // Extract the index of the pointer that left the touch sensor
        final int pointerIndex = (action & MotionEvent.ACTION_POINTER_INDEX_MASK) >> MotionEvent.ACTION_POINTER_INDEX_SHIFT;
        final int pointerId = ev.getPointerId(pointerIndex);
        if (pointerId == activePointerId) {

            final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
            lastTouchX = ev.getX(newPointerIndex);
            lastTouchY = ev.getY(newPointerIndex);
            activePointerId = ev.getPointerId(newPointerIndex);
        }
        break;
    }
    }

    return true;
}
}


我可以看到ondraw posX和posY正在改变,但屏幕上什么也没有发生。

调用
invalidate()
移动它后

我确实这样做了,我实际上通过设置平移而不是绘制画布偏移来找到答案。