3

I searched many blogs regarding pinch to zoom TextView in ViewPager. I just found many answers for pinch to zoom ImageView.

How I can add pinch to zoom in ViewPager.

Here are the links, which I read before posting:

Updated

@Override
protected boolean canScroll(View v, boolean checkV, int dx, int x, int y) {
    if (v instanceof TouchImageView) {
        return ((TouchImageView) v).canScrollHorizontallyFroyo(-dx);
    } else {
        return super.canScroll(v, checkV, dx, x, y);
    }
}

I am getting error in if (v instanceof TouchImageView) . error message says ,TouchImageView cannot be resolved

Community
  • 1
  • 1
scott
  • 3,112
  • 19
  • 52
  • 90
  • Can you elaborate on issues you are facing while using solutions given in first link ? I thought it should work – Ramesh May 12 '15 at 04:10
  • view pager not zooming text.whenever i try to pinch to zoom textview ,page will scroll @Ramesh. – scott May 12 '15 at 04:15
  • Must be a problem with event delegation . is it possible to share pseudo code – Ramesh May 12 '15 at 04:18
  • i will post the code soon.because i deleted the code which i used . thank you for the response @Ramesh – scott May 12 '15 at 04:20

1 Answers1

1

Create a private class SimpleOnScaleGestureListener which extends SimpleOnScaleGestureListener in your activity like this

public class SimpleOnScaleGestureListener extends
        ScaleGestureDetector.SimpleOnScaleGestureListener {

    float scaleFactor;

    @Override
    public boolean onScale(ScaleGestureDetector detector) {

        scaleFactor *= detector.getScaleFactor();
        if(isTablet(getApplicationContext())){
            setFontStyleandSizeTab();
        }else{
            setFontStyleandSize();
        }
        scaleFactor = Math.max(15.0f, Math.min(scaleFactor,36.0f));
         View view=pager.getFocusedChild().findViewById(R.id.article_content)  ;
         View article_intro=pager.getFocusedChild().findViewById(R.id.article_intro)  ;

         ViewGroup group = (ViewGroup) view;
         int count=group.getChildCount();

       for(int i=0;i<count;i++){
            View child=group.getChildAt(i);
              if(child instanceof TextView){
                  TextView textView=(TextView)child;
                  textView.setTextSize(scaleFactor);

              }
           }
        return true;
    }
}

Inside onScale function what we are doing is getting ScaleFactor first and setting limits for zooming text. Here textsize minimum is 15 and maximum will be 36.
From next line we are appling pinch zoom for Textview inside viewpager.

View view=pager.getFocusedChild().findViewById(R.id.article_content);

In this first I am obtaining viewpager current item by calling pager.getFocusedChild() from that I am obtaining a linearlayout which contains all that textviews which I want to zoom.

You can pass textview ids directly there. At runtime I am adding textviews to this layout that why I follow like this.
If you want to zoom mutilple textviews at a time which have same parent then you can follow like this or you can create multiple View instance by passing textview id directly.

textView.setTextSize(scaleFactor);

Here I am applying scaling for textview

you must override a method to detect touch event with viewpager

@Override
public boolean dispatchTouchEvent(MotionEvent ev) {

    super.dispatchTouchEvent(ev);
    return scaleGestureDetector.onTouchEvent(ev);

}

And in onCreate() create ScaleGestureDetector instance like this:

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

Now you can apply a smooth zooming in viewpager.

Stephan Bauer
  • 9,120
  • 5
  • 36
  • 58