Android ImageView node inside RelativeLayout parent

I would like to know how I can crop ImageView that have a scaled background image using a fixed width and height on it parent .

Basically, I want the image to be scaled using the ImageView android:background , and then I want to crop the part of the image that is outside the parent borders.

So far I have this code:

 <RelativeLayout android:id="@+id/time_foregrd" android:layout_width="57px" android:layout_height="100px" android:layout_marginLeft="100px" android:layout_marginTop="285px" android:clipChildren="true" > <ImageView android:layout_width="57px" android:layout_height="338px" android:minWidth="57px" android:minHeight="338px" android:background="@drawable/time_foreground" /> </RelativeLayout> 

But that doesn't work ... What am I doing wrong?

+4
source share
3 answers

Ok, I managed to do this. Instead of using RelativeLayout I used FrameLayout and it worked perfectly.

Here is the code:

 <FrameLayout android:id="@+id/time_foregrd" android:layout_width="57px" android:layout_height="100px" android:layout_marginLeft="100px" android:layout_marginTop="285px" > <ImageView android:layout_width="57px" android:layout_height="338px" android:layout_gravity="bottom" android:scaleType="fitXY" android:src="@drawable/time_foreground" /> </FrameLayout> 
+2
source

I was able to do the same in my RelativeLayout using

 android:scaleType="centerCrop" 
0
source

1- wrap this element with relativeLayout.

2- add this to your element: android:layout_gravity="center_vertical"

it will act just like ImageView scaleType = "centerCrop"

  <LinearLayout android:id="@+id/ll_wrapper" android:layout_width="match_parent" android:layout_height="250.0dp" > <TextureView android:layout_width="match_parent" android:layout_height="451.0dp" android:id="@+id/tv_YourElement" android:layout_gravity="center_vertical" /> </LinearLayout> 

Android: layout_gravity = "center_vertical", "Center_horizontal", "center", ...

0
source

Source: https://habr.com/ru/post/1498049/


All Articles