Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to put views on top of an ImageView, in relation to the ImageView's content size?

Background

Suppose I want to show an image of the something using an ImageView, and I want to put new views on top of it (animated ImageViews that show pins on a world map image, for example, or a Switch view on top of a smartphone image).

This means that no matter how the ImageView shows the image, the views should be in it, inside correct spot, with the same size as specified, or in a size related to the imageView itself

The problem

As opposed to other views, the ImageView can have a certain size, but its content is something else (to keep aspect ratio).

What I tried

I tried to use ConstraintLayout, but this can't really help, because the ImageView can change its size (for example when changing orientation), and thus ruining the position I've given the views compared to the ImageView.

I've found some libraries that can handle a similar thing (like here) and I even asked a similar question before (here), but all those solutions are for a static image within the ImageView, yet what I search for is adding a view on top of an ImageView.

The question

How do I put the views on top of the ImageView's content correctly?

How do I also scale the views down/up compared to the size of the ImageView's content ?

Can ConstraintLayout be used to change the scale of the views according to the ImageView's size ?

like image 595
android developer Avatar asked Jan 10 '17 13:01

android developer


People also ask

Which layout is used for overlapping of views?

Before Coordinator Layout, Frame Layout was used, but using more than one views in Frame Layout results in overlapping of views over one another.

How do I overlay one picture on another android?

Tap Edit and choose an image from your Photo Library or Stock Images. Scroll through the bottom menu and tap on Overlays.

Can ImageView be clickable?

You can make a view clickable, as a button, by adding the android:onClick attribute in the XML layout. For example, you can make an image act like a button by adding android:onClick to the ImageView .

Which method from the ImageView class allows you to adjust the transparency of an image?

image); image. setAlpha(150); // Value: [0-255]. Where 0 is fully transparent // and 255 is fully opaque. Set the value according // to your choice, and you can also use seekbar to // maintain the transparency.


2 Answers

Make FrameLayout with wrap_content around ImageView. Then you could set SwitchView on top of ImageView. You could align it to center, side or corners and using margins to get some fine position.

It still won't scale with image, but you can get pretty good results. If that doesn't fit you, you can programatically get width/height of ImageView and alter position (or margins) of SwitchView accordingly.

like image 94
JoKr Avatar answered Sep 20 '22 14:09

JoKr


With below you can manage width of switch or any other view as per image view width

android:layout_alignLeft="@+id/imageView"
android:layout_alignRight="@+id/imageView"

<Switch
    android:id="@+id/swi"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/imageView"
    android:layout_alignRight="@+id/imageView" />

<ImageView
    android:id="@+id/imageView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/swi"
    android:src="@drawable/download" />

like image 31
Pavya Avatar answered Sep 23 '22 14:09

Pavya