我有一个图像视图并使用位图显示图像。捏缩放由矩阵完成。对于缩放,我使用“ Matrix.postScale ”方法。在平移(拖动)图像时,我需要限制拖动。当它到达图像的末尾时,我需要停止拖动。
虽然缩放图像得到扩展,但图像视图的高度和宽度以及位图保持不变。拖动时到达图像末尾时如何找到。
我在下面给出的代码和 xml。
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/framlayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#000000"
android:orientation="vertical" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.70"
android:layout_gravity="center"
android:background="#000000"
android:scaleType="matrix"
/>
Java代码在这里
public boolean onTouch(View v, MotionEvent event) {
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;
break;
case MotionEvent.ACTION_POINTER_DOWN:
oldDist = spacing(event);
if (oldDist > 10f) {
savedMatrix.set(matrix);
midPoint(mid, event);
mode = ZOOM;
}
break;
case MotionEvent.ACTION_UP:
if(!IsZoomMode){
matrix.set(savedMatrix);
matrix.postTranslate(0,0);
}
break;
case MotionEvent.ACTION_POINTER_UP:
mode = NONE;
break;
case MotionEvent.ACTION_MOVE:
if(nTimerCountM < 30 && !bEnabledTaggingM)
{
nTimerCountM = 0;
if (mode == DRAG) {
matrix.set(savedMatrix);
matrix.postTranslate(event.getX() - start.x, event.getY()
- start.y);
} else if (mode == ZOOM) {
IsZoomMode = true;
float newDist = spacing(event);
if (newDist > 10f) {
matrix.set(savedMatrix);
float scale = newDist / oldDist;
matrix.postScale(scale, scale, mid.x, mid.y);
}
}
}
break;
}
view.setImageMatrix(matrix);
}
return true;
}
/** Determine the space between the first two fingers */
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);
}
/** Calculate the mid point of the first 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);