Android Tutorial Pinch Zoom and Move View tutorial

[Class] Pinch Zoom and Move View

I had the need for my purposes to do the Pinch Zoom in contemporary Move to a View, now I put my code available to anyone who might be in need, just to get ideas on what to do.

Is required the library Gesture and Reflection of Agraham.

Events:
- Click
- LongClick
- Change

Property:
- CanMove
- CanZoom
- LimitArea
- LimitBorder
- Zoom

[UPDATE 1.00]
I changed the code in Class.

[UPDATE 1.10]
Added the following methods:
SetIfCanMove() - sets whether the View may translare or not.
GetIfCanMove - returns if the View can translate.
SetZoom() - sets the zoom.
GetZoom - returns the current zoom.

[UPDATE 1.20]
Improved zoom function, now the zoom is localized based on the touches.
Renamed Class in PinchZoomAndMove.

[UPDATE 1.30]
Added the possibility to set the BringToFront.
Added the possibility to set the Tag for a multi-View.
Added the "Click" event for the View.

[UPDATE 1.40]
Added the LongClick event for the View.
Added methods SetIfCanMove() - sets whether the View may zoom or not.
Inserted also Library version in Zip Class.
Changed the type of data sent with the Click events and LongClick.

[UPDATE 1.41]
Now works in Obfuscated mode. Thanks Informatix!
Improvements to minors.

[UPDATE 1.43]
Now the events are generated in different activity from the Main.
Added the following methods:
SetLimitBorder() - if it is True, the View will not come out from the edges of its parent.
SetLimitArea() - if True, the edges of the View will remain within its parent.

[UPDATE 1.44]
Added the Change event for the View.

[UPDATE 1.45]
Converted the property into methods for ease of use, but this requires a small change to the code of the old methods.

For suggestions and bug reports please do not hesitate to post it here.
 

Attachments

  • Screenshot_2012-11-02-20-02-13 (ridotta).jpg
    Screenshot_2012-11-02-20-02-13 (ridotta).jpg
    28.8 KB · Views: 2,461
  • PinchZoomAndMove_1.45.zip
    115 KB · Views: 4,391
Last edited:

Dominex

Active Member
Licensed User
Longtime User
In fact, that does not change anything to the result (same final size on the screen, same user experience), that's why I said it's useless. And Abs() is useless too in your ipo and ipotenusa formulas because a power can't be negative (- * - = +). These things take CPU time for nothing.
I did not know that the POWER always returns a positive result, good to know.
Instead DENSITY is necessary otherwise when you touch the screen with two clicks of the image is resized, you can try by yourself.
 

Dominex

Active Member
Licensed User
Longtime User
We deal here with a lot of beginners. Your code will be of no help for them, I'm afraid. They will use it as-is without understanding how it works. And I'm not even sure they will succeed in implementing it in their own app without issue.
I can't say to one of them "hey use the Dominex's code". No I'll provide him with a link to the Warwound's library. There are better chances that it fulfills his needs. The big advantage of your code is that you can show to everyone how these things work with code written in Basic. Don't you think it would be a great idea to share your knowledge, and not only your code?
I have no problem with you or with your code. It's just my two cents.
I understand what you mean, though, although I do not much interest, those who use my code it is ready, you need only specify the View, inceve, especially the library Gesture Detector to program despite having more features. Personally I doubt that a true beginner face before Gesture Detector with respect to my code ready.

I'm thinking of converting my code in a class, it would be even easier.
 

Informatix

Expert
Licensed User
Longtime User
I understand what you mean, though, although I do not much interest, those who use my code it is ready, you need only specify the View, inceve, especially the library Gesture Detector to program despite having more features. Personally I doubt that a true beginner face before Gesture Detector with respect to my code ready.

I'm thinking of converting my code in a class, it would be even easier.

You probably used an automatic translation tool because I don't understand your answer. Could you send it again in your native language? I see the words Gesture Detector, so I presume you're comparing your code to my library. You should compare your code to the Warwound's library (TouchImageView), not to my library. My library has a different purpose; it eases the detection of gestures, nothing more.
 
