我正在做一个项目,其中我需要两个带有缩放选项的单一布局的图像视图。AFAIK android 允许一个视图轻松进行缩放。但我的问题不是这样的。我有两个图像视图一个接一个。我尝试在框架布局中做到这一点。但结果并不是那么好。当图像超出布局范围并且缩放不那么平滑时,图像会缩小。我尝试使用已弃用的绝对布局来做到这一点。但它不起作用欢迎所有建议。
我的代码在这里
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;
}
MoveView 函数是
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);
}
}
缩放功能在这里
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);
}
}