栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

根据Android中的一个手柄旋转和缩放视图

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

根据Android中的一个手柄旋转和缩放视图

如果需要使用一个处理程序图标同时调整大小和旋转图像,则应执行一些三角计算。

基于图像的初始角度和连接图像中心和当前手指位置的矢量旋转的角度,计算图像旋转的角度并不是很困难。稍微复杂一点的任务是将处理程序放置在屏幕的适当位置,以使其始终连接到主图像的角落。

public void setUpResize() {    dragHandle.setonTouchListener(new View.onTouchListener() {        float centerX, centerY, startR, startScale, startX, startY, startRotation, startA ;        public boolean onTouch(View v, MotionEvent e) { if (e.getAction() == MotionEvent.ACTION_DOWN) {     centerX = (imageView.getLeft() + imageView.getRight()) / 2f;     centerY = (imageView.getTop() + imageView.getBottom()) / 2f;     startX = e.getRawX() - dragHandle.getX() + centerX;     startY = e.getRawY() - dragHandle.getY() + centerY;     startR = (float) Math.hypot(e.getRawX() - startX, e.getRawY() - startY);     startA = (float) Math.toDegrees(Math.atan2(e.getRawY() - startY, e.getRawX() - startX));     startScale = imageView.getScaleX();     startRotation = imageView.getRotation(); } else if (e.getAction() == MotionEvent.ACTION_MOVE) {     float newR = (float) Math.hypot(e.getRawX() - startX, e.getRawY() - startY);     float newA = (float) Math.toDegrees(Math.atan2(e.getRawY() - startY, e.getRawX() - startX));     float newScale = newR / startR * startScale;     float newRotation = newA - startA + startRotation;     imageView.setScaleX(newScale);     imageView.setScaleY(newScale);     imageView.setRotation(newRotation);     // ----- this part takes some effort to understand... ------     dragHandle.setX((float) (centerX + Math.hypot(imageView.getWidth(), imageView.getHeight())/2f * newScale   * Math.cos(Math.toRadians(newRotation) + Math.atan2(imageView.getHeight(), imageView.getWidth()))));     dragHandle.setY((float) (centerY + Math.hypot(imageView.getWidth(), imageView.getHeight())/2f * newScale   * Math.sin(Math.toRadians(newRotation) + Math.atan2(imageView.getHeight(), imageView.getWidth()))));     //-----------------------------------------------------------     dragHandle.setPivotX(0);     dragHandle.setPivotY(0);     dragHandle.setRotation(newRotation); } else if (e.getAction() == MotionEvent.ACTION_UP) { } return true;        }    });}

所以,我在做什么?

Math.hypot(imageView.getWidth(), imageView.getHeight()) / 2f * newScale

-这将计算主图像对角线的一半长度,即其中心和角点之间的距离

Math.atan2(imageView.getHeight(), imageView.getWidth())

-这是对角线最初旋转的角度(由于图像不能为正方形,因此该角度并不总是45度)。

Math.cos(Math.toRadians(newRotation) + Math.atan2(imageView.getHeight(), imageView.getWidth()))

-这使我们可以投影到单位矢量的X轴上,并旋转一个角度,该角度由旋转图像的角度及其对角线的初始旋转角度组成。将其乘以对角线的一半长度后,我们得到图像角的X。

与Y相同,但使用

sin
代替
cos



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/438718.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号