amer halem
amer halem

Reputation: 121

How to Zoom a Text View in Scroll View?

I have seen a lots of code here which is helpful to zoom your textview but none of them work with my text because it is within scrollview. How can I get rid of this problem?

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

Upvotes: 1

Views: 4766

Answers (4)

Raphael Cordeiro
Raphael Cordeiro

Reputation: 1

I'm using this solution. Crédits for Zoom Algorithm in this vídeo

Use a TextView without ScrollView, just use 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));
    }
}

Upvotes: 0

Pankaj Lilan
Pankaj Lilan

Reputation: 4453

Following is the way for implementing Pinch Zoom in TextView with/without ScrollView

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));
        }
    }
}

Upvotes: 3

Tuss
Tuss

Reputation: 945

Hopefully this will help others. This answer is from 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;
        }
    });

}

The answer from here has the problem when text is resized even fingers are static (two fingers on the screen). What I did is add a check so that the textSize do not make any changes instantly.

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

You can play around with safe < 3 to your desired changes value.

Upvotes: 0

dtyrant
dtyrant

Reputation: 56

Use Polidea's zoomview, it works in a scrollview and has pinch zoom and double tap to zoom, one thing thought, I ended up disabling the pinch zoom and just using the double tap

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

Put your TextView andany other Views you are using into a LinearLayout that lives on a ZoomView which lives on the ScrollView, e.g.:

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

Upvotes: 0

Related Questions