android 坐标 获取展示图片_如何在Android中获取imageview的所有x和y坐标?

该博客讨论了如何在Android应用中获取ImageView在屏幕上移动时的准确x和y坐标。作者通过创建一个OnTouchListener监听图像视图的触摸事件,记录ACTION_DOWN、ACTION_MOVE和ACTION_POINTER_DOWN等状态,使用Matrix进行平移和缩放操作,并尝试获取图像视图的位置。然而,作者遇到了获取坐标始终为0和1的问题。
摘要由CSDN通过智能技术生成

我正在实现一个应用程序,用于在我移动屏幕上的图像位置时获取图像视图的正确x和y位置以及图像视图的x和y坐标.当我运行应用程序时,当我移动我的图像视图时,它将只获得0和1.有没有办法找出x和y位置?

这是我的主要活动类:

public class MainActivity 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[4];

@Override

public void onCreate(Bundle savedInstanceState)

{

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

final ImageView image = (ImageView) findViewById(R.id.ImageViewOne);

final Bitmap icon = BitmapFactory.decodeResource(this.getResources(),

R.drawable.yellowrose);

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;

System.out.println("mode = drag");

RectF r = new RectF();

matrix.mapRect(r);

break;

case MotionEvent.ACTION_UP:

case MotionEvent.ACTION_POINTER_UP:

mode = NONE;

System.out.println("mode = none = " + mode);

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;

System.out.println("scale " + scale);

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;

System.out.println("mode " + mode);

}

break;

}

// Perform the transformation

view.setImageMatrix(matrix);

return true;

}

});

}

private float spacing(MotionEvent event)

{

float x = event.getRawX() - event.getRawX();

System.out.println("spacing x" + x);

float y = event.getRawY() - event.getRawY();

System.out.println("spacing y" + y);

return FloatMath.sqrt(x * x + y * y);

}

private void midPoint(PointF point, MotionEvent event)

{

float x = event.getRawX() + event.getRawX();

System.out.println("midpoint x" + x);

float y = event.getRawY() + event.getRawY();

System.out.println("midpoint y" + y);

point.set(x / 2, y / 2);

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值