如何在滚动视图中缩放文本视图?

How to Zoom a Text View in Scroll View?

我在这里看到了很多有助于缩放 textview 的代码,但是其中 none 可以处理我的文本,因为它在 scrollview 中。我怎样才能摆脱这个问题?

import android.app.Activity;
import android.os.Bundle;
import android.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.TextView;


public class Introduce extends Activity implements OnTouchListener{

final static float STEP = 200;
TextView mtxtRatio1,mtxtRatio2,mtxtRatio3,mtxtRatio4;
float mRatio = 1.0f;
int mBaseDist;
float mBaseRatio;
float fontsize = 13;

public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.introduce);

  mtxtRatio1 = (TextView)findViewById(R.id.intro1);  
  mtxtRatio1.setTextSize(mRatio+13);  
} 

public boolean onTouchEvent(MotionEvent event) {
  if (event.getPointerCount() == 2) {
    int action = event.getAction();
    int pureaction = action & MotionEvent.ACTION_MASK;
    if (pureaction == MotionEvent.ACTION_POINTER_DOWN) {
      mBaseDist = getDistance(event);
      mBaseRatio = mRatio;
    } else {
      float delta = (getDistance(event) - mBaseDist) / STEP;
      float multi = (float)Math.pow(2, delta);
      mRatio = Math.min(1024.0f, Math.max(0.1f, mBaseRatio * multi));
      mtxtRatio1.setTextSize(mRatio+13);
    }
  }
  return true; 
}

int getDistance(MotionEvent event) {
  int dx = (int)(event.getX(0) - event.getX(1));
  int dy = (int)(event.getY(0) - event.getY(1));
  return (int)(Math.sqrt(dx * dx + dy * dy));
 }

public boolean onTouch(View v, MotionEvent event) {
  // TODO Auto-generated method stub
  return false; 
}
}

使用 Polidea 的缩放视图,它在滚动视图中工作,并具有捏合缩放和双击缩放功能,有一件事想到,我最终禁用了捏合缩放并仅使用双击

https://github.com/Polidea/android-zoom-view

将您的 TextView 和您正在使用的任何其他视图放入位于 ScrollView 上的 ZoomView 上的 LinearLayout,例如:

<ScrollView 
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

         <com.polidea.ZoomView 
                android:layout_width="match_parent"
                android:layout_height="wrap_content" >

                <LinearLayout
                       android:id="@+id/myLinearLayout"
                       android:layout_width="wrap_content"
                       android:layout_height="wrap_content"
                       android:orientation="vertical" >        
                </LinearLayout>

        </com.polidea.ZoomView>

</ScrollView>

希望这对其他人有所帮助。本回答来自here and here.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    tv = (TextView) findViewById(R.id.tv);

    tv.setText(getString(R.string.hello_world));


    scaleGestureDetector = new ScaleGestureDetector(this, new simpleOnScaleGestureListener());

    tv.setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if(event.getPointerCount() == 1){
                //stuff for 1 pointer
            }else{ //when 2 pointers are present
                switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    // Disallow ScrollView to intercept touch events.
                    v.getParent().requestDisallowInterceptTouchEvent(true);
                    scaleGestureDetector.onTouchEvent(event);
                    break;

                case MotionEvent.ACTION_MOVE:
                    // Disallow ScrollView to intercept touch events.
                    v.getParent().requestDisallowInterceptTouchEvent(true);
                    scaleGestureDetector.onTouchEvent(event);
                    break;

                case MotionEvent.ACTION_UP:
                    // Allow ScrollView to intercept touch events.
                    v.getParent().requestDisallowInterceptTouchEvent(false);
                    break;
                }

            }
            return true;
        }
    });

}

here 的答案在调整文本大小时出现问题,即使手指是静止的(两个手指在屏幕上)。我所做的是添加一个检查,以便 textSize 不会立即进行任何更改。

private float safe;

public class simpleOnScaleGestureListener extends SimpleOnScaleGestureListener {

