Android : Trying to make a movable view, works with bitmap not as a layout

on Wednesday, July 9, 2014


I have a class that I want to be able to move around the screen when you touch. It worked when I used it as an imageview but not as a layout. Does onDraw behave differently for a regular layout as opposed to an 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;
}


}


I can see that ondraw posX and posY are changing but nothing happens on the screen.


0 comments:

Post a Comment