我陷入了这个问题。我有一个imageView
,我想得到我的触摸只在图像内的位置。当我触摸出图像,我必须显示x=0
和y=0
。在这个应用程序中,我必须知道调整大小,缩放和旋转图像,有功能是固定的,但我有问题的图像坐标只在图像内。
这里有一个链接,我指的是link
这是我试图解决的同一个问题:如何获取图像缩放平移后的X、Y坐标
多谢了。
翁德雷
下面是我的代码:
public class Sample extends Activity{
static final int NONE = 0;
static final int DRAG = 1;
static final int ZOOM = 2;
int mode = NONE;
Matrix matrix = new Matrix();
Matrix savedMatrix = new Matrix();
PointF start = new PointF();
PointF mid = new PointF();
float oldDist = 1f;
int[] location = new int[2];
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final ImageView image = (ImageView) findViewById(R.id.mapView);
final Bitmap icon = BitmapFactory.decodeResource(this.getResources(),
R.drawable.mapa);
image.setImageBitmap(icon);
image.setOnTouchListener(new View.OnTouchListener()
{
public boolean onTouch(View v, MotionEvent event)
{
final 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;
RectF r = new RectF();
matrix.mapRect(r);
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_POINTER_UP:
mode = NONE;
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);
if (newDist > 10f)
{
matrix.set(savedMatrix);
float scale = newDist / oldDist;
matrix.postScale(scale, scale, mid.x, mid.y);
}
}
break;
case MotionEvent.ACTION_POINTER_DOWN:
oldDist = spacing(event);
if (oldDist > 10f)
{
savedMatrix.set(matrix);
midPoint(mid, event);
mode = ZOOM;
}
break;
}
// Perform the transformation
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);
}
main.xml看起来像这样:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:id="@+id/mapView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="right"
android:adjustViewBounds="true"
android:scaleType="matrix" >
</ImageView>
</FrameLayout>
2条答案
按热度按时间hgqdbh6s1#
我不太明白你在问什么,但我还是会尝试回答。听起来你想使用
getRawX()
和getRawY()
来获得屏幕上的触摸位置,而不是视图上的。你可能还必须为ImageView
之外的任何内容覆盖onTouchEvent
方法。vwhgwdsa2#
看一看
触摸事件(安卓系统、视图、运动事件)
以及
http://developer.android.com/reference/android/view/MotionEvent.html