Android supports 3 common image formats PNG, JPG, GIF, along with 9 patch PNG images. Images are stored in the directory res/layout/drawable.
As of version 1.6 of the Android SDK multiple drawable directories exist for different screen resolutions. There are low, medium, and high DPI specific directories, drawable-ldpi, drawable-mdpi, drawable-hdpi respectively. This allows you to create images at different DPI to enhance the appearance of your application.
All image filenames should be lowercase and only contain letters, numbers, and underscores.
ImageView displays an arbitrary image, such as an icon. The ImageView class can load images from various sources (such as resources or content providers), takes care of computing its measurement from the image so that it can be used in any layout manager, and provides various display options such as scaling and tinting.
You can read more about ImageView from here.
Step by Step Image zoom and drag example:
1. Create New Project (for example: touchmv)
2. Add image file (for example "butterfly.png") to "res/drawable/" folder
3. Edit res/layout/main.xml file as following
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/root"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/butterfly"
android:scaleType="matrix"
android:adjustViewBounds="true"
android:layout_gravity="center">
</ImageView>
</FrameLayout>
4. Create new class by name "Touch" as following: package com.blogspot.vmustafayev4en.touchmv.listener;
import android.graphics.Matrix;
import android.graphics.PointF;
import android.util.FloatMath;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageView;
public class Touch implements OnTouchListener {
// These matrices will be used to move and zoom image
Matrix matrix = new Matrix();
Matrix savedMatrix = new Matrix();
// We can be in one of these 3 states
static final int NONE = 0;
static final int DRAG = 1;
static final int ZOOM = 2;
int mode = NONE;
// Remember some things for zooming
PointF start = new PointF();
PointF mid = new PointF();
float oldDist = 1f;
@Override
public boolean onTouch(View v, MotionEvent event) {
ImageView view = (ImageView) v;
// Dump touch event to log
dumpEvent(event);
// Handle touch events here...
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
savedMatrix.set(matrix);
start.set(event.getX(), event.getY());
mode = DRAG;
break;
case MotionEvent.ACTION_POINTER_DOWN:
oldDist = spacing(event);
if (oldDist > 10f) {
savedMatrix.set(matrix);
midPoint(mid, event);
mode = ZOOM;
}
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_POINTER_UP:
mode = NONE;
break;
case MotionEvent.ACTION_MOVE:
if (mode == DRAG) {
// ...
matrix.set(savedMatrix);
matrix.postTranslate(event.getX() - start.x, event.getY() - start.y);
} else if (mode == ZOOM) {
float newDist = spacing(event);
if (newDist > 10f) {
matrix.set(savedMatrix);
float scale = newDist / oldDist;
matrix.postScale(scale, scale, mid.x, mid.y);
}
}
break;
}
view.setImageMatrix(matrix);
return true; // indicate event was handled
}
/** Show an event in the LogCat view, for debugging */
private void dumpEvent(MotionEvent event) {
String names[] = { "DOWN", "UP", "MOVE", "CANCEL", "OUTSIDE",
"POINTER_DOWN", "POINTER_UP", "7?", "8?", "9?" };
StringBuilder sb = new StringBuilder();
int action = event.getAction();
int actionCode = action & MotionEvent.ACTION_MASK;
sb.append("event ACTION_").append(names[actionCode]);
if (actionCode == MotionEvent.ACTION_POINTER_DOWN
|| actionCode == MotionEvent.ACTION_POINTER_UP) {
sb.append("(pid ").append(
action >> MotionEvent.ACTION_POINTER_ID_SHIFT);
sb.append(")");
}
sb.append("[");
for (int i = 0; i < event.getPointerCount(); i++) {
sb.append("#").append(i);
sb.append("(pid ").append(event.getPointerId(i));
sb.append(")=").append((int) event.getX(i));
sb.append(",").append((int) event.getY(i));
if (i + 1 < event.getPointerCount())
sb.append(";");
}
sb.append("]");
}
/** Determine the space between the first two fingers */
private float spacing(MotionEvent event) {
float x = event.getX(0) - event.getX(1);
float y = event.getY(0) - event.getY(1);
return FloatMath.sqrt(x * x + y * y);
}
/** Calculate the mid point of the first two fingers */
private void midPoint(PointF point, MotionEvent event) {
float x = event.getX(0) + event.getX(1);
float y = event.getY(0) + event.getY(1);
point.set(x / 2, y / 2);
}
}
5. Change your Activity class as following:package com.blogspot.vmustafayev4en.touchmv;
import com.blogspot.vmustafayev4en.touchmv.listener.Touch;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ImageView;
public class BlogActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ImageView iv = (ImageView) findViewById(R.id.imageView);
iv.setOnTouchListener(new Touch());
}
}
If you did all steps then you'll be able to drag image or zoom in zoom out it.
If this post was useful then I am happy and as additional tip would like to share one bestseller course which is great and worst each penny. I strongly recommend to give it a try The Complete Android N Developer Course
This course will teach you an Android App Development while building real apps including Uber, Whatsapp, and Instagram!
Have fun!
This course will teach you an Android App Development while building real apps including Uber, Whatsapp, and Instagram!
Have fun!
This is one of the unique post.This is one of the challenging post.Nice to read about your post.
ReplyDeleteAndroid app developer
Thanks :)
DeleteThis comment has been removed by the author.
ReplyDeleteThe zooming and scrolling goes to infinite. Can we restrict that to some point?
ReplyDeleteI am also facing the same problem. Zooming is going to infinite...how to restrict it.
DeleteAlso, I am trying to load the image on the Button Click event. The Touch method is being run but zooming of image is not happening. Please suggest how to implement the same on the button click even in a new screen.
use this code
Deletecase MotionEvent.ACTION_MOVE:
if (mode == DRAG) {
matrix.set(savedMatrix);
matrix.postTranslate(event.getX() - start.x, event.getY() - start.y);
} else if (mode == ZOOM) {
float[] f = new float[9];
float newDist = spacing(event);
if (newDist > 10f) {
matrix.set(savedMatrix);
float tScale = newDist / oldDist;
matrix.postScale(tScale, tScale, mid.x, mid.y);
}
matrix.getValues(f);
float scaleX = f[Matrix.MSCALE_X];
float scaleY = f[Matrix.MSCALE_Y];
if (scaleX <= 0.5f) {
matrix.postScale((0.5f) / scaleX, (0.5f) / scaleY, mid.x, mid.y);
} else if (scaleX >= 6.0f) {
matrix.postScale((6.0f) / scaleX, (6.0f) / scaleY, mid.x, mid.y);
}
}
break;
thank u very much
DeleteMay I please have the reply...
ReplyDeleteI have used this code in my android application https://play.google.com/store/apps/details?id=com.blogspot.vmustafayev4en.gdl
ReplyDeleteIn this program version not enough for u?
This comment has been removed by the author.
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteI'm wondering how you can limit the zoom. Like Sarma already said, to stop the infinity...also I would like to know how to center the image and zoom in a bit in default.
ReplyDeleteAnd how do keep the image inside the viewscreen?
DeleteMy Phone is Galaxy S II and android version is 2.3.3, may be why I have normal window in my application
Deletecan please explain me,how to zoom images in view flipper..??
ReplyDeletegood tutorial ...
ReplyDeletethx...
Not at all :)
Deletegood work Mr.Vasif, can you explain how this zooming can be implemented with more than 1 image in viewflipper?
ReplyDeleteSorry I don't know :(
DeleteThese articles have got thorough discernment without unclear the readers.
ReplyDeleteroot android phone
Excellent, thank you very much, helped me a lot.
ReplyDeleteGreetings from Colombia ....
fanelmar
Not at all, Good lucky :)
DeleteThank you for this easy to follow tutorial.
ReplyDeleteur post is very usefull thanks....
ReplyDeletehave one doubt..can i use two imageviews for drag n zoom in ur coding??
thanks in advance
Very useful post thanx
ReplyDeleteYaaa..This is Really UseFull Dear....
DeleteNot at all, please share our official web site with friends www.handysofts.com
Deletevery nice tutorial!
ReplyDeletethank you very much.
Hassy
Sorry This is not working, send me full source file on hemant.katariya26@gmail.com
ReplyDeleteIn the code of 5., you should put this code
ReplyDeleteiv.setScaleType(ScaleType.MATRIX);
then it works.
Thanks a lot for gr8 tutorial...
ReplyDeleteJust Awsmn Work Bro........Was Facing this problem from last many days..Thanks lot
ReplyDeleteI want to know if it possible to do zoom-in-zoom-out and drag with RelativeLayout???
ReplyDeleteReally good tutorial...it helped me a lot.
ReplyDeleteCan please tell me how to rotate same image??
HI Please check below URL. Need your support.
ReplyDeleteI need to create zoom and then able draw on zoom image part . i have done it but when i am saving image when its in zoom i just get image only zoom part which is display in device. Renaming all parts is missing.
http://stackoverflow.com/questions/26924062/android-zoom-image-and-draw-line-on-image-with-current-zoom
HI Please update me if you get something in my question. waiting for your replay.
Deletethankss
DeleteHi Vasif,
ReplyDeleteIt's very good one, but how to apply zoom & rotate for multiple images ?
can i integrate with view flipper control??
ReplyDeleteGreat Post . Thanks :)
ReplyDeleteNot at all, please share our official web site with friends www.handysofts.com
Deleteplz tel me how to zoom along x axis and y axis independently????
ReplyDeletethanks man...
ReplyDeleteNot at all, please share our official web site with friends www.handysofts.com
DeleteWhat if i put Linear Layout in place of Frame Layout?
ReplyDeletewill it work?
not at all working...!!!!!!!!!!!!
ReplyDeleteHow to make zoom work in scrollview?
ReplyDeleteI tried most of the sample but in all that image in the imageview is get Zoom-in and Zoom-out..but i got idea over this blogAndroid Training in velachery | Android Training in chennai | Android Training in chennai with placement
ReplyDeleteThanks a lot! You made a new blog entry to answer my question; I really appreciate your time and effort.
ReplyDeleteAndroid Training in chennai | Best Android Training in chennai|Android Training in chennai with placement | Android Training in velachery
Great post! You have an entry for the new blog answer to my question.I really appreciate your effort.
ReplyDeleteSelenium Training in Chennai | Best Software Testing Training in Chennai | Android Training in Chennai | Android Training in Chennai with placement
how to stop drag image?
ReplyDeletehttps://youtu.be/0CoX5WUL6_8
ReplyDeleteprivate float spacing(MotionEvent event) {
ReplyDeletefloat x = event.getX(0) - event.getX(1);
float y = event.getY(0) - event.getY(1);
return FloatMath.sqrt(x * x + y * y);
}
cannot resolve "sqrt"
replace with
Deleteprivate float spacing(MotionEvent event) {
float x = event.getX(0) - event.getX(1);
float y = event.getY(0) - event.getY(1);
return (float) Math.sqrt(x * x + y * y);
}
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.
ReplyDeleteClick here:
angularjs training in rajajinagar
Click here:
angularjs training in marathahalli
This comment has been removed by the author.
ReplyDeleteThe blog which you have shared is more informative… Thanks for your information.
ReplyDeleteAndroid Training in Chennai
Android Training in Coimbatore
Android Training in Bangalore
Android Training in Madurai
You are doing a great job. I would like to appreciate your work for good accuracy
ReplyDeleteRegards,
Selenium Training Institute in Chennai | Selenium Testing Training in chennai
Thank you for allowing me to read it, welcome to the next in a recent article. And thanks for sharing the nice article, keep posting or updating news article.
ReplyDeleteoppo service center chennai
oppo service center in chennai
oppo service centre chennai
I am reading your post from the beginning, it was so interesting to read & I feel thanks to you for posting such a good blog, keep updates regularly..
ReplyDeleteAndroid Training Institute in Noida
Java Training Institute in Noida
Totalsolution is the one of the best home appliances repair canter in all over Delhi we deals in repairing window ac, Split ac , fridge , microwave, washing machine, water cooler, RO and
ReplyDeletemore other home appliances in cheap rates
LCD, LED Repair in Janakpuri
LCD, LED Repair in Dwarka
LCD, LED Repair in Vikaspuri
LCD, LED Repair in Uttam Nagar
LCD, LED Repair in Paschim Vihar
LCD, LED Repair in Rohini
LCD, LED Repair in Punjabi Bagh
LCD, LED Repair in Delhi. & Delhi NCR
LCD, LED Repair in Delhi. & Delhi NCR
Washing Machine repair on your doorstep
Microwave repair on your doorstep
we are one of the top rated movers and packers service provider in all over india.we taqke all our own risks and mentanance. for more info visit our site and get all details and allso get
ReplyDeleteamazing offers
Packers and Movers in Haryana
Packers and Movers Haryana
Best Packers and Movers Gurugram
Packers and Movers in Gurugram
packers and movers in east delhi
packers and movers in south delhi
packer mover in delhi
cheapest packers and movers in faridabad
best Packers and Movers Faridabad
Thanks for giving great kind of information. So useful and practical for me. Thanks for your excellent blog, nice work keep it up thanks for sharing the knowledge.
ReplyDeleteAWS Training in Chennai | AWS Training Institute in Chennai
Great post. An elegant and an attractive entrance for your homes is now made possible with the help of Elite gates. Choose the Elite gates for your homes and make your investment a smart one which adds more value to your homes. The choice of Elite gates with high safety technology also allows theft free and safety homes for your loved ones at your homes.
ReplyDeleteChoose the right brand and enjoy the exiting features and advantages!
automatic gates
The revolutionary technology of the Nibav Home Lifts has changed the way people and goods are being transported vertically within their homes, and furthermore, has done so in an environmental friendly manner. Rather than using cables or pistons, this unique residential elevator is powered by the most abundant resources in the world ‘AIR’.
ReplyDeleteClick here to know more: vacuum elevators India | vacuum lifts
What a great online source of information about this topic. You have done great work. Keep continuing to share such kinds of post. Keep it up.
ReplyDeleteSoftware Testing Training in chennai | Software Testing Training in annanagar | Software Testing Training in omr | Software Testing Training in porur | Software Testing Training in tambaram | Software Testing Training in velachery
Grab the Oracle Training in Chennai from Infycle Technologies, the best software training and placement center in Chennai which is providing technical software courses such as Data Science, Artificial Intelligence, Cyber Security, Big Data, Java, Hadoop, Selenium, Android, and iOS Development, DevOps, etc with 100% hands-on practical training. Call 7504633633 to get a free demo with more info.
ReplyDeleteIt let you access all the components and programs remotely from anywhere and any device to get full control over the connected system. It is .Download Vnc Server Full Crack
ReplyDelete