我被困在這個問題中。我有一個imageView
,我想只在圖像中獲得我的觸摸位置。當我接觸圖像時,我必須顯示x=0
和y=0
。在這個應用程序,我必須知道調整大小,縮放和旋轉圖像,功能是固定的,但我只有在圖像內的圖像的座標有問題。如何在android中的imageview中獲取圖像的x和y座標?
這裏是一個鏈接,我的意思link
這是一個同樣的問題,我嘗試解決:How to get Coordinates X,Y of Image after Zoom and Pan
非常感謝。
的Ondrej
這裏是我的代碼:
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>
雖然這可能在理論上回答這個問題,但[這將是更可取的](http://meta.stackexchange.com/q/8259)在這裏包含答案的基本部分,並提供供參考的鏈接。 – 2012-07-17 22:26:37
非常感謝您的留言,但它非常廣泛。我嘗試解決同樣的問題,例如:[如何在縮放和平移後獲取圖像的座標X,Y](http://stackoverflow.com/questions/ 9093429/android-how-to-get-coordinates-xy-image-after-zoom-and-pan?s = d0f1ef64-d0d7-497b-aae8-824fe8027177#new-answer),但我不明白如何得到一個規模和一個左和頂部變量。謝謝 – user1532985 2012-07-18 23:13:22