无法在onTouch事件中使用setScaleX和setScaleY调整大小和旋转视图

时间:2019-01-20 08:41:40

标签: java android image mobile-development

在这里,我试图用单指onTouchevent扩大视图的大小。几乎它适用于长字符串,但不适用于诸如“ Just”或“ Know”之类的小词,整个视图在抖动,有时视图尺寸意外增加,视图旋转。如果有人可以帮助我更正我的代码,将会很有帮助。

在这里我要添加预期的输出屏幕图像

enter image description here

这是TextView:

<?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;

}

0 个答案:

没有答案