0

我有这个 :
源案例

而我真正想要的是通过带有 OnTouchListener 的对象的坐标(X,Y)显示位图的一部分(橙色正方形,中心有点)。

所以问题是我想绘制图像的一部分,就像它在图像上显示的那样(红色正方形“我想要显示的区域”)。
所以在这种情况下,例外结果是(位图的一部分): 目前我正在这样做:
异常结果

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;
}
}

这是不正确的。

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

PS 据我了解,event.getX() / event.getY()正在获得相对坐标(我不清楚,从带有触摸监听器的 imageview 对象中得到的坐标到底是什么(橙色正方形,中心点为圆点),我的意思是它获取此对象的中心或Top.Left角(X,Y))?

4

4 回答 4

0

你必须得到这样的坐标,它会给你参考视图而不是屏幕的坐标。

                float xCord = event.getRawX()-v.getX();
                float yCord = event.getRawY()-v.getY();
于 2015-12-03T11:31:04.827 回答
0

遗憾的是,如果您想真正解决视图范围内的事件,则没有简单的方法可以解决此问题。这仍然在某些硬件上的 Android Pie 上发生。解决奇数触摸事件的唯一方法是在渲染视图位置之前比较最后 3 个事件(如果在 ACTION_MOVE 期间没有奇数移动)。下面是在圆形视图上检查事件时的示例。假设您创建了一个新的 double[3]临时变量:

  /**
     * This detects if your finger movement is a result of an actual raw touch event of if it's from a view jitter.
     * This uses 3 events of rotation as temporary storage so that differentiation between swapping touch events can be determined.
     *
     * @param newRotationValue
     */
    private boolean isRotationTouchEventConsistent(final double newRotationValue) {
        double evalValue = newRotationValue;

        if (Double.compare(newRotationStore[2], newRotationStore[1]) != 0) {
            newRotationStore[2] = newRotationStore[1];
        }
        if (Double.compare(newRotationStore[1], newRotationStore[0]) != 0) {
            newRotationStore[1] = newRotationStore[0];
        }

        newRotationStore[0] = evalValue;

        if (Double.compare(newRotationStore[2], newRotationStore[0]) == 0
                || Double.compare(newRotationStore[1], newRotationStore[0]) == 0
                || Double.compare(newRotationStore[2], newRotationStore[1]) == 0

                //Is the middle event the odd one out
                || (newRotationStore[0] > newRotationStore[1] && newRotationStore[1] < newRotationStore[2])
                || (newRotationStore[0] < newRotationStore[1] && newRotationStore[1] > newRotationStore[2])
        ) {
            return false;
        }
        return true;
    }
于 2019-01-30T10:33:36.070 回答
0

抱歉,在更详细地寻找解决方案时,我想我找到了它并且我正在工作,所以你可以试试这个:

event.getX()不使用view.getX()。您正在触摸的视图的view.getX()返回位置,而不是您感兴趣的触摸事件。

这是可能会反转的 Y 坐标。

event.getX()触摸事件的位置也是如此。

event.getY()是倒置的 Y 位置,所以getHeight() - event.getY()会给你你所追求的 Y 位置。

编辑

在 MOTION_MOVE 的情况下,getX 和 getY 返回最新数据并保留历史数据。我想最近的一个是最有价值的,因为那是你要画的地方。

在这种情况下,请使用文档中的批处理报价:

批处理 - http://developer.android.com/reference/android/view/MotionEvent.html

为提高效率,带有 ACTION_MOVE 的运动事件可以将单个对象内的多个运动样本批处理在一起。使用 getX(int) 和 getY(int) 可获得最新的指针坐标。使用 getHistoricalX(int, int) 和 getHistoricalY(int, int) 访问批次中较早的坐标。只有在比批次中的当前坐标更早的情况下,坐标才是“历史的”;但是,它们仍然不同于先前运动事件中报告的任何其他坐标。要按时间顺序处理批处理中的所有坐标,首先消耗历史坐标,然后消耗当前坐标。

于 2015-12-03T11:07:15.730 回答
0

所以解决这个问题很简单。
解决方案
我有一个带有 OnTouchListener 的对象(橙色正方形)(例如,他是一个大小为 50dp高度/50dp宽度的四边形)。
因此view.getX()/view.getY()采用对象(具有 OnTouchListener)的相对坐标(中心)。
所以我需要什么:

//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);  

仅此而已。
享受!

于 2015-12-14T09:49:58.600 回答