大道之行,天下为公。这篇文章主要讲述Android获取两条线之间的夹角度数相关的知识,希望能为你提供帮助。
核心代码【Android获取两条线之间的夹角度数】centerX、centerY为公共点,xInView、yInView为触摸点的坐标
/**
*获取两条线的夹角
* @param centerX
* @param centerY
* @param xInView
* @param yInView
* @return
*/
public static int getRotationBetweenLines(float centerX, float centerY, float xInView, float yInView) {
double rotation = 0;
double k1 = (double) (centerY - centerY) / (centerX * 2 - centerX);
double k2 = (double) (yInView - centerY) / (xInView - centerX);
double tmpDegree = Math.atan((Math.abs(k1 - k2)) / (1 + k1 * k2)) / Math.PI * 180;
if (xInView >
centerX &
&
yInView <
centerY) {//第一象限
rotation = 90 - tmpDegree;
} else if (xInView >
centerX &
&
yInView >
centerY) //第二象限
{
rotation = 90 + tmpDegree;
} else if (xInView <
centerX &
&
yInView >
centerY) { //第三象限
rotation = 270 - tmpDegree;
} else if (xInView <
centerX &
&
yInView <
centerY) { //第四象限
rotation = 270 + tmpDegree;
} else if (xInView == centerX &
&
yInView <
centerY) {
rotation = 0;
} else if (xInView == centerX &
&
yInView >
centerY) {
rotation = 180;
}return (int) rotation;
}
具体使用
@Override
public boolean onTouch(View v, MotionEvent event) {
float centerX = img_colors.getWidth() / 2;
float centerY = img_colors.getHeight() / 2;
setPointerRotation(Tools.getRotationBetweenLines(centerX, centerY, event.getX(), event.getY()));
return true;
}
推荐阅读
- 绑定this(call()apply()bind()和它们的内部实现原理 + 箭头函数中的this)
- App资源在线升级更新
- hashmapput和get值的时候是怎么确定key在数组中的位子的
- Mapper代理类实现
- "FedStart Failed" message appears in db2diag.log periodically
- xcode 嵌入flutter_module后编译报错 This app could not be installed at this time.
- H5活动全屏滚动页面在安卓智能电视TV调试
- SwiftUI - 一起来仿写微信APP之一首页列表视图
- ESA2GJK1DH1K基础篇: APP使用SmartConfig绑定Wi-Fi 设备并通过MQTT控制设备(V1.0)(AT+TCP非透传指令)