Pinch Zoom on Two Views in Single Viewgroup android
문제
I am doing a project in which i need two imageviews in single layout with zooming options. AFAIK android allows one view to make zoom easily. But my problem is not like that. I have two imageviews one over another. I've try to do it in Frame Layout. but the results are not so good. The images are shrink when it goes out of bounds of layout and the zooming is not so smooth. I have tried to do it with the deprecated Absolute layout.but it doesn't work All suggestions are welcomed.
My Code is here
int x = (int)event.getRawX();
int y = (int)event.getRawY();
ImageView view = (ImageView) v;
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
savedMatrix.set(matrix);
start.set(event.getX(), event.getY());
mode = DRAG;
hoffset = x - v.getLeft();
voffset = y - v.getTop();
break;
case MotionEvent.ACTION_POINTER_DOWN:
oldDist = spacing(event);
Log.d(TAG, "oldDist=" + oldDist);
if (oldDist > 10f) {
savedMatrix.set(matrix);
midPoint(mid, event);
mode = ZOOM;
Log.d(TAG, "mode=ZOOM");
}
break;
case MotionEvent.ACTION_UP:
// moveView(v, x - hoffset, y - voffset);
case MotionEvent.ACTION_POINTER_UP:
mode = NONE;
Log.d(TAG, "mode=NONE");
break;
case MotionEvent.ACTION_MOVE:
if (mode == DRAG) {
moveView(v, x - hoffset, y - voffset);
hrecent = x;
vrecent = y;
}
else if (mode == ZOOM) {
newDist = spacing(event);
if (newDist > 10f) {
matrix.set(savedMatrix);
float scale = newDist / oldDist;
matrix.postScale(ScaleFactorForZoom, ScaleFactorForZoom, mid.x, mid.y);
float delta =10.0f+ (newDist -oldDist)/oldDist;
// view.setImageMatrix(matrix);
float newHeight;
float newWidth;
newHeight=(float) ((delta)+view.getHeight());
newWidth=(float) ((delta)+view.getWidth());
oldDist = newDist;
zoomView(view, newWidth, newHeight);
}
}
break;
}
return true;
}
The MoveView Function is
public void moveView(View v, int x, int y)
{
ViewGroup.LayoutParams params = v.getLayoutParams();
if (params instanceof FrameLayout.LayoutParams)
{
FrameLayout.LayoutParams par = (FrameLayout.LayoutParams)params;
View parent = (View)(v.getParent());
x = Math.min(x, parent.getWidth());
x = Math.max(x, 0);
y = Math.min(y, parent.getHeight());
y = Math.max(y, 0);
par.leftMargin=x;
par.topMargin=y;
v.setLayoutParams(par);
}
}
The Zoom Function is here
public void zoomView(View v, float width, float height)
{
ViewGroup.LayoutParams params = v.getLayoutParams();
if (params instanceof FrameLayout.LayoutParams)
{
FrameLayout.LayoutParams par = (FrameLayout.LayoutParams)params;
par.width = (int)width;
par.height = (int)height;
v.setLayoutParams(par);
}
}
해결책
I did it by placing two image views above Frame Layout and write touch event for Frame Layout and put two buttons for choosing the view to zoom and set matrix for that image
다른 팁
You should not use par.width = (int) width because the fractional part is truncated by a fault always by default, instead uses par.width = Math.round (Width) and think of a mechanism to restore the default values sometimes.