Zoom Image on Double Tap in android
The code below shows how we can zoom image on double tap in anroid, it's custom view class which will help us to make zoom image effect in anroid
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.GestureDetector.OnGestureListener;
import android.widget.FrameLayout.LayoutParams;
/**
* Custom class to zoom image in android
*
* @author Yash@iotasol.com
*/
public class ZoomImageView extends View implements OnGestureListener {
private static final int SCALING_FACTOR = 50;
private final int LANDSCAPE = 1;
private GestureDetector gestureDetector;
private Drawable image = null;
private int scalefactor = 0;
private int orientation;
private int zoomCtr = 0;
private long lastTouchTime = 0;
private int winX, winY, imageX, imageY, scrollX = 0, scrollY = 0, left,
top, bottom, right;
public ZoomImageView(Context context, int orientation) {
super(context);
setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT));
this.orientation = orientation;
gestureDetector = new GestureDetector(this);
}
public void setImage(Drawable bitmap, Activity activity) {
image = bitmap;
imageSetting(activity);
}
public void setImage(Bitmap bitmap, Activity activity) {
image = new BitmapDrawable(bitmap).getCurrent();
imageSetting(activity);
}
/**
* Works in both landscape and potrait mode.
*/
private void imageSetting(Activity activity) {
scrollX = scrollY = 0;
scalefactor = 0;
imageX = winX = activity.getWindow().getWindowManager()
.getDefaultDisplay().getWidth();
imageY = winY = activity.getWindow().getWindowManager()
.getDefaultDisplay().getHeight();
if (orientation == LANDSCAPE) {
imageX = 3 * imageY / 4;
}
calculatePos();
}
public void calculatePos() {
int tempx, tempy;
tempx = imageX + imageX * scalefactor / 100;
tempy = imageY + imageY * scalefactor / 100;
left = (winX - tempx) / 2;
top = (winY - tempy) / 2;
right = (winX + tempx) / 2;
bottom = (winY + tempy) / 2;
invalidate();
}
/**
* Redraws the bitmap when zoomed or scrolled.
*/
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (image == null)
return;
image.setBounds(left + scrollX, top + scrollY, right + scrollX, bottom
+ scrollY);
image.draw(canvas);
}
public void zoomIn() {
scalefactor += SCALING_FACTOR;
calculatePos();
}
public void zoomOut() {
if (scalefactor == 0)
return;
scrollX = scrollY = 0;
scalefactor -= SCALING_FACTOR;
calculatePos();
}
public void scroll(int x, int y) {
scrollX += x / 5;
scrollY += y / 5;
if (scrollX + left > 0) {
scrollX = 0 - left;
} else if (scrollX + right < winX) {
scrollX = winX - right;
}
if (scrollY + top > 0) {
scrollY = 0 - top;
} else if (scrollY + bottom < winY) {
scrollY = winY - bottom;
}
invalidate();
}
@Override
public boolean onTouchEvent(MotionEvent me) {
boolean onTouchEvent = gestureDetector.onTouchEvent(me);
return onTouchEvent;
}
@Override
public boolean onDown(MotionEvent arg0) {
long thisTime = arg0.getEventTime();
if (thisTime - lastTouchTime < 250) {
lastTouchTime = -1;
onDoubleTap();
return true;
}
lastTouchTime = thisTime;
return true;
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
float distanceY) {
if (zoomCtr == 0)
return false;
scroll((int) (e2.getX() - e1.getX()), (int) (e2.getY() - e1.getY()));
return true;
}
private void onDoubleTap() {
if (zoomCtr == 0) {
zoomCtr++;
zoomIn();
return;
}
zoomCtr--;
zoomOut();
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
// TODO Auto-generated method stub
return true;
}
@Override
public void onLongPress(MotionEvent e) {
}
@Override
public void onShowPress(MotionEvent e) {
}
@Override
public boolean onSingleTapUp(MotionEvent e) {
// TODO Auto-generated method stub
return true;
}
}
Below is the test class which is used as example to run the ZoomImageView
import android.app.Activity;
import android.os.Bundle;
public class TestTapActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ZoomImageView imageView = new ZoomImageView(this, getWindow()
.getWindowManager().getDefaultDisplay().getOrientation());
imageView.setImage(this.getResources().getDrawable(R.drawable.sample1),
this);
this.setContentView(imageView);
}
}
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.GestureDetector.OnGestureListener;
import android.widget.FrameLayout.LayoutParams;
/**
* Custom class to zoom image in android
*
* @author Yash@iotasol.com
*/
public class ZoomImageView extends View implements OnGestureListener {
private static final int SCALING_FACTOR = 50;
private final int LANDSCAPE = 1;
private GestureDetector gestureDetector;
private Drawable image = null;
private int scalefactor = 0;
private int orientation;
private int zoomCtr = 0;
private long lastTouchTime = 0;
private int winX, winY, imageX, imageY, scrollX = 0, scrollY = 0, left,
top, bottom, right;
public ZoomImageView(Context context, int orientation) {
super(context);
setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT));
this.orientation = orientation;
gestureDetector = new GestureDetector(this);
}
public void setImage(Drawable bitmap, Activity activity) {
image = bitmap;
imageSetting(activity);
}
public void setImage(Bitmap bitmap, Activity activity) {
image = new BitmapDrawable(bitmap).getCurrent();
imageSetting(activity);
}
/**
* Works in both landscape and potrait mode.
*/
private void imageSetting(Activity activity) {
scrollX = scrollY = 0;
scalefactor = 0;
imageX = winX = activity.getWindow().getWindowManager()
.getDefaultDisplay().getWidth();
imageY = winY = activity.getWindow().getWindowManager()
.getDefaultDisplay().getHeight();
if (orientation == LANDSCAPE) {
imageX = 3 * imageY / 4;
}
calculatePos();
}
public void calculatePos() {
int tempx, tempy;
tempx = imageX + imageX * scalefactor / 100;
tempy = imageY + imageY * scalefactor / 100;
left = (winX - tempx) / 2;
top = (winY - tempy) / 2;
right = (winX + tempx) / 2;
bottom = (winY + tempy) / 2;
invalidate();
}
/**
* Redraws the bitmap when zoomed or scrolled.
*/
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (image == null)
return;
image.setBounds(left + scrollX, top + scrollY, right + scrollX, bottom
+ scrollY);
image.draw(canvas);
}
public void zoomIn() {
scalefactor += SCALING_FACTOR;
calculatePos();
}
public void zoomOut() {
if (scalefactor == 0)
return;
scrollX = scrollY = 0;
scalefactor -= SCALING_FACTOR;
calculatePos();
}
public void scroll(int x, int y) {
scrollX += x / 5;
scrollY += y / 5;
if (scrollX + left > 0) {
scrollX = 0 - left;
} else if (scrollX + right < winX) {
scrollX = winX - right;
}
if (scrollY + top > 0) {
scrollY = 0 - top;
} else if (scrollY + bottom < winY) {
scrollY = winY - bottom;
}
invalidate();
}
@Override
public boolean onTouchEvent(MotionEvent me) {
boolean onTouchEvent = gestureDetector.onTouchEvent(me);
return onTouchEvent;
}
@Override
public boolean onDown(MotionEvent arg0) {
long thisTime = arg0.getEventTime();
if (thisTime - lastTouchTime < 250) {
lastTouchTime = -1;
onDoubleTap();
return true;
}
lastTouchTime = thisTime;
return true;
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
float distanceY) {
if (zoomCtr == 0)
return false;
scroll((int) (e2.getX() - e1.getX()), (int) (e2.getY() - e1.getY()));
return true;
}
private void onDoubleTap() {
if (zoomCtr == 0) {
zoomCtr++;
zoomIn();
return;
}
zoomCtr--;
zoomOut();
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
// TODO Auto-generated method stub
return true;
}
@Override
public void onLongPress(MotionEvent e) {
}
@Override
public void onShowPress(MotionEvent e) {
}
@Override
public boolean onSingleTapUp(MotionEvent e) {
// TODO Auto-generated method stub
return true;
}
}
Below is the test class which is used as example to run the ZoomImageView
import android.app.Activity;
import android.os.Bundle;
public class TestTapActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ZoomImageView imageView = new ZoomImageView(this, getWindow()
.getWindowManager().getDefaultDisplay().getOrientation());
imageView.setImage(this.getResources().getDrawable(R.drawable.sample1),
this);
this.setContentView(imageView);
}
}
I hope this post will be helpful.
Please share your comments and and you can also contact me @ info@iotasol.com or visit our site www.iotasol.com , www.iotadomains.com.
i want to implement auto zoom image; without touching .. thanks in advance.
ReplyDeletebut my view fliper doen't works now
ReplyDeleteWe share perfect explanation about Android programming code and it's example.In this information very useful to me.Thanks for such a valuable information.
ReplyDeleteJava Training in Chennai | Java Training Institute in Chennai | Best Java Training in Chennai
Nice it seems to be good post... It will get readers engagement on the article since readers engagement plays an vital role in every
ReplyDeleteblog.. i am expecting more updated posts from your hands.
Fitness SMS
Fitness Text
Salon SMS
Salon Text
Investor Relation SMS
Investor Relation Text
Mobile Marketing Services
mobile marketing companies
Sms API
This is excellent information. It is amazing and wonderful to visit your site.Thanks for sharing this information,this is useful to me...
ReplyDeleteMobile Marketing Service
Mobile Marketing Companies
Sms API
Texting API
sms marketing
how to more zoom when double click and what parameter to change
ReplyDeleteAll are saying the same thing repeatedly, but in your blog I had a chance to get some useful and unique information, I love your writing style very much, I would like to suggest your blog in my dude circle, so keep on updates.
ReplyDeleteautomation anywhere training in chennai
automation anywhere training in bangalore
automation anywhere training in pune
automation anywhere online training
blueprism online training
rpa Training in sholinganallur
rpa Training in annanagar
iot-training-in-chennai
blueprism-training-in-pune
automation-anywhere-training-in-pune
Inspiring writings and I greatly admired what you have to say , I hope you continue to provide new ideas for us all and greetings success always for you..Keep update more information..
ReplyDeleteData Science training in marathahalli
Data Science training in btm
Data Science training in rajaji nagar
Data Science training in chennai
Data Science training in kalyan nagar
Data Science training in electronic city
Data Science training in USA
Data science training in pune
The knowledge of technology you have been sharing thorough this post is very much helpful to develop new idea. here by i also want to share this.
ReplyDeletejava training in chennai | java training in bangalore
java online training | java training in pune
java training in chennai | java training in bangalore
Thanks for your informative article, Your post helped me to understand the future and career prospects & Keep on updating your blog with such awesome article.
ReplyDeleteBlueprism training in Chennai
Blueprism online training
Blue Prism Training in Pune
Hello! Someone in my Facebook group shared this website with us, so I came to give it a look.
ReplyDeletesafety course institute in chennai
I am really happy with your blog because your article is very unique and powerful for new reader.
ReplyDeleteClick here:
selenium training in chennai
selenium training in bangalore
selenium training in Pune
selenium training in pune
Selenium Online Training
https://mean-aakash.blogspot.com/2015/03/transcludeelement-demystified.html
Impressive. Your story always bring hope and new energy. Keep up the good work.
ReplyDeletedevops online training
aws online training
data science with python online training
data science online training
rpa online training
Good Post! Thank you so much for sharing this pretty post, it was so good to read and useful to improve my knowledge as updated one, keep blogging.
ReplyDeleteblue prism Training in Electronic City
Appreciating the persistence you put into your blog and detailed information you provide.
ReplyDeleteAws training chennai | AWS course in chennai
Rpa training in chennai | RPA training course chennai
oracle training chennai | oracle training in chennai
php training in chennai | php course in chennai
Your good knowledge and kindness in playing with all the pieces were very useful. I don’t know what I would have done if I had not encountered such a step like this.
ReplyDeleteBest PHP Training Institute in Chennai|PHP Course in chennai
Best .Net Training Institute in Chennai
Big Data Hadoop Training in Chennai
Linux Training in Chennai
Cloud Computing Training in Chennai
very nice blogger.......................!!!
ReplyDeleteinplant training in chennai
inplant training in chennai
inplant training in chennai for it
panama web hosting
syria hosting
services hosting
afghanistan shared web hosting
andorra web hosting
belarus web hosting
brunei darussalam hosting
It's interesting that many of the bloggers to helped clarify a few things for me as well as giving
ReplyDeleteSalesforce Training | Online Course | Certification in chennai | Salesforce Training | Online Course | Certification in bangalore | Salesforce Training | Online Course | Certification in hyderabad | Salesforce Training | Online Course | Certification in pune
Inspiring writings and I greatly admired what you have to say , I hope you continue to provide new ideas for us all and greetings success always for you.keep on update.
ReplyDeleteAi & Artificial Intelligence Course in Chennai
PHP Training in Chennai
Ethical Hacking Course in Chennai Blue Prism Training in Chennai
UiPath Training in Chennai
This article is very helpful for me.Thank you for sharing. Java training in Chennai | Certification | Online Course Training | Java training in Bangalore | Certification | Online Course Training | Java training in Hyderabad | Certification | Online Course Training | Java training in Coimbatore | Certification | Online Course Training | Java training in Online | Certification | Online Course Training
ReplyDeleteThe Casino | MapyRO
ReplyDeleteFind 세종특별자치 출장안마 your way around the casino with 태백 출장마사지 Mapy's Ringer's, an interactive sports betting platform. 안성 출장마사지 It's 세종특별자치 출장샵 free and easy to download. Play on your smartphone or tablet or make 성남 출장안마