我正在做一個項目,其中需要使用縮放選項的單個佈局中的兩個圖像視圖。 AFAIK android允許一個視圖輕鬆進行縮放。但我的問題不是那樣的。我有兩個圖像瀏覽。我試圖在框架佈局中做到這一點。但結果並不好。當圖像超出佈局範圍並且縮放不太平滑時,圖像會縮小。我試圖用棄絕的絕對佈局來做。但它不起作用 歡迎您提供所有建議。在單個視圖組中縮放兩個視圖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);
}
}
如果發現請發佈完整的解決方案。這對某人會有所幫助。 –