Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I implemented a pinch zoom for my <code>TextView</code>, using <a href="http://www.zdnet.com/blog/burnette/how-to-use-multi-touch-in-android-2-part-6-implementing-the-pinch-zoom-gesture/1847" rel="noreferrer">this</a> tutorial. The resulting code is this:</p> <pre><code>private GestureDetector gestureDetector; private View.OnTouchListener gestureListener; </code></pre> <p>and in onCreate():</p> <pre><code> // Zoom handlers gestureDetector = new GestureDetector(new MyGestureDetector()); gestureListener = new View.OnTouchListener() { // We can be in one of these 2 states static final int NONE = 0; static final int ZOOM = 1; int mode = NONE; static final int MIN_FONT_SIZE = 10; static final int MAX_FONT_SIZE = 50; float oldDist = 1f; @Override public boolean onTouch(View v, MotionEvent event) { TextView textView = (TextView) findViewById(R.id.text); switch (event.getAction() &amp; MotionEvent.ACTION_MASK) { case MotionEvent.ACTION_POINTER_DOWN: oldDist = spacing(event); Log.d(TAG, "oldDist=" + oldDist); if (oldDist &gt; 10f) { mode = ZOOM; Log.d(TAG, "mode=ZOOM" ); } break; case MotionEvent.ACTION_POINTER_UP: mode = NONE; break; case MotionEvent.ACTION_MOVE: if (mode == ZOOM) { float newDist = spacing(event); // If you want to tweak font scaling, this is the place to go. if (newDist &gt; 10f) { float scale = newDist / oldDist; if (scale &gt; 1) { scale = 1.1f; } else if (scale &lt; 1) { scale = 0.95f; } float currentSize = textView.getTextSize() * scale; if ((currentSize &lt; MAX_FONT_SIZE &amp;&amp; currentSize &gt; MIN_FONT_SIZE) ||(currentSize &gt;= MAX_FONT_SIZE &amp;&amp; scale &lt; 1) || (currentSize &lt;= MIN_FONT_SIZE &amp;&amp; scale &gt; 1)) { textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, currentSize); } } } break; } return false; } </code></pre> <p>Magic constants 1.1 and 0.95 were chosen empirically (using <code>scale</code> variable for this purpose made my <code>TextView</code> behave kind of weird).</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload