Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class ZoomingTextView extends TextView {
- private static final int DEFAULT_TEXT_SIZE = 18;
- private static final int MINIMAL_TEXT_SIZE = 6, MAXIMAL_TEXT_SIZE = 50;
- private static final float MIN_SCALE = MINIMAL_TEXT_SIZE / (float) DEFAULT_TEXT_SIZE;
- private static final float MAX_SCALE = MAXIMAL_TEXT_SIZE / (float) DEFAULT_TEXT_SIZE;
- private ScaleGestureDetector scaleDetector;
- private float scaleFactor;
- public ZoomingTextView(Context context) {
- super(context);
- init(context);
- }
- public ZoomingTextView(Context context, AttributeSet attrs) {
- super(context, attrs);
- init(context);
- }
- public ZoomingTextView(Context context, AttributeSet attrs, int defStyle) {
- super(context, attrs, defStyle);
- init(context);
- }
- private void init(Context context) {
- scaleDetector = new ScaleGestureDetector(context, new ScaleListener());
- scaleFactor = 1.0f;
- }
- @Override
- public boolean onTouchEvent(MotionEvent event) {
- scaleDetector.onTouchEvent(event);
- return super.onTouchEvent(event);
- }
- private class ScaleListener extends ScaleGestureDetector.SimpleOnScaleGestureListener {
- @Override
- public boolean onScale(ScaleGestureDetector detector) {
- scaleFactor *= detector.getScaleFactor();
- scaleFactor = Math.max(MIN_SCALE, Math.min(scaleFactor, MAX_SCALE));
- final int textSize = (int) (scaleFactor * DEFAULT_TEXT_SIZE);
- setTextSize(textSize);
- return true;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement