View.setRotation導(dǎo)致內(nèi)部坐標(biāo)系旋轉(zhuǎn)導(dǎo)致問題解決方案

問題描述

使用方法 view.setRotation(float rotation) 方法旋轉(zhuǎn) view 導(dǎo)致其內(nèi)部坐標(biāo)系也發(fā)生旋轉(zhuǎn),再之后執(zhí)行 其自身的 touchEvent 拖拽移動(dòng)時(shí)出現(xiàn)極其鬼畜亂飄的問題,經(jīng)查證,是 touchEvent中的 event.getX() 與 event.getY() 返回了旋轉(zhuǎn)后坐標(biāo)系的坐標(biāo)導(dǎo)致,解決方案基本確定了采用轉(zhuǎn)換坐標(biāo)到旋轉(zhuǎn)前的坐標(biāo)系的解決方案。

解決方案

核心代碼:

private float[] coordinateTransformation(float[] xy, int r) {
    float[] result = new float[2];
    float x1 = xy[0];
    float y1 = xy[1];
    
    result[0] = (int) ((x1) * Math.cos(Math.PI / 180.0 * r) - (y1) * Math.sin(Math.PI / 180.0 * r));
    result[1] = (int) ((x1) * Math.sin(Math.PI / 180.0 * r) + (y1) * Math.cos(Math.PI / 180.0 * r));
    
    return result;
}

參數(shù):(int[event.getX(),event.getY()] , rotation)
int[event.getX(),event.getY()]:旋轉(zhuǎn)后的錯(cuò)誤坐標(biāo);
rotation:旋轉(zhuǎn)度數(shù)。

返回,原坐標(biāo)系x,y集合。

解決完成。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容