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,495
  • PinchZoomAndMove_1.45.zip
    115 KB · Views: 4,428
Last edited:

susu

Well-Known Member
Licensed User
Longtime User
I got Forced Close when try your code.
 

susu

Well-Known Member
Licensed User
Longtime User
Yes, I added icon.png manually but it still forced close.
 

vb1992

Well-Known Member
Licensed User
Longtime User
Susu, seems like a gesture library problem?
What version of b4a you running, and also the gesture library?

for my test that worked:

b4a: 1.92
gestures: 1.20
 

susu

Well-Known Member
Licensed User
Longtime User
Thank you vb1992, I updated Gesture library and there's no forced close but I can't pinch zoom. My device uses Android 2.3.4, is that the reason?
 

Djembefola

Active Member
Licensed User
Longtime User
Susu, have you tried to "pinch-in" (make the image smaller)?
 

vb1992

Well-Known Member
Licensed User
Longtime User
I would add some debugging to see if your phone ever hits
that part of the gesture routine. It's commented where
the pinch/zoom happens.

I use 2.3.6 and it's working, I also tested it on a tablet
and a cheap cricket phone too, worked.

So far this has been the best pinch/zoom/move solution for an image
with b4a :sign0098: (unless I missed a library somewhere on here)

Just needs rotate image X degrees via gestures.
(Going to try this: http://www.b4x.com/forum/basic4android-updates-questions/21480-how-rotate-imageview.html#post124236)
(Java solution: http://stackoverflow.com/a/10104318/1016322)

Can apple sue us if we use it in an app? :)
 
Last edited:

Dominex

Active Member
Licensed User
Longtime User
Thank you vb1992, I updated Gesture library and there's no forced close but I can't pinch zoom. My device uses Android 2.3.4, is that the reason?
You have to try to give us more details to understand your problem. Personally, I have always worked even on different versions of Android.
 

Dominex

Active Member
Licensed User
Longtime User
So far this has been the best pinch/zoom/move solution for an image
with b4a :sign0098: (unless I missed a library somewhere on here)
Thanks

I do not think it is difficult to use the rotation. The code you indicated, however, do not believe takes account of the fact that a imagines rotated has a dimension greater than a unrotated. However it would be complicated.

Can apple sue us if we use it in an app? :)
Certainly :D
 

susu

Well-Known Member
Licensed User
Longtime User
You have to try to give us more details to understand your problem. Personally, I have always worked even on different versions of Android.

I compiled and installed your code on my device. Everything seems ok, I see image at center of the screen but I can't pinch to zoom in / out. I insert some Log("***Here***") to indicate where the code is but there's nothing in log cat.
That's all I can say :)
 

Informatix

Expert
Licensed User
Longtime User
So far this has been the best pinch/zoom/move solution for an image
with b4a :sign0098: (unless I missed a library somewhere on here)

What's the difference with the TouchImageView library ?
And for the gesture part, with the Gesture Detector library ?
The Dominex's code brings nothing new, nor improvement, and doing the same with these two libraries is much simpler (and they can do a lot more). BUT this code is written in Basic and can be modified to suit specific needs. It's an advantage. And Dominex shared his knowledge. That's always a good thing.

@Dominex
In your distance computation (Ipotenusa), dividing by Density is useless.
 

vb1992

Well-Known Member
Licensed User
Longtime User
this code is written in Basic and can be modified to suit specific needs


Exactly. but, They are all good.

I personally like to see variations so I can compare and test on multiple devices to see
how everything runs, speed and stability wise. especially with a pinch/zoom of an image
 

Informatix

Expert
Licensed User
Longtime User
I personally like to see variations so I can compare and test on multiple devices to see
how everything runs, speed and stability wise. especially with a pinch/zoom of an image

I agree.
I thought you were considering that was a better code than the Warwound's TouchImageView library. They use different methods but I can't see any difference between them otherwise.

There's a thing annoying me: the post title says "Pinch Zoom and Move View tutorial". Where's the tutorial? Dominex should explain how he does.
 

Dominex

Active Member
Licensed User
Longtime User
@Dominex
In your distance computation (Ipotenusa), dividing by Density is useless.
I thought that eliminating the differences of density from display to display I would get a proportion of the distance of movement of the fingers may like each other, but I admit to not having studied with testing this thing.

If you do not need it can be removed. Simple. ;)
 

Dominex

Active Member
Licensed User
Longtime User
There's a thing annoying me: the post title says "Pinch Zoom and Move View tutorial". Where's the tutorial? Dominex should explain how he does.
I thought that the post code was enough to understand how to do, for me it is. Moreover, the code is so short that there is not much to explain.

However, there is no problem, I corrected the title.

I wanted to try my hand with the Pinch zoom because I had to use in my app, I just tried to do everything myself, and then I have placed my result.
 

Informatix

Expert
Licensed User
Longtime User
I thought that the post code was enough to understand how to do, for me it is. Moreover, the code is so short that there is not much to explain.

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.
 

Informatix

Expert
Licensed User
Longtime User
I thought that eliminating the differences of density from display to display I would get a proportion of the distance of movement of the fingers may like each other, but I admit to not having studied with testing this thing.

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