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:

Unknown said...

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

Jeet Dholakia said...
This comment has been removed by the author.
Anonymous said...

Thank you so much,very useful!

Anonymous said...

thank you so much,its really helpful!

Anonymous said...

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

syed said...

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

Unknown said...

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

lavanya said...

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

jeni said...

THnkyou for providing the information, I am looking forward for more number of updates
hardware and networking training in chennai

hardware and networking training in velachery

xamarin training in chennai

xamarin training in velachery

ios training in chennai

ios training in velachery

iot training in chennai

iot training in velachery

deiva said...

ya its very good ..but i need to stop in an certain limit in zoom in and out so the text will be readable..
data science training in chennai

data science training in omr

android training in chennai

android training in omr

devops training in chennai

devops training in omr

artificial intelligence training in chennai

artificial intelligence training in omr

Jayalakshmi said...

Needed to compose you a very little word to thank you yet again regarding the nice suggestions you’ve contributed here.
angular js training in chennai

angular js training in tambaram

full stack training in chennai

full stack training in tambaram

php training in chennai

php training in tambaram

photoshop training in chennai

photoshop training in tambaram

shiny said...

The knowledge of technology you have been sharing thorough this post is very much helpful to develop new idea.


hardware and networking training in chennai

hardware and networking training in annanagar

xamarin training in chennai

xamarin training in annanagar

ios training in chennai

ios training in annanagar

iot training in chennai

iot training in annanagar

jenani said...

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

vanathi said...

its really amazing, its very use full to us, keep posting like this and thank for sharing article with us .
Software Testing Training in Chennai

Software Testing Training in Velachery

Software Testing Training in Tambaram

Software Testing Training in Porur

Software Testing Training in Omr
Software Testing Training in Annanagar

subathara said...

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.Digital Marketing Training in Chennai

Digital Marketing Training in Velachery

Digital Marketing Training in Tambaram

Digital Marketing Training in Porur

Digital Marketing Training in Omr

Digital MarketingTraining in Annanagar




Hi Every One said...

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

The Blogger Worlds said...

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

Hi Every One said...

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