How to Implement an Android:Background That Doesn't Stretch

How to implement an android:background that doesn't stretch?

You should use ImageView if you don't want it to stretch.
Background images will always stretch to fit the view.
You need to set it as a Drawable to force the image aspect to the object.

Otherwise, if you are sticking with the Button idea, then you will need to force the scaling in the button to prevent the image from stretching.

Code:

onCreate(Bundle bundle) {
// Set content layout, etc up here

// Now adjust button sizes
Button b = (Button) findViewById(R.id.somebutton);
int someDimension = 50; //50pixels
b.setWidth(someDimension);
b.setHeight(someDimension);
}

How to fill image in background without stretching?

Place your image file in Res/drawable instead of Res/mipmap.

Res/mipmap is for app launcher icons only

Try this:

<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:id="@+id/view"
android:src="@drawable/landscape"
android:scaleType="center" />

Android. Layout background stretches

You can use an XML drawable that uses the tag to disable the stretching:

<bitmap android:src="@drawable/background" android:gravity="center" />

Don't stretch ViewPager background

Sorry, there are no 'centerCrop' option for background drawable. How I do it is set your use FrameLayout having the back contain the ImageView using centerCrop, then the front(top) will be your content with transparent background.



Related Topics



Leave a reply



Submit