    @Override
    public boolean onScale(ScaleGestureDetector detector) {
        // TODO Auto-generated method stub
        float size = tv.getTextSize();
        Log.d("TextSizeStart", String.valueOf(size));

        //float factor = detector.getScaleFactor();
        float factor = Math.max(0.5f, Math.min(detector.getScaleFactor(), 2f));
        Log.d("Factor", String.valueOf(factor));


        float product = size*factor;
        Log.d("TextSize", String.valueOf(product));

        safe = Math.abs(product - size);

        if(product <= 100 && product >= 20 && safe < 3){
            //tv.setText("factor= " +factor + "\n" +  "product = \n" + size + " * " + factor + " \n= " + product +"\n" + getString(R.string.hello_world));
            tv.setTextSize(TypedValue.COMPLEX_UNIT_PX, product);
        }

        size = tv.getTextSize();
        Log.d("TextSizeEnd", String.valueOf(size));
        return true;
    }
}

您可以尝试 safe < 3 到您想要的更改值。

以下是在TextView with/without ScrollView

中实现Pinch Zoom的方法

MainActivity.java

public class MainActivity extends AppCompatActivity{

    final static float STEP = 200;
    float mRatio = 1.0f;
    int mBaseDist;
    float mBaseRatio;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true);

        textViewData = (TextView).findViewById(R.id.tvContributeData);
        textViewData.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View view, MotionEvent event) {
            if (event.getPointerCount() == 2) {
                int action = event.getAction();
                int pureaction = action & MotionEvent.ACTION_MASK;
                if (pureaction == MotionEvent.ACTION_POINTER_DOWN) {
                    mBaseDist = getDistance(event);
                    mBaseRatio = mRatio;
                } else {
                    float delta = (getDistance(event) - mBaseDist) / STEP;
                    float multi = (float) Math.pow(2, delta);
                    mRatio = Math.min(1024.0f, Math.max(0.1f, mBaseRatio * multi));
                    textViewData.setTextSize(mRatio + 13);
                }
            }
            return true;
        });

        int getDistance(MotionEvent event) {
            int dx = (int) (event.getX(0) - event.getX(1));
            int dy = (int) (event.getY(0) - event.getY(1));
            return (int) (Math.sqrt(dx * dx + dy * dy));
        }
    }
}

我正在使用这个解决方案。 此 vídeo

中缩放算法的学分

使用不带 ScrollView 的 TextView,只需使用 android:scrollbars="vertical"

        <TextView
        android:id="@+id/activity_content_text_content"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_weight="1"
        android:gravity="fill"
        android:textSize="8pt"
        android:scrollbars="vertical"
        />

Java:

public class MainActivity extends Activity implements View.OnTouchListener {

   private TextView textContent = null;
   private final static float move = 200;
   private float ratio = 1.0f;
   private int baseDist;
   private float baseRatio;

   @Override
   protected void onCreate(@Nullable Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.teste_layout);
       textContent = findViewById(R.id.activity_content_text_content);
       textContent.setText("Lorem ipsum dolor sit amet......");

       textContent.setMovementMethod(new ScrollingMovementMethod());
       textContent.setOnTouchListener(this);
    }


    @Override
    public boolean onTouch(View v, MotionEvent event) {
        return onTouchEvent(event);
    }


    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if(event.getPointerCount() == 2){

            int action = event.getAction();
            int mainAction = action&MotionEvent.ACTION_MASK;

            if(mainAction == MotionEvent .ACTION_POINTER_DOWN){
                    baseDist = getDisTance(event);
                    baseRatio = ratio;
                } else {
                    float scale = (getDisTance(event)-baseDist)/move;
                    float factor = (float)Math.pow(2, scale);
                ratio = Math.min(1024.0f, Math.max(0.1f, baseRatio*factor));
                textContent.setTextSize(ratio+15);
            }
        } else {
            return false;
        }
        return true;
    }

    private int getDisTance(MotionEvent event) {
        int dx = (int) (event.getX(0)-event.getX(1));
        int dy = (int) (event.getY(0)-event.getY(1));
        return (int) (Math.sqrt(dx*dx+dy*dy));
    }
}