java - 无法在 onTouch 事件中使用 setScaleX 和 setScaleY 调整视图大小和旋转视图
问题描述
在这里,我尝试用单指扩展视图大小onTouchevent
。几乎它适用于长字符串,但不适用于“Just”或“Know”等小词,完整的视图正在摇晃,有时视图尺寸会意外增加并且视图会旋转。如果有人帮助我更正我的代码,那将会很有帮助。
在这里,我添加了我预期的输出屏幕图像
这是文本视图:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<FrameLayout
android:id="@+id/frmBorder"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="8dp"
android:background="@drawable/rounded_border_tv">
<TextView
android:id="@+id/tvPhotoEditorText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:textColor="#000000"
android:textSize="18sp"
tools:text="Burhanuddin"
tools:textColor="@android:color/black" />
</FrameLayout>
<ImageView
android:id="@+id/imgPhotoEditorClose"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_gravity="top|start"
android:elevation="1dp"
android:src="@drawable/ic_remove" />
<ImageView
android:id="@+id/imgPhotoEditorRotate"
android:layout_width="15dp"
android:layout_height="15dp"
android:layout_gravity="bottom|end"
android:elevation="1dp"
android:src="@drawable/ic_remove" />
</FrameLayout>
并为 imgPhotoEditorRotate 添加了 TouchListener
final View textRootView = getLayout(ViewType.TEXT);
final ImageView imgRotate = textRootView.findViewById(R.id.imgPhotoEditorRotate);
SingleTouchListener singleTouchListener= new SingleTouchListener(
textRootView, parentView, this.imageView);
imgRotate.setOnTouchListener(singleTouchListener);
OnTouch 事件
public boolean onTouch(View view, MotionEvent event) {
int action = event.getAction();
float x = event.getX();
float y = event.getY();
float mx = view.getX();
float my = view.getY();
switch (action)
{
case MotionEvent.ACTION_DOWN:
last_x=x;
last_y=y;
textRootView.bringToFront();
break;
case MotionEvent.ACTION_MOVE:
float dx = x - last_x;
float dy = y - last_y;
updateRotateAndScale(dx, dy,mx,my);
textRootView.setScaleX(mScale);
textRootView.setScaleY(mScale);
float rotation = adjustAngle(textRootView.getRotation() +
mRotateAngle);
textRootView.setRotation(rotation);
last_x = x;
last_y = y;
break;
case MotionEvent.ACTION_UP:
if (onSingleTouchListener!=null)
onSingleTouchListener.onRemoveViewListener(view);
resetView();
break;
}
return true;
}
public void updateRotateAndScale(final float dx, final float dy, float mx,
float my)
{
float Frame_c_x = textRootView.getPivotX()/2; // frame layout center
position, for to perform animation and scale.
float Frame_c_y = textRootView.getPivotY()/2;
float x = mx; // view last x , y position
float y = my;
float n_x = x + dx; // extended view
float n_y = y + dy;
float xa = x - Frame_c_x; // Off of the previous view
float ya = y - Frame_c_y;
float xb = n_x - Frame_c_x;
float yb = n_y - Frame_c_y;
float srcLen = (float) Math.sqrt(xa * xa + ya * ya);
float curLen = (float) Math.sqrt(xb * xb + yb * yb);
float scale = curLen / srcLen;
mScale *= scale;
float newWidth = textRootView.getWidth() * mScale;
if (newWidth < 70) {
mScale /= scale;
return;
}
double cos = (xa * xb + ya * yb) / (srcLen * curLen);
if (cos > 1 || cos < -1)
return;
float angle = (float) Math.toDegrees(Math.acos(cos));
float calMatrix = xa * yb - xb * ya;
int flag = calMatrix > 0 ? 1 : -1;
angle = flag * angle;
mRotateAngle += angle;
}
public void resetView() {
mRotateAngle = 0;
mScale = 1;
}
解决方案
推荐阅读
- snakemake - 大型工作流的 DAG 完成后 Snakemake 挂起
- hadoop - 物理节点和虚拟节点有什么区别?
- java - Java -Executable JAR- 创建 FileWriter 时出错:FileNotFoundException 访问被拒绝
- python - 有什么方法可以知道 Python 中的单独程序可用的命令行选项吗?
- python-3.x - 找不到命令“python”
- c++ - 如何创建一个类并存储它而不是仅仅创建它
- python - 在 pandas 数据框中选择具有两个唯一标识符的行并将它们存储为新数据框
- arrays - 从 vb.net 中的字节数组创建 1bpp 位图的问题
- javascript - 如何在特定服务器中查找成员角色
- java - 尝试从 URL 读取文件时 url.openStream() 方法出错