Невозможно изменить размер и повернуть представление с помощью setScaleX и setScaleY в событии onTouch - PullRequest
0 голосов
/ 20 января 2019

Здесь я пытаюсь увеличить размер просмотра одним пальцем onTouchevent.Почти он работает для длинных строк, но он не работает для маленьких слов, таких как «Просто» или «Знай», полный вид становится дрожащим, иногда размеры вида неожиданно увеличиваются, а вид вращается.Было бы полезно, если бы кто-нибудь помог мне исправить мой код.

Здесь я добавляю свое ожидаемое изображение экрана вывода

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>

И добавлен TouchListener для imgPhotoEditorRotate

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;

}
...