本文介绍了Android的捏和缩放图像的活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一组图像的ListView控件。在点击其中任何一个,被点击的图像上会出现一个新的活动,并在ImageView的活动的中心,也实在是可缩放的。问题是,当我放大它的收益放大也像移动拖放的形象。我应该消除图像的运动,应该只从活动的中心缩放。
ZoomInZoomOutActivity.java
公共类ZoomInZoomOutActivity扩展活动实现OnTouchListener
{
私有静态最后字符串变量=触摸;
@燮pressWarnings(未使用)
私有静态最终浮动MIN_ZOOM = 1楼,MAX_ZOOM = 1F;
字模=新的Matrix();
矩阵savedMatrix =新的Matrix();
静态最终诠释无= 0;
静态最终诠释DRAG = 1;
静态最终诠释ZOOM = 2;
INT模式=无;
的PointF开始=新的PointF();
的PointF中期=新的PointF();
浮oldDist = 1F;
@覆盖
公共无效的onCreate(包savedInstanceState)
{
super.onCreate(savedInstanceState);
的setContentView(R.layout.imagedisp);
捆绑额外= getIntent()getExtras()。
长的id = extras.getLong(ID);
字符串s =将String.valueOf(ID);
ImageView的观点;
鉴于=(ImageView的)findViewById(R.id.imageView1);
view.setOnTouchListener(本);
如果(s.equals(0))
{
view.setImageResource(R.drawable.img1);
}
否则如果(s.equals(1))
{
view.setImageResource(R.drawable.img2);
}
否则如果(s.equals(2))
{
view.setImageResource(R.drawable.img3);
}
其他
{
view.setImageResource(R.drawable.img4);
}
}
公共布尔onTouch(视图V,MotionEvent事件)
{
ImageView的观点=(ImageView的)V;
view.setScaleType(ImageView.ScaleType.MATRIX);
浮规模;
开关(event.getAction()及MotionEvent.ACTION_MASK)
{
/ *案例MotionEvent.ACTION_DOWN:
savedMatrix.set(矩阵);
start.set(event.getX(),event.getY());
Log.d(TAG模式= DRAG);
模式=阻力;
打破; * /
案例MotionEvent.ACTION_UP:
案例MotionEvent.ACTION_POINTER_UP:
模式=无;
Log.d(TAG模式= NONE);
打破;
案例MotionEvent.ACTION_POINTER_DOWN:
oldDist =间距(事件);
Log.d(TAG,oldDist =+ oldDist);
如果(oldDist> 5F){
savedMatrix.set(矩阵);
中点(中,事件);
模式=变焦;
Log.d(TAG模式= ZOOM);
}
打破;
案例MotionEvent.ACTION_MOVE:
如果(模式==拖动)
{
matrix.set(savedMatrix);
matrix.postTranslate(event.getX() - start.x,event.getY() - start.y);
}
否则,如果(模式== ZOOM)
{
浮newDist =间距(事件);
Log.d(TAG,newDist =+ newDist);
如果(newDist> 5F)
{
matrix.set(savedMatrix);
规模= newDist / oldDist;
matrix.postScale(规模化,规模化,mid.x,mid.y);
}
}
打破;
}
view.setImageMatrix(矩阵);
返回true;
}
私人浮动间距(MotionEvent事件)
{
浮X = event.getX(0) - event.getX(1);
浮Y = event.getY(0) - event.getY(1);
返回FloatMath.sqrt(X * X + Y * Y);
}
私人无效中点(的PointF点,MotionEvent事件)
{
浮X = event.getX(0)+ event.getX(1);
浮Y = event.getY(0)+ event.getY(1);
point.set(X / 2,Y / 2);
}
}
imagedisp.xml
< XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:方向=垂直
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT
机器人:重力=中心>
< ImageView的机器人:layout_width =FILL_PARENT
机器人:ID =@ + ID / imageView1
机器人:SRC =@可绘制/ IMG1
机器人:layout_height =FILL_PARENT
机器人:scaleType =centerInside>
< / ImageView的>
< / LinearLayout中>
解决方案
试试这个code:
进口android.content.Context;
进口android.graphics.Bitmap;
进口android.graphics.Matrix;
进口android.graphics.PointF;
进口android.util.AttributeSet;
进口android.view.MotionEvent;
进口android.view.ScaleGestureDetector;
进口android.view.View;
进口android.widget.ImageView;
/ **
*扩展的Android ImageView的,包括捏缩放和平移。
* /
公共类TouchImageView扩展的ImageView
{
字模=新的Matrix();
//我们可以在这3个状态之一
静态最终诠释无= 0;
静态最终诠释DRAG = 1;
静态最终诠释ZOOM = 2;
INT模式=无;
//记住一些东西放大
的PointF最后=新的PointF();
的PointF开始=新的PointF();
浮minScale = 1F;
浮maxScale = 3F;
浮动[]米;
浮redundantXSpace,redundantYSpace;
浮动宽度,高度;
静态最终诠释CLICK = 3;
浮saveScale = 1F;
浮动右,下,origWidth,origHeight,bmWidth,bmHeight;
ScaleGestureDetector mScaleDetector;
上下文语境;
公共TouchImageView(上下文的背景下)
{
超(上下文);
sharedConstructing(上下文);
}
公共TouchImageView(上下文的背景下,ATTRS的AttributeSet)
{
超(背景下,ATTRS);
sharedConstructing(上下文);
}
私人无效sharedConstructing(上下文的背景下)
{
super.setClickable(真正的);
this.context =背景;
mScaleDetector =新ScaleGestureDetector(上下文,新ScaleListener());
matrix.setTranslate(1F,1F);
M =新的浮动[9];
setImageMatrix(矩阵);
setScaleType(ScaleType.MATRIX);
setOnTouchListener(新OnTouchListener()
{
@覆盖
公共布尔onTouch(视图V,MotionEvent事件)
{
mScaleDetector.onTouchEvent(事件);
matrix.getValues(米);
浮法X = M [Matrix.MTRANS_X]
浮动Y = M [Matrix.MTRANS_Y]
的PointF CURR =新的PointF(event.getX(),event.getY());
开关(event.getAction())
{
案例MotionEvent.ACTION_DOWN:
last.set(event.getX(),event.getY());
start.set(最后一次);
模式=阻力;
打破;
案例MotionEvent.ACTION_MOVE:
如果(模式==拖动)
{
浮DELTAX = curr.x - last.x;
漂浮移动deltaY = curr.y - last.y;
浮scaleWidth = Math.round(origWidth * saveScale);
浮scaleHeight = Math.round(origHeight * saveScale);
如果(scaleWidth<宽)
{
DELTAX = 0;
如果(Y +移动deltaY大于0)
移动deltaY = -y;
否则,如果(Y +移动deltaY<汉字)
移动deltaY = - (Y +底部);
}
否则,如果(scaleHeight<高)
{
移动deltaY = 0;
如果(X + DELTAX大于0)
DELTAX = -x;
否则,如果(X + DELTAX<右汉字)
DELTAX = - (X +右);
}
其他
{
如果(X + DELTAX大于0)
DELTAX = -x;
否则,如果(X + DELTAX<右汉字)
DELTAX = - (X +右);
如果(Y +移动deltaY大于0)
移动deltaY = -y;
否则,如果(Y +移动deltaY<汉字)
移动deltaY = - (Y +底部);
}
matrix.postTranslate(DELTAX,移动deltaY);
last.set(curr.x,curr.y);
}
打破;
案例MotionEvent.ACTION_UP:
模式=无;
INT xDiff =(INT)Math.abs(curr.x - start.x);
INT yDiff =(INT)Math.abs(curr.y - start.y);
如果(xDiff<点击&功放;&安培; yDiff<点击)
performClick();
打破;
案例MotionEvent.ACTION_POINTER_UP:
模式=无;
打破;
}
setImageMatrix(矩阵);
无效();
返回true; //表示事件已经被处理
}
});
}
@覆盖
公共无效setImageBitmap(位图BM)
{
super.setImageBitmap(BM);
如果(BM!= NULL)
{
bmWidth = bm.getWidth();
bmHeight = bm.getHeight();
}
}
公共无效setMaxZoom(浮X)
{
maxScale = X;
}
私有类ScaleListener扩展
ScaleGestureDetector.SimpleOnScaleGestureListener
{
@覆盖
公共布尔onScaleBegin(ScaleGestureDetector检测器)
{
模式=变焦;
返回true;
}
@覆盖
公共布尔onScale(ScaleGestureDetector检测器)
{
浮动mScaleFactor =(浮点)Math.min(
Math.max(.95f,detector.getScaleFactor()),1.05);
浮origScale = saveScale;
saveScale * = mScaleFactor;
如果(saveScale> maxScale)
{
saveScale = maxScale;
mScaleFactor = maxScale / origScale;
}
否则,如果(saveScale< minScale)
{
saveScale = minScale;
mScaleFactor = minScale / origScale;
}
右=宽* saveScale - 宽 - (2 * redundantXSpace * saveScale);
底部=身高* saveScale - 高度
- (2 * redundantYSpace * saveScale);
如果(origWidth * saveScale< =宽度|| origHeight * saveScale< =身高)
{
matrix.postScale(mScaleFactor,mScaleFactor,宽/ 2,高度/ 2);
如果(mScaleFactor&小于1)
{
matrix.getValues(米);
浮法X = M [Matrix.MTRANS_X]
浮动Y = M [Matrix.MTRANS_Y]
如果(mScaleFactor&小于1)
{
如果(Math.round(origWidth * saveScale)<宽)
{
如果(Y<汉字)
matrix.postTranslate(0, - (Y +底部));
否则如果(γ大于0)
matrix.postTranslate(0,-y);
}
其他
{
如果(X<右汉字)
matrix.postTranslate( - (X +右),0);
否则如果(X 0)
matrix.postTranslate(-x,0);
}
}
}
}
其他
{
matrix.postScale(mScaleFactor,mScaleFactor,detector.getFocusX(),
detector.getFocusY());
matrix.getValues(米);
浮法X = M [Matrix.MTRANS_X]
浮动Y = M [Matrix.MTRANS_Y]
如果(mScaleFactor&小于1)
{
如果(X<右汉字)
matrix.postTranslate( - (X +右),0);
否则如果(X 0)
matrix.postTranslate(-x,0);
如果(Y<汉字)
matrix.postTranslate(0, - (Y +底部));
否则如果(γ大于0)
matrix.postTranslate(0,-y);
}
}
返回true;
}
}
@覆盖
保护无效onMeasure(INT widthMeasureSpec,INT heightMeasureSpec)
{
super.onMeasure(widthMeasureSpec,heightMeasureSpec);
宽度= MeasureSpec.getSize(widthMeasureSpec);
身高= MeasureSpec.getSize(heightMeasureSpec);
//适应屏幕。
浮规模;
浮动的scaleX =(浮点)宽/(浮点)bmWidth;
浮动的scaleY =(浮点)高度/(浮点)bmHeight;
规模= Math.min(将scaleX,的scaleY);
matrix.setScale(秤,秤);
setImageMatrix(矩阵);
saveScale = 1F;
//中心图像
redundantYSpace =(浮点)高度 - (规模*(浮点)bmHeight);
redundantXSpace =(浮点)宽度 - (规模*(浮点)bmWidth);
redundantYSpace / =(浮点)2;
redundantXSpace / =(浮点)2;
matrix.postTranslate(redundantXSpace,redundantYSpace);
origWidth =宽度 - 2 * redundantXSpace;
origHeight =身高 - 2 * redundantYSpace;
右=宽* saveScale - 宽 - (2 * redundantXSpace * saveScale);
底部=身高* saveScale - 高 - (2 * redundantYSpace * saveScale);
setImageMatrix(矩阵);
}
}
I have a ListView with set of images. On clicking any of them, the clicked image will appear on a new activity and at the center of the activity in ImageView, also it is Zoomable. The Problem is when i zoom the image it gains Zoom but also moves like drag and drop. I should eliminate that movement of the image and should only be zoomable from the center of the activity.
ZoomInZoomOutActivity.java
public class ZoomInZoomOutActivity extends Activity implements OnTouchListener
{
private static final String TAG = "Touch";
@SuppressWarnings("unused")
private static final float MIN_ZOOM = 1f,MAX_ZOOM = 1f;
Matrix matrix = new Matrix();
Matrix savedMatrix = new Matrix();
static final int NONE = 0;
static final int DRAG = 1;
static final int ZOOM = 2;
int mode = NONE;
PointF start = new PointF();
PointF mid = new PointF();
float oldDist = 1f;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.imagedisp);
Bundle extras = getIntent().getExtras();
long id =extras.getLong("ID");
String s=String.valueOf(id);
ImageView view;
view = (ImageView) findViewById(R.id.imageView1);
view.setOnTouchListener(this);
if (s.equals("0"))
{
view.setImageResource(R.drawable.img1);
}
else if (s.equals("1"))
{
view.setImageResource(R.drawable.img2);
}
else if (s.equals("2"))
{
view.setImageResource(R.drawable.img3);
}
else
{
view.setImageResource(R.drawable.img4);
}
}
public boolean onTouch(View v, MotionEvent event)
{
ImageView view = (ImageView) v;
view.setScaleType(ImageView.ScaleType.MATRIX);
float scale;
switch (event.getAction() & MotionEvent.ACTION_MASK)
{
/*case MotionEvent.ACTION_DOWN:
savedMatrix.set(matrix);
start.set(event.getX(), event.getY());
Log.d(TAG, "mode=DRAG");
mode = DRAG;
break; */
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_POINTER_UP:
mode = NONE;
Log.d(TAG, "mode=NONE");
break;
case MotionEvent.ACTION_POINTER_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);
}
else if (mode == ZOOM)
{
float newDist = spacing(event);
Log.d(TAG, "newDist=" + newDist);
if (newDist > 5f)
{
matrix.set(savedMatrix);
scale = newDist / oldDist;
matrix.postScale(scale, scale, mid.x, mid.y);
}
}
break;
}
view.setImageMatrix(matrix);
return true;
}
private float spacing(MotionEvent event)
{
float x = event.getX(0) - event.getX(1);
float y = event.getY(0) - event.getY(1);
return FloatMath.sqrt(x * x + y * y);
}
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);
}
}
imagedisp.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center">
<ImageView android:layout_width="fill_parent"
android:id="@+id/imageView1"
android:src="@drawable/img1"
android:layout_height="fill_parent"
android:scaleType="centerInside">
</ImageView>
</LinearLayout>
解决方案
Try this code:
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.graphics.PointF;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
import android.view.View;
import android.widget.ImageView;
/**
* Extends Android ImageView to include pinch zooming and panning.
*/
public class TouchImageView extends ImageView
{
Matrix matrix = new Matrix();
// We can be in one of these 3 states
static final int NONE = 0;
static final int DRAG = 1;
static final int ZOOM = 2;
int mode = NONE;
// Remember some things for zooming
PointF last = new PointF();
PointF start = new PointF();
float minScale = 1f;
float maxScale = 3f;
float[] m;
float redundantXSpace, redundantYSpace;
float width, height;
static final int CLICK = 3;
float saveScale = 1f;
float right, bottom, origWidth, origHeight, bmWidth, bmHeight;
ScaleGestureDetector mScaleDetector;
Context context;
public TouchImageView(Context context)
{
super(context);
sharedConstructing(context);
}
public TouchImageView(Context context, AttributeSet attrs)
{
super(context, attrs);
sharedConstructing(context);
}
private void sharedConstructing(Context context)
{
super.setClickable(true);
this.context = context;
mScaleDetector = new ScaleGestureDetector(context, new ScaleListener());
matrix.setTranslate(1f, 1f);
m = new float[9];
setImageMatrix(matrix);
setScaleType(ScaleType.MATRIX);
setOnTouchListener(new OnTouchListener()
{
@Override
public boolean onTouch(View v, MotionEvent event)
{
mScaleDetector.onTouchEvent(event);
matrix.getValues(m);
float x = m[Matrix.MTRANS_X];
float y = m[Matrix.MTRANS_Y];
PointF curr = new PointF(event.getX(), event.getY());
switch (event.getAction())
{
case MotionEvent.ACTION_DOWN:
last.set(event.getX(), event.getY());
start.set(last);
mode = DRAG;
break;
case MotionEvent.ACTION_MOVE:
if (mode == DRAG)
{
float deltaX = curr.x - last.x;
float deltaY = curr.y - last.y;
float scaleWidth = Math.round(origWidth * saveScale);
float scaleHeight = Math.round(origHeight * saveScale);
if (scaleWidth < width)
{
deltaX = 0;
if (y + deltaY > 0)
deltaY = -y;
else if (y + deltaY < -bottom)
deltaY = -(y + bottom);
}
else if (scaleHeight < height)
{
deltaY = 0;
if (x + deltaX > 0)
deltaX = -x;
else if (x + deltaX < -right)
deltaX = -(x + right);
}
else
{
if (x + deltaX > 0)
deltaX = -x;
else if (x + deltaX < -right)
deltaX = -(x + right);
if (y + deltaY > 0)
deltaY = -y;
else if (y + deltaY < -bottom)
deltaY = -(y + bottom);
}
matrix.postTranslate(deltaX, deltaY);
last.set(curr.x, curr.y);
}
break;
case MotionEvent.ACTION_UP:
mode = NONE;
int xDiff = (int) Math.abs(curr.x - start.x);
int yDiff = (int) Math.abs(curr.y - start.y);
if (xDiff < CLICK && yDiff < CLICK)
performClick();
break;
case MotionEvent.ACTION_POINTER_UP:
mode = NONE;
break;
}
setImageMatrix(matrix);
invalidate();
return true; // indicate event was handled
}
});
}
@Override
public void setImageBitmap(Bitmap bm)
{
super.setImageBitmap(bm);
if (bm != null)
{
bmWidth = bm.getWidth();
bmHeight = bm.getHeight();
}
}
public void setMaxZoom(float x)
{
maxScale = x;
}
private class ScaleListener extends
ScaleGestureDetector.SimpleOnScaleGestureListener
{
@Override
public boolean onScaleBegin(ScaleGestureDetector detector)
{
mode = ZOOM;
return true;
}
@Override
public boolean onScale(ScaleGestureDetector detector)
{
float mScaleFactor = (float) Math.min(
Math.max(.95f, detector.getScaleFactor()), 1.05);
float origScale = saveScale;
saveScale *= mScaleFactor;
if (saveScale > maxScale)
{
saveScale = maxScale;
mScaleFactor = maxScale / origScale;
}
else if (saveScale < minScale)
{
saveScale = minScale;
mScaleFactor = minScale / origScale;
}
right = width * saveScale - width - (2 * redundantXSpace * saveScale);
bottom = height * saveScale - height
- (2 * redundantYSpace * saveScale);
if (origWidth * saveScale <= width || origHeight * saveScale <= height)
{
matrix.postScale(mScaleFactor, mScaleFactor, width / 2, height / 2);
if (mScaleFactor < 1)
{
matrix.getValues(m);
float x = m[Matrix.MTRANS_X];
float y = m[Matrix.MTRANS_Y];
if (mScaleFactor < 1)
{
if (Math.round(origWidth * saveScale) < width)
{
if (y < -bottom)
matrix.postTranslate(0, -(y + bottom));
else if (y > 0)
matrix.postTranslate(0, -y);
}
else
{
if (x < -right)
matrix.postTranslate(-(x + right), 0);
else if (x > 0)
matrix.postTranslate(-x, 0);
}
}
}
}
else
{
matrix.postScale(mScaleFactor, mScaleFactor, detector.getFocusX(),
detector.getFocusY());
matrix.getValues(m);
float x = m[Matrix.MTRANS_X];
float y = m[Matrix.MTRANS_Y];
if (mScaleFactor < 1)
{
if (x < -right)
matrix.postTranslate(-(x + right), 0);
else if (x > 0)
matrix.postTranslate(-x, 0);
if (y < -bottom)
matrix.postTranslate(0, -(y + bottom));
else if (y > 0)
matrix.postTranslate(0, -y);
}
}
return true;
}
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
width = MeasureSpec.getSize(widthMeasureSpec);
height = MeasureSpec.getSize(heightMeasureSpec);
// Fit to screen.
float scale;
float scaleX = (float) width / (float) bmWidth;
float scaleY = (float) height / (float) bmHeight;
scale = Math.min(scaleX, scaleY);
matrix.setScale(scale, scale);
setImageMatrix(matrix);
saveScale = 1f;
// Center the image
redundantYSpace = (float) height - (scale * (float) bmHeight);
redundantXSpace = (float) width - (scale * (float) bmWidth);
redundantYSpace /= (float) 2;
redundantXSpace /= (float) 2;
matrix.postTranslate(redundantXSpace, redundantYSpace);
origWidth = width - 2 * redundantXSpace;
origHeight = height - 2 * redundantYSpace;
right = width * saveScale - width - (2 * redundantXSpace * saveScale);
bottom = height * saveScale - height - (2 * redundantYSpace * saveScale);
setImageMatrix(matrix);
}
}
这篇关于Android的捏和缩放图像的活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!