Android imageview with zoom

Android imageview 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

Источник

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.

Читайте также:  Обновление андроид 12 для асус рог фон 3

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.

Читайте также:  Hill climb для андроида

Источник

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 🙂

Источник

ImageView library that allows full screen pinch zoom?

I have a FrameLayout that contains an ImageView which I’d like to be able to zoom in on. How can I enable a «pinch-to-zoom» in the ImageView?

I’d like to be able to not have the zooming be restricted to the size of the ImageView but rather to the size of the containing FrameLayout as illustrated here (different app):

4 Answers 4

you can use android studio for doing that , here is steps how to achive it .

you may refer to this link to use it by

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.

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 Android studio sync gradle file. (Large preview)

Читайте также:  Recovery для android 4pda

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.

8- Add some margin above the ImageView and place it in the center.

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.

10- 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.

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.

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.

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.

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

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.

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.

22- Great now you have a fully working app with pinch to zoom Android function 🙂

Источник

Оцените статью