Add buttons to specific positions on an image - android

I was trying to add some buttons to some specific positions on an image. I am making use of webview to display the image because it embeds zoom in/out function; I have tried AbsoluteLayout to put buttons on it but when I zoom in, the button moved..
What should I do then? How can I make the button stick to one position of an image no matter how I zoom in and out?

It seems impossible to get the desired effect if you use WebView, since WebView's scaling does not affect the child view added to it.
If you are working on API 11+, you can use a ViewGroup such as FrameLayout to hold the image and the button, then call View.setScaleX(float sf) and View.setScaleY(float sf) on FrameLayout to scale it, so that all of its child will be scaled too.
I don't have a good solution for API level before 11, since View class does not contain transform information before API 11, maybe you can just recursively adjust the LayoutParams of all the Views to get the effect, I didn't try it, though.

Related

Setting points to click on my Map ImageView

I have a blueprint image in my app. The user will be able to click on some pre-established points and after they click on this point, a dialog will show some information.
My question is how I add the specific points in my ImageView Blueprint and the click function on these points?
EDIT 08/08/2014
My ImageView now has pinch zoom and the app will have more than 1 blueprint.
Thanks and regards!
You could go about this multiple ways. If your blueprint is full screen on the users device then you could create your own custom layout to encapsulate the imageview and override the ViewGroup's onDraw an onTouch methods to add your points. Then you could have a method like
public void addPoints(float[] points) { // do something here }
Another option is to extend the ImageView itself and make your own custom imageview and then again override onDraw and overlay the points ontop of the image. YOu can set the bitmap (your blueprint) as the background and then drop ontop of that. You would have to figure out where the specific points are though depending on the size of your image and the device, unless you set the size to be pixel density independent.
The first thing that comes to mind is you could use RelativeLayout in the XML file. This allows you to layer elements on top of one another if you so choose. You could use this to position invisible buttons above the image.
With RelativeLayout, each button could be positioned using its margin attributes, and it should be fairly simple to make the button invisible by assigning the transparent color for its background and providing no text.

Android imageview not behaving with setAlpha for transparent image

I have a view that I am dynamically adding two ImageView elements to at runtime before adding the view to the parent container to display it. On drag events, I want to set the image transparency to hidden or shown depending on the direction of move. I am trying to use imageView.setAlpha(0) to make the image invisible but even though the code is being called, the images stay visible or partly visible from when their alpha value was changed earlier to say 0.5.
Is there any reason an ImageView would refuse to trigger on a programmatic setAlpha()?
Set it to 0.0. To change the opacity, you need to use the setAlpha(float) not setAlpha(int)

Positioning views in Horizontal Scroll View

I'm trying to build a point-and-click adventure for Android without using any pre-written engine, but I'm stuck in a really crucial point!
I have a HorizontalScrollView bigger than the screen, so the user can scroll left and right in portrait mode to search around rooms, now what I need is to insert items that the player can use inside this View.
I'm trying to use static ImageView, but I'm really confused on how to insert Views in an absolute position inside the HorizontalScrollView. All I know is that Android manages Views location relative to other views (align on top, next to, bottom of), but what I use if I need to position a View in a specified position using specific coordinates without worrying that the image will be misplaced in a different screen size for other Android devices?
I really am confused on how position views in Android :/
Consider creating your own view instead of abusing HSV.
Custom views, doDraw() method, canvas and even gesture detection is not that hard.
Here is a link that will help you with basics, simple viewport implementation for android with scrolling via key events: http://sonnygill.net/android/android-viewport/
what I use if I need to position a View in a specified position using specific coordinates without worrying that the image will be misplaced in a different screen size for other Android devices
Actually, that's exactly the reason ViewGroup (layouts) were created.
You should choose the right layout based on your requirements.
If you do want to position the views by yourself, you can create your own custom ViewGroup by extending one of the view group classes and override the onLayout(...) and onMeasure(...) methods which are used to position and measure child views (in you case, probably the ImageViews) respectively.
You can use this example of FlowLayout as a reference on how you can write your own custom ViewGroup.
Please note that if your content is bigger than the screen as you mentioned, the HorizontalScrollView should be a parent of a single child (which should be the ViewGroup containing your images).

Implement zoom functionality in layout having images

I want to implement pinch zoom functionality for a layout which have a background image and on background image it have 4 another image . see the image
I have 2 idea to implement my problem
Make a parent layout (framelayout) which will have these 4 images as child layout(imageview) and somehow (i dont know how) if i am able to make it pinch zoom. my problem will get solved
i can use webview but i dont know how to put these images in webview.
can anyone help me to get it solved.
Note : i want pinch zoom functionality only for background image. as if background image will get zoom, other images will also get zoomed. i dont want independent zoom for images 1 2 3 4.
Thanks
All layouts are already put in to the default FrameLayout, so, Merge as a root layout is enough.
As for zooming the main image, the only problem IMHO, is that these children could sometime catch the event instead of the root layout. So, you need do make them unfocusable and disabled. After that all events will be catched by the parent.

Animate viewgroup without animating children

i have built a custom viewgroup and m beginning to implement animations tot his viewgroup. the problem i am having is that i need the viewgroup to be able to animate without it's children being animated by the same animation.
Lets say i am scaling the viewgroup to 2x size, but i still want to keep children at the same size (thus fitting more children into the viewgroup).
Right now i am calling the animation as follows:
ScaleAnimation expandAnimation = new ScaleAnimation(1,2,1,2);
this.startAnimation(expandAnimation);
Thanks in advance! :)
You can't directly do what you're seeking to do with the old Android animation API. You've already noticed one limitation - that scaling the group also includes the children.
The second limitation is that the old Android animation API only affects the visual render of the item you're animating, and only for the time the animation is active. In other words, it doesn't actually alter the size or positioning of the item in question in terms of view layout. It only temporarily renders it a different way. So for instance, to animate a button moving from x to y across the screen, you would need to run the animation, then when the animation completes actually reposition the button using the layout APIs.
Probably the way to do what you're looking for is:
Incorporate an extra ViewGroup
widget in your layout, positioned
and sized over top the widget
containing the "real" items.
Initially, it's invisible.
Make
it visible, and play the animation.
When animation complete, hide the
extra widget, then resize your real
ViewGroup widget via LayoutParams
and add your extra items.
You'll
probably need to tweak this general
outline to make the effect look
smooth. (For instance, resize the real viewgroup in the background before you remove the fake viewgroup from the screen).
Android introduced a newer, more flexible animation API in 3.0 Honeycomb, but unfortunately it's not available if you're targeting 2.x devices (i.e., phones). Hopefully this will be easier once the new APIs are mainstream.

Categories

Resources