2014-07-10 40 views
1

我有一個班級,我希望能夠在觸摸時在屏幕上移動。當我用它作爲imageview而不是佈局時它工作。是否onDraw有不同的表現對於常規的佈局,而不是一個imageview的試圖製作一個可移動的視圖,使用位圖而不是佈局

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和波西正在改變,但沒有任何反應在屏幕上。

+0

我做的,我居然通過設置翻譯,而不是畫在畫布上的偏移想出了一個答案。 – Brian

回答

0

通話invalidate()你移動後

相關問題