android : issue with the zoom
in
Android Processing
•
1 year ago
here is a simple code that shows how I used zoom (scale) in my 2D app , but :
zoom has 2 problems :
1. it doesn't translate to the center point of the fingers
2. previous zoom resets at the beginning of each frame
please help me fixing those issues !
- float zoom;
- // A vector to store the offset from the center
- String[] lines;
- int index = 0;
- float x0, x1, x2;
- float y0, y1, y2;
- float z4;
- float pointNum;
- boolean mode;
- void setup() {
- size(screenWidth, screenHeight);
- zoom = 1.0;
- }
- void draw() {
- background(255);
- pushMatrix();
- // Everything must be drawn relative to center
- translate(width/2, height/2);
- // Use scale for 2D "zoom"
- scale(zoom);
- // The offset (note how we scale according to the zoom)
- fill(255,0,0);
- rect(10,10,20,10);
- ellipse(50,60,5,8);
- rect(80,111,20,10);
- fill(0,255,0);
- ellipse(-40,-60,10,5);
- rect(-110,-10,20,10);
- }
- boolean surfaceTouchEvent(MotionEvent event) {
- pointNum=event.
getPointerCount(); - switch (event.getAction() & MotionEvent.ACTION_MASK) {
- case MotionEvent.ACTION_DOWN:
- //User is pressing on finger
- mode = false; //DRAG
- break;
- case MotionEvent.ACTION_POINTER_
DOWN: - x1=event.getX(0);
- x2=event.getX(1);
- y1=event.getY(0);
- y2=event.getY(1);
- z4 = dist(x1, y1, x2, y2);
- mode = true; // pinch
- break;
- case MotionEvent.ACTION_UP:
- mode = false;
- case MotionEvent.ACTION_POINTER_UP:
- // User is released one of the fingers.
- mode = false;
- break;
- case MotionEvent.ACTION_MOVE:
- if (mode = false) {
- // x1=event.getX(0)-x0;
- //y1=event.getY(0)-y0;
- }
- if (mode = true) {
- x1=event.getX(0);
- x2=event.getX(1);
- y1=event.getY(0);
- y2=event.getY(1);
- float z3 = dist(x1, y1, x2, y2);
- zoom = z3/z4;
- }
- break;
- }
- return super.surfaceTouchEvent(event)
; - }
1