簡體   English   中英

如何使用OnTouchListener獲取對象的正確坐標(event.getX()/ event.getY())

[英]How to get correct coords(event.getX() / event.getY()) of Object with OnTouchListener

我有這個 :
源案例

我確切想要的是通過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()正在獲得相對坐標(對我來說還不清楚,從帶有觸摸監聽器的圖像視圖對象中獲得的確切坐標是什么(中心帶有點的橙色正方形),我的意思是獲取此對象的中心Top.Left corners(X,Y))?

抱歉,在詳細了解解決方案時,我認為我找到了它,並且我正在工作,所以您可以嘗試以下方法:

使用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)訪問批處理中的較早坐標。 坐標僅在其早於批處理中的當前坐標的范圍內才是“歷史的”。 但是,它們仍然不同於先前運動事件中報告的任何其他坐標。 要按時間順序處理批處理中的所有坐標,請首先使用歷史坐標,然后使用當前坐標。

您必須像這樣獲得坐標,它將為您提供參考以查看屏幕而不是屏幕。

                float xCord = event.getRawX()-v.getX();
                float yCord = event.getRawY()-v.getY();

因此,解決此問題很簡單。
解決方案
我有一個帶有OnTouchListener的對象( 橙色正方形 )(例如,他是一個大小為50dp Height / 50dp Width的四邊形)。
因此, 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);  

僅此而已。
請享用!

可悲的是,如果您想在視圖范圍內真正解決該事件,沒有簡單的解決方法。 在某些硬件上的Android Pie上仍然會發生這種情況。 解決奇數觸摸事件的唯一方法是,如果在ACTION_MOVE期間沒有奇異動作,則比較最后3個事件,然后再放置視圖位置。 以下是在圓形視圖上檢查事件的示例。 假設您創建了一個新的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;
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM