Creating a drawable & zoomable image view in android
Goal:
Create an imageview that is drawable and zoomable,
That means when I press a button to on , it is drawable,
or when I turn off, that is zoomable.
*Notice the drawings should zoom align with the imageview
Recently I wrote a custom drawable image view like this:
The problem is , how to make it zoom-able as well? notice that the drawings should align with the imageview when zooming.
Attempt using some custom imageview library but no luck.
e.g. When I use photoview it can be zoom but the drawings not align, and zoom level will reset after I turn on / off the zooming https://github.com/chrisbanes/PhotoView
Also , find some other library like that but not fit the custom view case https://github.com/matabii/scale-imageview-android
Update1: demo
Recommended to reference this app, the drawing function is actually the same as what I am struggling to achieve, but I can’t figure out how they get it done
Update2: From Sandeep Maram Source
Thanks a lot Sandeep Maram, after testing the code, everything work well, the only thing remain is the drawings is not align with the zoom view. Please take a look at the screenshot
Before:
The circle is not scale up / down when zoom, would be really nice if fix that. also that is not matter if the image overlap the button.
Источник
How to implement zoom effect for image view in android?
I have to implement image zooming, I have tried with so many codes.But i didnt get full idea of gesture events. I want to implement when we apply double tap, image will be zooming according to the touch place(event x and y).I have to implement only zooming at this time no pan.Can any body suggest me ?
Edit: Actually i have implemented that viewflipper with images, if i try to apply pan and zoom effect, sometimes images will be changed.I want to implement zoom effect for where the user is clicked.
Give me some idea of that.
9 Answers 9
Lazy man can use this lib, Just import inside your project and
Here is one of the most efficient way, it works smoothly:
Place TouchImageView.java in your project. It can then be used the same as ImageView .
Example:
If you are using TouchImageView in xml, then you must provide the full package name, because it is a custom view.
Example:
I hope you are doing well.it often happens with all when they want to add new functionality in your app then normally they all search for libraries which are not good tactic because you don`t know what kind of code is in that libabry. so I always prefer to fork the libraries and add the useful classes and methods in my application code.
so when I stuck with the same issue, I make lots of much R&D then I find a class which gives the ability to zoomIn ,zoomOut and pinIn and out. so you can see that class here..
so as I told before, it is a single class. so you can put this class anywhere in your projects like utils folder.and put below lines into your XML files like:
and you can find that image view in your respected activity, as you did for all views like -:
that’s it for TouchImageView . enjoy our code 🙂
Источник
Android Image View Pinch Zooming
I am using code sample from Making Sense of Multitouch for zooming image view. On ScaleListener I added ScaleGestureDetector.getFocusX() and getFocusY() for content to zoom about the focal point of the gesture. It is working fine.
The problem is, on first multitouch the entire Image drawing position is changing to the current touch point and zooming it from there. Could you help me to resolve this issue?
Here is My Code Sample For TouchImageView.
And here how I used it within my activity.
8 Answers 8
and dont forget to set scaleType property to matrix of ImageView tag like:
and the variables used are:
You can use this class : TouchImageView
Using a ScaleGestureDetector
When learning a new concept I don’t like using libraries or code dumps. I found a good description here and in the documentation of how to resize an image by pinching. This answer is a slightly modified summary. You will probably want to add more functionality later, but it will help you get started.
Layout
The ImageView just uses the app logo since it is already available. You can replace it with any image you like, though.
Activity
We use a ScaleGestureDetector on the activity to listen to touch events. When a scale (ie, pinch) gesture is detected, then the scale factor is used to resize the ImageView .
Notes
- Although the activity had the gesture detector in the example above, it could have also been set on the image view itself.
You can limit the size of the scaling with something like
Going on
You will probably want to do other things like panning and scaling to some focus point. You can develop these things yourself, but if you would like to use a pre-made custom view, copy TouchImageView.java into your project and use it like a normal ImageView . It worked well for me and I only ran into one bug. I plan to further edit the code to remove the warning and the parts that I don’t need. You can do the same.
I made my own custom imageview with pinch to zoom. There is no limits/borders on Chirag Ravals code, so user can drag the image off the screen. This will fix it.
Here is the CustomImageView class:
This is how you can use it in your activity:
Add bellow line in build.gradle:
Custom zoom view in Kotlin
I made code for imageview with pinch to zoom using zoomageview. so user can drag the image off the screen and zoom-In , zoom-out the image.
You can follow this link to get the Step By Step Code and also given Output Screenshot.
In the TouchImageViewSample class scaling happens around the pivot point. Pixel belong to the pivot point doesn’t get effected by scaling of the image. When you are changing the pivot point, the view get redrawn and scaling happens around the new pivot point. This changes the location of previous pivot point and you see this as the image get shifted every time you touching down on the image. You have to compensate this shifting error by translating the image. See how this is done in my ZoomGestureDetector.updatePivotPoint() method.
ZoomGestureDetector
I created custom zoom gesture detector class. It can do scaling, translation and rotation at the same time. It also supports fling animation.
I used ZoomGestureDetector in a FrameLayout as below.
Источник
Pinch to Zoom Android ImageView Tutorial
Hi and welcome to another tutorial from Codingdemos, today you will learn how to build pinch to zoom Android function on an ImageView using 3rd party library called PhotoView. The experience will be a lot similar to what you see today in social media apps.
You will be building an app that have an Android ImageView and a TextView, when you tap on the ImageView an Android Alertdialog will appear on the screen showing you the image in full size where you can pinch and zoom on the image itself.
By the end of this tutorial, you will have an app that looks like this. (Large preview)
In this tutorial we will be using the following:
- – Android studio version 3.0
– Android emulator Nexus 5 with API 26
– Minimum SDK API 16
– PhotoView 3rd party library
1- Open up Android Studio and create a new project and give it a name, in our case we’ve named it (ImageZoom), choose API 16 as the minimum SDK, then choose a blank activity, click “Finish” and wait for Android Studio to build your project.
Create new Android Studio project. (Large preview)
2- Open up build.gradle (Module:app) and add PhotoView library.
3- Now you need to open up build.gradle (Project) and you need to add this line maven < url "https://jitpack.io" >inside (allprojects) tag.
4- Sync the project by clicking on (Sync Now).
Android studio sync gradle file. (Large preview)
5- Open activity_main.xml file, here you will add 3 views:
– Android ImageView that will hold the picture.
– Android TextView for the image title and description.
6- Add Android ImageView and make sure that you have added the image that you want to use for the ImageView inside the project drawable folder.
Android ImageView with image. (Large preview)
7- Reduce the size of the ImageView by adjusting android:layout_width and android:layout_height
Adjust width and height of Android ImageView. (Large preview)
8- Add some margin above the ImageView and place it in the center.
Android ImageView placed in the center with margin. (Large preview)
9- When you tap on the ImageView inside Android Studio preview window, you will see an empty space from top and bottom of the ImageView. You can fix that by using android:scaleType=»centerCrop» which allow the image to fill the remaining space.
Android ImageView with scaletype. (Large preview)
10- Add Android TextView which will hold the name of the picture, this TextView will be placed below ImageView (ivIcon) in the center, add some margin from the top, try to increase text size to (20sp) and change text style to (italic and bold).
11- Now you add the final TextView for this layout which will hold the description about the picture, this TextView will be placed below (tvName), try to increase text size to (16sp) and don’t forget to add some margin from the top so that it doesn’t appear to close with (tvName) TextView.
Flower title and description. (Large preview)
12- Now you are done working on activity_main.xml file, next you need to open up MainActivity.java file and initialize Android ImageView and Android AlertDialog.
13- Initialize ImageView (ivIcon).
14- Next you need to change the shape of ImageView (ivIcon) to circle using RoundedBitmapDrawable and Bitmap .
15- Build and run the app to see the progress.
Changed the shape of ImageView (ivIcon) to circle. (Large preview)
16- Now you need to work on Android AlertDialog, the AlertDialog will appear on the screen when you try to tap on ImageView (ivIcon). Before you start that, you first need to create another layout file that you will use it later for AlertDialog.
17- Create a new layout file and name it (dialog_custom_layout.xml), this file will have a PhotoView that will match the width and height of the screen.
18- Now go back to MainActivity.java file, to be able to tap on the ImageView you will need to use setOnClickListener and inside onClick is where you will create AlertDialog.
19- Build and run the app to test out Android pinch and zoom function.
PhotoView image doesn’t completely fill out AlertDialog. (Large preview)
20- Hmm it seems that the image doesn’t fill the entire AlertDialog! You can actually fix it by using android:scaleType=»centerCrop» .
21- Build and run the app to see the result.
PhotoView image appears inside AlertDialog. (Large preview)
22- Great now you have a fully working app with pinch to zoom Android function 🙂
The source code for this tutorial is available on GitHub, I hope you find this tutorial helpful and if you have any question please post them in the comment below.
Источник
Android image with zoom
A simple pinch-to-zoom ImageView library for Android with an emphasis on a smooth and natural feel.
Simply add a ZoomageView as you would any typical ImageView in Android. The scaleType that you set on your ZoomageView will determine the starting size and position of your ZoomageView’s image. This is the inherited ImageView.ScaleType from Android. With a ZoomageView, the fitCenter or centerInside scale types usually make the most sense to use, fitCenter being Android’s default scale type.
If using a ZoomageView with a view pager, it is recommended that ViewPager2 is used.
Restricts the bounds of the image so it does not wander outside the border of the ImageView when it’s smaller than the frame size, and restricts the bounds to stop at the edges of the ImageView when the image is larger than the frame size. Default value is false.
Image will animate back to its starting size whenever it is reset if true, and will snap back to its starting size when false. Default value is true.
Determines at what times the image will reset to its starting size. Note that UNDER, OVER, and ALWAYS all have the effect of resetting the image to its starting position if its size has not changed. Default value is UNDER.
This will cause the image to pull itself into view on-screen if it is partially off-screen. Default value is true.
The minimum allowed scale for the image. Ideally this should be less than 1, must be greater than 0, and must be less than maxScale. Default value is 0.6.
The maximum allowed scale for the image. Ideally this should be greater than 1, must be greater than 0, and must be greater than minScale. Default value is 8.
Sets whether zooming is allowed. Default value is true.
Sets whether translation is allowed. Default value is true.
Sets whether double tap to zoom functionality is enabled. Default is true.
Sets the scale factor for double tap to zoom functionality. Default is 3.
Special thanks to @mchowning for all his help
Источник