Picture

I want to display the image as a background in my application. Now I used this statement: android:background="@drawable/background" in <LineraLayout> . Here is the .xml code:

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="overbit.user.MainActivity" android:background="@drawable/background"> </LinearLayout> 

Now I get this output:

image

But I want this:

as

Maybe one of you can help me. Thanks.

+5
source share
4 answers

There is a BitmapDrawable that allows you to do this. First you need to create a resource with the ability to draw as follows (let it be a file in the project resources res/drawable/mybg.xml ):

 <bitmap xmlns:android="http://schemas.android.com/apk/res/android" android:src="@drawable/background" android:gravity="center" /> 

And then specify it as a background resource in your layout:

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="overbit.user.MainActivity" android:background="@drawable/mybg"> </LinearLayout> 
+7
source

You can accomplish what you are trying to do using BitmapRegionDecoder .

From the docs:

BitmapRegionDecoder can be used to decode a rectangle region from an image. BitmapRegionDecoder is especially useful when the original image is large and you only need portions of the image.

This allows you to fairly easily decode the Rect region of a particular Bitmap, the only thing you need to do is calculate the Rect region to decode relative to the bitmap.

+4
source

You need to resize the image in Photoshop or something to achieve the desired result (yet it can be very difficult due to the different screen sizes for Android smartphones). A workaround would be to change the parent layout to Relativelayout, and then use the image to display your image as follows:

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" xmlns:ads="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:layout_width="match_parent" //make sure its match parent android:layout_height="match_parent"// and this one also android:src="@drawable/yourBackgroundImage" android:scaleType="..."/> <LinearLayout>put your childviews here</LinearLayout> 

4-5 options are available for android: scaleType until you get the desired result. Also note that you need to use android: src , not android: background image attribute

+3
source

In android, the position of the ImageView is determined by the upper left of the image. I assume that the x direction is 1/3 of the width from the starting point. Now we can install the rod. Basically, the location of the pivot point is the location around which the view will rotate and scale.

 int displacementX = ImageView.getWidth() / 3; int displacementY = 10; imgview.setPivotX((float)displacementX); imgview.setPivotY((float)displacementY); 

Now that we have set the anchor point, we can use the scale type to match the image.

 imgview.setScaleType(ImageView.ScaleType.CENTER_CROP); 
0
source

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


All Articles