How do I add a pinch to enlarge the text in the viewpager?
I've searched a lot of blogs on pinch to zoom TextView
in ViewPager
. I just found many answers to increase to increase ImageView
.
How can I add a pinch to zoom in the ViewPager.
Here are the links I read before posting:
Update
@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)
. the error message TouchImageView
cannot be resolved
source to share
Create a private class SimpleOnScaleGestureListener
that 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;
}
}
Internally, the onScale
function we are doing first gets ScaleFactor
and sets the limits for scaling the text. Here the minimum text is 15 and the maximum will be 36 .
From the next line, we are using scaling for the Textview inside the viewpager.
View view=pager.getFocusedChild().findViewById(R.id.article_content);
In this first one, I get the current viewpager by calling pager.getFocusedChild()
, from which I get a linearlayout that contains all those text images that I want to enlarge.
You can pass text IDs right there. At runtime, I am adding text images to this layout, so I follow like this. If you want to scale a mutilated text image at a time that has the same parent, you can follow this example, or you can create multiple View instances by passing the text directly.
textView.setTextSize(scaleFactor);
Here I am applying scaling to textview
you have to override the touch event detection method with viewpager
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
super.dispatchTouchEvent(ev);
return scaleGestureDetector.onTouchEvent(ev);
}
And in onCreate()
create an instance of ScaleGestureDetector like this:
scaleGestureDetector = new ScaleGestureDetector(this, new simpleOnScaleGestureListener());
Now you can apply smooth scaling in the viewpager.
source to share