Monday 21 December 2015

Scaling Object in Android OpenGL ES


Continue previous tutorial,

In the Stage class, create a global variable float dist1 and dist2, this two variable is use to calculate the ratio of scaling object with finger pointer on the phone screen

Go to the onTouchEvent method add new variable float x1, x2, y1, y2 and ratio

Next add this function inside the renderer:

public void setRatio(float scale){
r = scale;
}

Next add this code inside onTouchEvent method:

if(event.getPointerCount()==2){
if (action == MotionEvent.ACTION_POINTER_UP) {
x1 = event.getX(0);
y1 = event.getY(0);
} else {
x1 = event.getX(0);
y1 = event.getY(0);
} if (action == MotionEvent.ACTION_POINTER_DOWN) {
x2 = event.getX(1);
y2 = event.getY(1);
dist1 = (float)Math.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
} else {
x2 = event.getX(1);
y2 = event.getY(1);
dist2 = (float)Math.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
}
ratio = dist2/dist1;
mRenderer.setRatio(ratio);
requestRender();
}

Then compile it and test with your device

Video Tutorial: Click here

No comments:

Post a Comment