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.