我已经尝试了一个多星期,但是我无法根据触摸正确旋转布局(相对).我需要设置旋转角度,以使布局在我将手指移到布局的左下角(图像很小)时旋转.基本上,我有一个带有贴纸图像的相对布局.我可以通过触摸布局的各个角来拖动和缩放贴纸(图像),但不能正确旋转它.有人可以帮我设置旋转角度,以便整个布局都能平滑旋转吗?
这是代码片段
switch (event.getAction()) { case MotionEvent.ACTION_DOWN: { lasttouchedScaleXposition = event.getX(); lasttouchedScaleYposition = event.getY(); if(prevIoUsXposition == 0 && prevIoUsYposition == 0){ int[] vIEwLocation = new int[2]; v.getLocationOnScreen(vIEwLocation); prevIoUsXposition= vIEwLocation[0]; prevIoUsYposition= vIEwLocation[1]; } break; } case MotionEvent.ACTION_MOVE: { currenttouchedXposition = event.getX() - lasttouchedScaleXposition; currenttouchedYposition = event.getY() - lasttouchedScaleYposition; currentXposition = prevIoUsXposition + currenttouchedXposition; currentYposition = prevIoUsYposition + currenttouchedYposition; relativeLayout mainLayout = (relativeLayout) findVIEwByID(R.ID.parent); int w = mainLayout.getWIDth(); int h = mainLayout.getHeight(); float center_x = 0; float center_y = 0; center_x += w/2; center_y += h/2; double tx = currentXposition - center_x, ty = currentYposition - center_y; double t_length = Math.sqrt(tx*tx + ty*ty); double a = Math.todegrees(Math.acos(ty / t_length)); mainLayout.setRotation((float) a); mainLayout.setTranslationX((w - h) / 2); mainLayout.setTranslationY((h - w) / 2); VIEwGroup.LayoutParams lp = (VIEwGroup.LayoutParams) mainLayout.getLayoutParams(); lp.height = w; lp.wIDth = h; parent.setLayoutParams(lp); prevIoUsXposition = currentXposition; prevIoUsYposition = currentYposition; break; } } return true; }
`
解决方法:
尝试了很多事情,但没有得到理想的结果.此链接帮助我解决了问题.
rotate and resize the image view with single finger in android
源链接https://github.com/ryanch741/android-view-rotate-zoom-single-finger
总结以上是内存溢出为你收集整理的在Android中旋转包含基于onTouchListener的标签的布局全部内容,希望文章能够帮你解决在Android中旋转包含基于onTouchListener的标签的布局所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)