本文介绍了如何获得对象的正确COORDS(event.getX()/ event.getY())与OnTouchListener的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的:结果

和正是我想要的是通过 COORDS 的对象(X,Y)与OnTouchListener(橙色与中心点)显示位图的一部分。

and what i want exactly is that to show portion of Bitmap via coords(X,Y) of object with OnTouchListener(orange square with dot in center).

所以,问题是,我想画的图像部分像它显示了图像(红方区,我想,以示)。结果
所以在此情况下,例外的结果是(位图的一部分):结果,
结果
在当前时刻我做这样的:

So the problem is that i want to draw portion of image like it shows on image(Red square "Area that i want" to show).
So in this case,excepted result is(portion of bitmap) :

At current moment i'm doing like this:

public boolean onTouch(View view, MotionEvent event) {

    switch (event.getAction()) {
        case MotionEvent.ACTION_MOVE:

            Bitmap mBitmap = Bitmap.createBitmap(sourceBitmap,view.getX() - view.getWidth(),view.getY()-view.getHeight(),250,250);
            //other stuff to fill this portion of bitmap
            break;
    }

    return true;
}
}

和它的不正确的。

我怎样才能做到这一点?
尝试了很多公式,但没有运气。有什么建议么?

how can i achieve this?Tried a lot of formulas,but no luck. Any suggestions?

PS我的理解 event.getX() / event.getY()越来越相对COORDS(和它的不明确对我来说,究竟COORDS从对象获取带触摸监听器(与中心点橙色正方形),我的意思是它获得的中心这个物体或的ImageView的 Top.Left 角(X,Y))?

PS As i understand event.getX()/event.getY() is getting relative coords(and it's not clear for me,what exactly coords getting from object of imageview with touch listener(orange square with dot in center),i mean its getting center of this objects or Top.Left corners(X,Y)) ?

推荐答案

所以这是简单的解决这个问题。结果
解决方案:结果
我有一个对象(橙色正方形)与OnTouchListener(例如用于他与大小50dp 高度的四 / 50dp 宽度)。结果
因此, view.getX()/ view.getY()接受对象的相对COORDS(中心)(有OnTouchListener)。结果
所以我需要什么:

So it was simple to resolve this problem.
Solution:
I have an object(orange square) with OnTouchListener(for e.g. he's an quad with size 50dp Height/ 50dp Width).
So view.getX()/view.getY() takes relative coords(center) of object(that have OnTouchListener).
So what i needed:

//imageview parameters ,where i will show this part of bitmap
int Width = img.getWidth();
int Height = img.getHeight();
//after this,i make margin of my coords,via this simple formula -> Current Coord X - Width / 2 (same for Y)
 Bitmap mBitmap = Bitmap.createBitmap(sourceBitmap,view.getX() - Width / 2,view.getY()-Height / 2,Width,Height);

这就是所有。结果
尽情享受吧!

And that is all.
Enjoy!

这篇关于如何获得对象的正确COORDS(event.getX()/ event.getY())与OnTouchListener的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-18 02:47