Pages

Wednesday, June 6, 2012

[Android] pinch zoom event with font size change

Android Java code for the pinch zoom event that makes increase font size.

import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.TextView;


public class Introduce extends Activity implements OnTouchListener{
 
 final static float STEP = 200;
 TextView mtxtRatio1,mtxtRatio2,mtxtRatio3,mtxtRatio4;
 float mRatio = 1.0f;
 int mBaseDist;
 float mBaseRatio;
 float fontsize = 13;

 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.introduce);
  
  mtxtRatio1 = (TextView)findViewById(R.id.intro1);
  mtxtRatio1.setTextSize(mRatio+13);  
 }

 public boolean onTouchEvent(MotionEvent event) {
  if (event.getPointerCount() == 2) {
   int action = event.getAction();
   int pureaction = action & MotionEvent.ACTION_MASK;
   if (pureaction == MotionEvent.ACTION_POINTER_DOWN) {
    mBaseDist = getDistance(event);
    mBaseRatio = mRatio;
   } else {
    float delta = (getDistance(event) - mBaseDist) / STEP;
    float multi = (float)Math.pow(2, delta);
    mRatio = Math.min(1024.0f, Math.max(0.1f, mBaseRatio * multi));
    mtxtRatio1.setTextSize(mRatio+13);
   }
  }
  return true; 
 }

 int getDistance(MotionEvent event) {
  int dx = (int)(event.getX(0) - event.getX(1));
  int dy = (int)(event.getY(0) - event.getY(1));
  return (int)(Math.sqrt(dx * dx + dy * dy));
 }

 public boolean onTouch(View v, MotionEvent event) {
  // TODO Auto-generated method stub
  return false;
 }
}

18 comments:

  1. Byung Yong Park this is great, could you explain a little how to wire it up?
    Thanks!

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. Thank you so much,very useful!

    ReplyDelete
  4. thank you so much,its really helpful!

    ReplyDelete
  5. if the textview is in a scrollview , then it dont zoom rather it scroll . how can i solve this ??

    ReplyDelete
  6. ya its very good ..but i need to stop in an certain limit in zoom in and out so the text will be readable

    ReplyDelete
  7. I am happy to find this post very useful for me, as it contains a lot of information. I always prefer to read the quality content I found in you post. Thanks for sharing.
    Java Training in Chennai | Java Training Institute in Chennai

    ReplyDelete
  8. The post is written in very a good manner and it entails many useful information for me. I am happy to find your distinguished way of writing the post. Now you make it easy for me to understand and implement the concept. All the best


    Java training in Chennai

    Java training in Bangalore

    Java training in Hyderabad

    Java Training in Coimbatore

    Java Online Training

    ReplyDelete
  9. I believe that your blog will surely help the readers who are really in need of this vital piece of information. Waiting for your updates.
    Java Training in Chennai

    Java Training in Velachery

    Java Training in Tambaram

    Java Training in Porur

    Java Training in Omr

    Java Training in Annanagar

    ReplyDelete
  10. Pinnacle Game Profiler Crack allows you to use any gamepad, joystick, or other type of game controller to play your favorite games. PinnacleGameProfiler Torrent

    ReplyDelete
  11. Read reviews, compare customer ratings, see screenshots and learn more about Mega crack. Download Mega crack and enjoy it on your iPhone, Mega Crack

    ReplyDelete
  12. To the most beautiful princess of my life, wishing you all the margaritas and the vodkas in the world! With you by my side, there’s definitely not a dull moment so thank you for being my happiness. You Are My Princess Quotes

    ReplyDelete