gpt4 book ai didi

java - 如何在缩小时将图像返回到原始位置 - Android

转载 作者:行者123 更新时间:2023-12-01 19:40:55 24 4
gpt4 key购买 nike

当我缩小图像并且图像比例小于第一个初始图像尺寸时,我想将图像返回到原始位置

@Override
public boolean onTouch(View v, MotionEvent event)
{
ImageView view = (ImageView) v;
view.setScaleType(ImageView.ScaleType.MATRIX);
float scale = 0;

dumpEvent(event);
// Handle touch events here...

switch (event.getAction() & MotionEvent.ACTION_MASK)
{
case MotionEvent.ACTION_DOWN: // first finger down only
savedMatrix.set(matrix);
start.set(event.getX(), event.getY());
Log.d(TAG, "mode=DRAG"); // write to LogCat
mode = DRAG;
break;

/*case MotionEvent.ACTION_UP: // first finger lifted

case MotionEvent.ACTION_POINTER_UP: // second finger lifted

mode = NONE;
Log.d(TAG, "mode=NONE");
break;*/

case MotionEvent.ACTION_UP: // first finger lifted
mode = NONE;
break;
case MotionEvent.ACTION_POINTER_UP:// second finger lifted
/*matrix.set(savedMatrix);*/
/*view.setScaleType(ImageView.ScaleType.CENTER);*/
Log.v("/h/", ""+scale);

mode = ZOOMOUT;


//When release back to position
matrix.set(savedMatrix);
Log.d(TAG, "mode=ZOOMOUT");
break;

case MotionEvent.ACTION_POINTER_DOWN: // first and second finger down

oldDist = spacing(event);
Log.d(TAG, "oldDist=" + oldDist);
if (oldDist > 5f) {
savedMatrix.set(matrix);
midPoint(mid, event);
mode = ZOOM;
Log.d(TAG, "mode=ZOOM");
}
break;

case MotionEvent.ACTION_MOVE:

if (mode == DRAG)
{
matrix.set(savedMatrix);
matrix.postTranslate(event.getX() - start.x, event.getY() - start.y); // create the transformation in the matrix of points
}
else if (mode == ZOOM)
{
// pinch zooming
float newDist = spacing(event);
Log.d(TAG, "newDist=" + newDist);

if (newDist > 5f)
{
matrix.set(savedMatrix);
scale = newDist / oldDist; // setting the scaling of the
// matrix...if scale > 1 means
// zoom in...if scale < 1 means
// zoom out
if(scale>1) {
//Zoom In
}else if(scale<1) {
//Zoom Out
}


matrix.postScale(scale, scale, mid.x, mid.y);

Log.v("/h/", ""+scale+mode);

}

}


break;

}

view.setImageMatrix(matrix); // display the transformation on screen

return true; // indicate event was handled
}

/*
* --------------------------------------------------------------------------
* Method: spacing Parameters: MotionEvent Returns: float Description:
* checks the spacing between the two fingers on touch
* ----------------------------------------------------
*/

private float spacing(MotionEvent event)
{
float x = event.getX(0) - event.getX(1);
float y = event.getY(0) - event.getY(1);
return (float) Math.sqrt(x * x + y * y);
}

/*
* --------------------------------------------------------------------------
* Method: midPoint Parameters: PointF object, MotionEvent Returns: void
* Description: calculates the midpoint between the two fingers
* ------------------------------------------------------------
*/

private void midPoint(PointF point, MotionEvent event)
{
float x = event.getX(0) + event.getX(1);
float y = event.getY(0) + event.getY(1);
point.set(x / 2, y / 2);
}

最佳答案

  • 在 ACTION_DOWN 时将 savingMatrix 设置为 view.getImageMatrix()
  • 在单独的矩阵上进行缩放/拖动更改
  • 在 ACTION_UP 或 ACTION_POINTER_UP 上将原始矩阵设置回 ImageView 。view.setImageMatrix(savedMatrix)

关于java - 如何在缩小时将图像返回到原始位置 - Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59175339/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com