Advertisement
aNNiMON

ZoomingTextView (Android)

Oct 13th, 2012
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.64 KB | None | 0 0
  1. public class ZoomingTextView extends TextView {
  2.    
  3.     private static final int DEFAULT_TEXT_SIZE = 18;
  4.     private static final int MINIMAL_TEXT_SIZE = 6, MAXIMAL_TEXT_SIZE = 50;
  5.     private static final float MIN_SCALE = MINIMAL_TEXT_SIZE / (float) DEFAULT_TEXT_SIZE;
  6.     private static final float MAX_SCALE = MAXIMAL_TEXT_SIZE / (float) DEFAULT_TEXT_SIZE;
  7.    
  8.     private ScaleGestureDetector scaleDetector;
  9.     private float scaleFactor;
  10.    
  11.     public ZoomingTextView(Context context) {
  12.         super(context);
  13.         init(context);
  14.     }
  15.    
  16.     public ZoomingTextView(Context context, AttributeSet attrs) {
  17.         super(context, attrs);
  18.         init(context);
  19.     }
  20.    
  21.     public ZoomingTextView(Context context, AttributeSet attrs, int defStyle) {
  22.         super(context, attrs, defStyle);
  23.         init(context);
  24.     }
  25.    
  26.     private void init(Context context) {
  27.         scaleDetector = new ScaleGestureDetector(context, new ScaleListener());
  28.         scaleFactor = 1.0f;
  29.     }
  30.    
  31.  
  32.     @Override
  33.     public boolean onTouchEvent(MotionEvent event) {
  34.         scaleDetector.onTouchEvent(event);
  35.         return super.onTouchEvent(event);
  36.     }
  37.    
  38.     private class ScaleListener extends ScaleGestureDetector.SimpleOnScaleGestureListener {
  39.  
  40.         @Override
  41.         public boolean onScale(ScaleGestureDetector detector) {
  42.             scaleFactor *= detector.getScaleFactor();
  43.             scaleFactor = Math.max(MIN_SCALE, Math.min(scaleFactor, MAX_SCALE));
  44.  
  45.             final int textSize = (int) (scaleFactor * DEFAULT_TEXT_SIZE);
  46.             setTextSize(textSize);
  47.  
  48.             return true;
  49.         }
  50.     }
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement