Background in android layout is stretched - android

I put a background image to my Relative layout using android:background="#drawable/rajat2", where rajat2 is an image in all the drawable folders.
It sets the background to my layout but the background comes stretched to the whole screen.
I've tried putting the wallpaper through java code too but it still behaves the same!

If your drawable size is not exactly the screen size, the background will be stretched to the entire screen size. Consider creating the drawable for each type of screen size so it doesn't appear stretched.
If you don't want the image to take up the entire screen size, consider using an ImageView instead, and placing that inside your layout.

use draw9patch for the background image. it well help you.

If You are developing an application then set the layout width and height as match parent
and use 9 patch image for more exact value what you want

Related

How to set background image scale type of a theme?

I'm using a potrait photo as the background image of my application. but when the orientation changes, the background image is streched and ugly. I dont want the image to fit XY .. instead of that, I want it to be centerCropped .. remember , its not a background image of any linear layout. its the background of application(theme) .
I think this is possible, I've seen contact+ to do this in their app.. But I don't know how do they do it. Can anybody help me out please?
NOTE: I have used and gravity but it doesn't help.. The image is still fitting XY.not getting cropped to keep its aspect ratio with filling the window as well .
Is the background image of your app being set either by android:background="..." in your layout xml,
or programmatically by calling setBackgroundResource()?
If so, this sets the background resource of a View.
A View's background always stretches to fill up all of the View
width and height, losing its aspect ratio when layout dimensions change
such as when device orientation moves or
when the application runs in devices
with other screen aspect ratios. There is no way of configuring this
to happen otherwise.
Workarounds:
Convert your background to a Nine-Patch file, in order to the
stretching is
applied in selected areas of the image only. This may or may not fit well
to the design of your app.
Make your application detect changes in your layout. When it happens,
build a new background image on-the-fly keeping the aspect ratio of the original image, and set the View's background to the new image.
I've posted
a description on how to keep aspect ratio of View background image
using this technique; hopefully it can serve as a starting point.

Android ImageView scaleType

I have an image to be used a background in activity:
I want this image to fit screen by its height.
It means that for wide-screen smartphones, I want my image to be fit by height and centered:
and for square-screen smartphones, I want my image to be cut:
I use ImageView for the image. What scaleType should I use? Looking at the second figure, I'd say to use android:scaleType="centerInside". But looking at the third, I'd say to use android:scaleType="centerCrop". What is correct?
Your evaluation of the different scaleType's is correct. If you want the whole image to be visible, use "centerInside", or if you want to fill the whole view then use centerCrop.
To use a mix of both, you can set the scaleType in your onCreate() method. Based on the behavior you want to have, you can check the orientation or size of the screen and set the appropriate choice.
imageView.setScaleType(ScaleType.CENTER_INSIDE); // or ScaleType.CENTER_CROP
You can have two layouts, one for each of your configurations. You can then load the proper one at the activity's onCreate() call.
When you fit one side of image to background, you will face with two problems, first one is screen width is bigger than image's width or screen height is bigger than image height.So you will have empty space.
If you do not want to face with resolution problem and you want to fit both side of image to background, you need to use centerCROP.But as i said, if one side of image is not enough to fit background, image gets bigger till it will be filled.
Use centerCrop because centerInside doesn't scale an image in a view and you have to create the image with appropriate height to achieve wide-screened smartphones background filling. Or alternative you could use fitCenter to get uniformly scaled image by both axes which fills the all background.

Scale down ImageButton / ImageView without leaving space around

I have some problems resizing ImageViews and ImageButtons.
Let's say that I have a Layout that has a rectangular shape (I don't want to know if it is a horizontal or vertical rectangle) and a ImageButton that contains a transparent background and as ImageResource a square image.
I want to keep the button square, so I use setScaleType(ScaleType.FIT_CENTER) to stretch the button. It works well.
The problems come when the button needs to be REDUCED to fit the rectangular layout, instead of stretched: in that case, the image is reduced correctly, but the space reserved in the layout is the one that would be reserved by the image if I hade made it crop.
This is what I think that happens:
the image is put in the layout
the space in the layout is reserved
AFTER THIS the image is resized
if the space asked is increased, the layout is enlarged, otherwise nothing is done
as a consequence in the layout the image results rounded by A LOT of empty space if the image needed to be reduced.
The classical problem is: I have a layout that should contain one row with - say - six square buttons. IF the button size is larger than the height of the Horizontal LinearLayout, the buttons end to be distantiated with a lot of empty space, instead of touching them.
I tried using fixed sizes for the images, to force them resize before putting them in the layout, but this is not a solution for me. First of all I want it to be dynamic (ie: the layout could change size during the app lifetime and the images should follow that). Second of all, when I put the image into the layout it can easily happen that the layout is not set yet and its size returns zero.
Can anyone help me?
Thank you.
Just add the attribute
android:adjustViewBounds="true"
to your image view element in your layout. You can have a look to the post Unwanted padding around an ImageView

ImageButton Stretching Background Image

I am trying to create an ImageButton with no border but I am running into an issue with the sizing of the image button.
Using Eclipse ADT I drag an ImageButton to the layout and select my background image. The image button appears as follows:
As you can see there is a border between the background image and the image button perimeter which I would like to get rid of. To do so, I set the padding property to 0dp. The image button now looks like this:
Finally, I set the scale type to fitXY:
Notice that the image is stretched and is no longer the size of the original image. (I want the ImageButton size to be exactly the same size as the background image.)
Here is a snapshot of all 3 images next to each other to make the difference more clear:
I have tried re-sizing the image, setting the max size properties, etc. but nothing seems to prevent the image button from stretching the background image.
How do I prevent the image from stretching like it is?
UPDATE
I also tried setting scale type to matrix but here is what happened:
Just set your image as android:background for button and set layout_height and layout_width for button to wrap_content
Maybe this might help, but I also know its best practice to make different sized images from android development.
https://developers.google.com/appengine/docs/java/images/overview
Use an ImageView instead and set it to be clickable. Set the scaleType to matrix (which will cause the ImageView to be as big as the image inside of it).
The problem with ImageButton is that it adds a background that causes padding, etc.
You can set
android:background="#null"

Image Draw9Patched is stretched on the wrong area

After Draw9Patching some image I had in my drawable folder, I want to make it have a certain height.
At start I have this picture :
Then, when I apply ScaleType to "fitXY" in order to "strech" my image, and when I define my height, it's incorrect :
The arrow on the bottom of my imageview get stretched, however I mentioned on my draw9patch not to stretch this area.
Do you have any explanation of this problem ?
Thanks in advance!
I'm pretty sure 9patches are meant to be used as backgrounds of Views, not as the source of an ImageView. If you set the background of a TextView to this 9patch, everything will work fine.

Categories

Resources