Last edited:

Informatix

Expert
Licensed User
Longtime User
Instead DENSITY is necessary otherwise when you touch the screen with two clicks of the image is resized, you can try by yourself.

I re-did a few tests and, finally, I agree. The changes are more natural with "/density" and there are no "size jumps" when you put both fingers on screen. That was not obvious on my device, but on a different device, it's noticeable. Good point for you.

Doing a class is a good idea. I think that eases a lot the use of your code.
 
Last edited:

Dominex

Active Member
Licensed User
Longtime User
You probably used an automatic translation tool because I don't understand your answer. Could you send it again in your native language? I see the words Gesture Detector, so I presume you're comparing your code to my library. You should compare your code to the Warwound's library (TouchImageView), not to my library. My library has a different purpose; it eases the detection of gestures, nothing more.
Volevo dire che sebbene ci sono delle librerie che fanno questo ed altro, usare il mio codice è semplice perché è già pronto, invece ad esempio la libreria Gesture Detector deve essere un minimo programmata.

I wanted to say that although there are libraries that do this and more, use my code is as simple as it is ready, however, for example the library Gesture Detector must be scheduled a minimum.
 

Dominex

Active Member
Licensed User
Longtime User
I re-did a few tests and, finally, I agree. The changes are more natural with "/density" and there are no "size jumps" when you put both fingers on screen. That was not obvious on my device, but on a different device, it's noticeable. Good point for you.

Doing a class is a good idea. I think that eases a lot the use of your code.
Well, now I'm just considering whether to add the rotation. :cool:
 

Informatix

Expert
Licensed User
Longtime User
however, for example the library Gesture Detector must be programmed a minimum.

Of course. It is a generic library. If you want to zoom something with the pinch gesture, you have to handle the increase/decrease in size, but that's not the most difficult part. Most users have no idea how to get the gesture itself, and do not know how to compute a distance with the Pythagoras theorem. The library is there to do these things for them. You are using yourself the Gestures library to do your stuff. My library is a complete replacement for this library. If you use my library, for example, your code will contain less lines of code and will be easier to understand than it is currently.
As I previously said, you should compare your code with the TouchImageView library, not with my library. The Warwound's library offers a lot more than your class, so you have to find something new to give your class some value, since you don't want to use it for an educational purpose. That's only my opinion, of course. ;)
 

zeuspower

Member
Licensed User
Longtime User
It is what I was searching !

BUT

after zooming and paning how my user can get back easy the original size-place of image ?

because in my app the user selects images from a database,and after zooming and paning a image, the next image is a small mess !
 

Dominex

Active Member
Licensed User
Longtime User
It is what I was searching !

BUT

after zooming and paning how my user can get back easy the original size-place of image ?

because in my app the user selects images from a database,and after zooming and paning a image, the next image is a small mess !

You can easily get size and position simply by using a View said in Globals. For example:

B4X:
Sub Globals
   Dim PZ As PinchZoomMove
   Dim imv As ImageView
End Sub

Then get set and what you need with imv.Left, .Top, .Width, .Height.
 

susu

Well-Known Member
Licensed User
Longtime User
I just found that if I compile with "Release (obfuscated)" option, your class will not work. But it's not a big problem to me :D
 

Dominex

Active Member
Licensed User
Longtime User
I just found that if I compile with "Release (obfuscated)" option, your class will not work. But it's not a big problem to me :D
I know, I honestly do not really know how the compilation (obfuscated), I've never used it. But if you fill in "Release" will it work?
 

susu

Well-Known Member
Licensed User
Longtime User
Yes, it worked with "Release" option.
 

Dominex

Active Member
Licensed User
Longtime User
[UPDATE 1.4]

Added the "LongClick" event for the View.
Added methods "SetIfCanMove()" - sets whether the View may zoom or not.
Inserted also Library version in Zip Class.
Changed the type of data sent with the Click events and LongClick.
 
Last edited:
Top