猿问

Android图像视图捏缩放

我正在使用Makeing Sense of Multitouch中的代码示例 来缩放图像视图。在ScaleListener上,我添加ScaleGestureDetector.getFocusX() and getFocusY()了内容以缩放手势的焦点。一切正常。


问题是,在第一次多点触摸时,整个图像绘制位置都将更改为当前接触点,并从此处进行缩放。您能帮我解决这个问题吗?


这是我的TouchImageView代码示例。

以及我在活动中如何使用它。


ImageView imageView = (ImageView) findViewById(R.id.imgView);


int hMargin = (int) (displayMetrics.widthPixels * .10);

int vMargin = (int) (displayMetrics.heightPixels * .10);


RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(displayMetrics.widthPixels - (hMargin * 2), (int)(displayMetrics.heightPixels - btnCamera.getHeight()) - (vMargin * 2));

params.leftMargin = hMargin;

params.topMargin =  vMargin;

imageView.setLayoutParams(params);

imageView.setImageDrawable(drawable);


PIPIONE
浏览 351回答 3
3回答

慕姐4208626

我捏捏缩放以制作自己的自定义图像视图。Chirag Raval的代码没有限制/限制,因此用户可以将图像拖出屏幕。这将解决它。这是CustomImageView类:&nbsp; &nbsp; public class CustomImageVIew extends ImageView implements OnTouchListener {&nbsp; &nbsp; private Matrix matrix = new Matrix();&nbsp; &nbsp; private Matrix savedMatrix = new Matrix();&nbsp; &nbsp; static final int NONE = 0;&nbsp; &nbsp; static final int DRAG = 1;&nbsp; &nbsp; static final int ZOOM = 2;&nbsp; &nbsp; private int mode = NONE;&nbsp; &nbsp; private PointF mStartPoint = new PointF();&nbsp; &nbsp; private PointF mMiddlePoint = new PointF();&nbsp; &nbsp; private Point mBitmapMiddlePoint = new Point();&nbsp; &nbsp; private float oldDist = 1f;&nbsp; &nbsp; private float matrixValues[] = {0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f};&nbsp; &nbsp; private float scale;&nbsp; &nbsp; private float oldEventX = 0;&nbsp; &nbsp; private float oldEventY = 0;&nbsp; &nbsp; private float oldStartPointX = 0;&nbsp; &nbsp; private float oldStartPointY = 0;&nbsp; &nbsp; private int mViewWidth = -1;&nbsp; &nbsp; private int mViewHeight = -1;&nbsp; &nbsp; private int mBitmapWidth = -1;&nbsp; &nbsp; private int mBitmapHeight = -1;&nbsp; &nbsp; private boolean mDraggable = false;&nbsp; &nbsp; public CustomImageVIew(Context context) {&nbsp; &nbsp; &nbsp; &nbsp; this(context, null, 0);&nbsp; &nbsp; }&nbsp; &nbsp; public CustomImageVIew(Context context, AttributeSet attrs) {&nbsp; &nbsp; &nbsp; &nbsp; this(context, attrs, 0);&nbsp; &nbsp; }&nbsp; &nbsp; public CustomImageVIew(Context context, AttributeSet attrs, int defStyle) {&nbsp; &nbsp; &nbsp; &nbsp; super(context, attrs, defStyle);&nbsp; &nbsp; &nbsp; &nbsp; this.setOnTouchListener(this);&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public void onSizeChanged (int w, int h, int oldw, int oldh){&nbsp; &nbsp; &nbsp; &nbsp; super.onSizeChanged(w, h, oldw, oldh);&nbsp; &nbsp; &nbsp; &nbsp; mViewWidth = w;&nbsp; &nbsp; &nbsp; &nbsp; mViewHeight = h;&nbsp; &nbsp; }&nbsp; &nbsp; public void setBitmap(Bitmap bitmap){&nbsp; &nbsp; &nbsp; &nbsp; if(bitmap != null){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; setImageBitmap(bitmap);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mBitmapWidth = bitmap.getWidth();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mBitmapHeight = bitmap.getHeight();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mBitmapMiddlePoint.x = (mViewWidth / 2) - (mBitmapWidth /&nbsp; 2);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mBitmapMiddlePoint.y = (mViewHeight / 2) - (mBitmapHeight / 2);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; matrix.postTranslate(mBitmapMiddlePoint.x, mBitmapMiddlePoint.y);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.setImageMatrix(matrix);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public boolean onTouch(View v, MotionEvent event){&nbsp; &nbsp; &nbsp; &nbsp; switch (event.getAction() & MotionEvent.ACTION_MASK) {&nbsp; &nbsp; &nbsp; &nbsp; case MotionEvent.ACTION_DOWN:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; savedMatrix.set(matrix);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mStartPoint.set(event.getX(), event.getY());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mode = DRAG;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; case MotionEvent.ACTION_POINTER_DOWN:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; oldDist = spacing(event);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(oldDist > 10f){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; savedMatrix.set(matrix);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; midPoint(mMiddlePoint, event);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mode = ZOOM;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; case MotionEvent.ACTION_UP:&nbsp; &nbsp; &nbsp; &nbsp; case MotionEvent.ACTION_POINTER_UP:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mode = NONE;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; case MotionEvent.ACTION_MOVE:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(mode == DRAG){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; drag(event);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else if(mode == ZOOM){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; zoom(event);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return true;&nbsp; &nbsp; }&nbsp; &nbsp;public void drag(MotionEvent event){&nbsp; &nbsp; &nbsp; &nbsp;matrix.getValues(matrixValues);&nbsp; &nbsp; &nbsp; &nbsp;float left = matrixValues[2];&nbsp; &nbsp; &nbsp; &nbsp;float top = matrixValues[5];&nbsp; &nbsp; &nbsp; &nbsp;float bottom = (top + (matrixValues[0] * mBitmapHeight)) - mViewHeight;&nbsp; &nbsp; &nbsp; &nbsp;float right = (left + (matrixValues[0] * mBitmapWidth)) -mViewWidth;&nbsp; &nbsp; &nbsp; &nbsp;float eventX = event.getX();&nbsp; &nbsp; &nbsp; &nbsp;float eventY = event.getY();&nbsp; &nbsp; &nbsp; &nbsp;float spacingX = eventX - mStartPoint.x;&nbsp; &nbsp; &nbsp; &nbsp;float spacingY = eventY - mStartPoint.y;&nbsp; &nbsp; &nbsp; &nbsp;float newPositionLeft = (left&nbsp; < 0 ? spacingX : spacingX * -1) + left;&nbsp; &nbsp; &nbsp; &nbsp;float newPositionRight = (spacingX) + right;&nbsp; &nbsp; &nbsp; &nbsp;float newPositionTop = (top&nbsp; < 0 ? spacingY : spacingY * -1) + top;&nbsp; &nbsp; &nbsp; &nbsp;float newPositionBottom = (spacingY) + bottom;&nbsp; &nbsp; &nbsp; &nbsp;boolean x = true;&nbsp; &nbsp; &nbsp; &nbsp;boolean y = true;&nbsp; &nbsp; &nbsp; &nbsp;if(newPositionRight < 0.0f || newPositionLeft > 0.0f){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if(newPositionRight < 0.0f && newPositionLeft > 0.0f){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;x = false;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;} else{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;eventX = oldEventX;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;mStartPoint.x = oldStartPointX;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp;if(newPositionBottom < 0.0f || newPositionTop > 0.0f){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if(newPositionBottom < 0.0f && newPositionTop > 0.0f){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;y = false;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;} else{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;eventY = oldEventY;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;mStartPoint.y = oldStartPointY;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp;if(mDraggable){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;matrix.set(savedMatrix);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;matrix.postTranslate(x? eventX - mStartPoint.x : 0, y? eventY - mStartPoint.y : 0);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;this.setImageMatrix(matrix);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if(x)oldEventX = eventX;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if(y)oldEventY = eventY;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if(x)oldStartPointX = mStartPoint.x;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if(y)oldStartPointY = mStartPoint.y;&nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp;}&nbsp; &nbsp;public void zoom(MotionEvent event){&nbsp; &nbsp; &nbsp; &nbsp;matrix.getValues(matrixValues);&nbsp; &nbsp; &nbsp; &nbsp;float newDist = spacing(event);&nbsp; &nbsp; &nbsp; &nbsp;float bitmapWidth = matrixValues[0] * mBitmapWidth;&nbsp; &nbsp; &nbsp; &nbsp;float bimtapHeight = matrixValues[0] * mBitmapHeight;&nbsp; &nbsp; &nbsp; &nbsp;boolean in = newDist > oldDist;&nbsp; &nbsp; &nbsp; &nbsp;if(!in && matrixValues[0] < 1){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return;&nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp;if(bitmapWidth > mViewWidth || bimtapHeight > mViewHeight){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;mDraggable = true;&nbsp; &nbsp; &nbsp; &nbsp;} else{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;mDraggable = false;&nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp;float midX = (mViewWidth / 2);&nbsp; &nbsp; &nbsp; &nbsp;float midY = (mViewHeight / 2);&nbsp; &nbsp; &nbsp; &nbsp;matrix.set(savedMatrix);&nbsp; &nbsp; &nbsp; &nbsp;scale = newDist / oldDist;&nbsp; &nbsp; &nbsp; &nbsp;matrix.postScale(scale, scale, bitmapWidth > mViewWidth ? mMiddlePoint.x : midX, bimtapHeight > mViewHeight ? mMiddlePoint.y : midY);&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;this.setImageMatrix(matrix);&nbsp; &nbsp;}&nbsp; &nbsp; /** Determine the space between the first two fingers */&nbsp; &nbsp; private float spacing(MotionEvent event) {&nbsp; &nbsp; &nbsp; &nbsp; float x = event.getX(0) - event.getX(1);&nbsp; &nbsp; &nbsp; &nbsp; float y = event.getY(0) - event.getY(1);&nbsp; &nbsp; &nbsp; &nbsp; return (float)Math.sqrt(x * x + y * y);&nbsp; &nbsp; }&nbsp; &nbsp; /** Calculate the mid point of the first two fingers */&nbsp; &nbsp; private void midPoint(PointF point, MotionEvent event) {&nbsp; &nbsp; &nbsp; &nbsp; float x = event.getX(0) + event.getX(1);&nbsp; &nbsp; &nbsp; &nbsp; float y = event.getY(0) + event.getY(1);&nbsp; &nbsp; &nbsp; &nbsp; point.set(x / 2, y / 2);&nbsp; &nbsp; }}这是在活动中使用它的方式:CustomImageVIew mImageView = (CustomImageVIew)findViewById(R.id.customImageVIew1);mImage.setBitmap(your bitmap);和布局:<your.package.name.CustomImageVIew&nbsp; &nbsp; &nbsp; &nbsp; android:id="@+id/customImageVIew1"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_width="fill_parent"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_height="fill_parent"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_marginBottom="15dp"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_marginLeft="15dp"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_marginRight="15dp"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_marginTop="15dp"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_centerHorizontal="true"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_centerVertical="true"&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; android:scaleType="matrix"/> // important

繁星coding

在build.gradle中添加以下行:compile 'com.commit451:PhotoView:1.2.4'要么compile 'com.github.chrisbanes:PhotoView:1.3.0'在Java文件中:PhotoViewAttacher photoAttacher;photoAttacher= new PhotoViewAttacher(Your_Image_View);photoAttacher.update();
随时随地看视频慕课网APP

相关分类

Android
我要回答