Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent background image from stretching the view

Tags:

android

Whenever I assign background to a view that is laid out with wrap_content, if the background image is larger than a view, the view is stretched so that it can hold the entire background. Why is it so and how to prevent it? It occurs even if the image is 9-patch and has stretchable areas marked - why isn't the image shrunk to the size of the view?

like image 675
Fixpoint Avatar asked Jan 18 '11 11:01

Fixpoint


People also ask

How do I stop my background image from stretching CSS?

1 Answer. Show activity on this post. The 100% 100% background-size value means the background should stretch (100%) of the width of the element and (100%) of the height of the element. Have either of them set to auto , which will size the undefined dimension automatically, while preserving the images aspect ratio.

How do I make the background image fit my screen size?

Using CSS, you can set the background-size property for the image to fit the screen (viewport). The background-size property has a value of cover . It instructs browsers to automatically scale the width and height of a responsive background image to be the same or bigger than the viewport.

Why is image stretched?

If you added the image and changed the dimensions right away the image will appear to be missing one of the dimensions. Word reads this incorrectly and in turn stretches the image. You can tell your image might do this because it's missing part of the dimensions like the image below.

How do I fit a div image without stretching it?

Those images on the site you linked to are actual size, so the simple answer is just to resize the image. You can't really do this without "stretching" the image if the image happens to be less than 300px wide or tall, but you can do it without changing the ratio, which is what I think you mean.


2 Answers

You can use RelativeLayout and create two elements inside it. An ImageView for the Background and another view for your content. Use the android:layout_alignBottom to the the ImageView and set the value to the id of your content view. Also set the scaleType of the ImageView to fitXY.

<RelativeLayout android:id="@+id/relativeLayout1" 
  android:layout_height="wrap_content" 
  android:layout_gravity="center" 
  android:layout_width="fill_parent">

<ImageView android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:scaleType="fitXY"
  android:src="@drawable/chronometer_bg"
  android:id="@+id/imageView_chronometerBackground"
  android:layout_alignBottom="@id/chronometer" />

<Chronometer android:text="Chronometer"
      android:id="@+id/chronometer" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_centerHorizontal="true"
      android:textSize="32sp"
      android:gravity="center" />

</RelativeLayout>
like image 156
Mark Pazon Avatar answered Oct 11 '22 23:10

Mark Pazon


Either override the methods getSuggestedMinimumWidth and getSuggestedMinimumHeight of your layout view or the methods getMinimumWidth and getMinimumHeight of your background drawable to achieve that.

like image 26
Alexander Haroldo da Rocha Avatar answered Oct 12 '22 01:10

Alexander Haroldo da Rocha