I have a background image which should be scaled on the height, but keep its aspect ratio. I tried it with this code:
<ImageView
android:id="#+id/iv_background"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:src="#drawable/bg" />
This works on phones in portrait mode, but when I test it on a tablet (default landscape), it scales the width and cuts the image on top and bottom. Is this because of the xml code or because of the landscape?
Try something like this:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:scaleType="centerCrop"
android:src="#drawable/background_image"/>
<!-- Your other views -->
</RelativeLayout>
Related
I want to display an image on top of my activity that always fill the screens width. If I set its width to match parent and its height to wrap content there is a white stripe at the sides of the image on larger screen. How can I make that this image always fill the screen horizontally?
My xml:
<RelativeLayout 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"
tools:context=".MainActivity" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:contentDescription="#string/hello_world"
android:src="#drawable/tbbt" />
</RelativeLayout>
Thanks in advance.
change imageview to fit the complete screen width
android:scaleType="fitXY"
or
android:scaleType="centerCrop"
There are many images that appear in the ListView.
My list_view_item.xml (image in ivEventImage):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_height="wrap_content"
android:layout_width="match_parent">
<TextView
android:id="#+id/tvText"
android:autoLink="web"
android:linksClickable="true"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<ImageView
android:id="#+id/ivEventImage"
android:adjustViewBounds="true"
android:scaleType="fitXY"
android:src="#null"
android:layout_width="match_parent"
android:layout_height="match_parent" />
They have different sizes - 640 x 480, 1280 x 720, 1920x1080, 1080x1920 and others. In addition to different sizes, different orientation - portrait or landscape.
And all images in different aspect ratio.
How to make the display of images in ImageView, BUT with an aspect ratio of 3:2 and if the image height not fit to show only the middle part of the image. But not to break ratio.
Black square - ivEventImage.
Left - 1280x720, right - 1080x1920 (for example).
Use centerCrop instead of fitXY for android:scaleType as below...
android:scaleType="centerCrop"
So, ImageView xml will be...
<ImageView
android:id="#+id/ivEventImage"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
android:src="#null"
android:layout_width="match_parent"
android:layout_height="match_parent" />
I have an activity where a user can choose a photo through an intent. The expected behavior is as follows:
The image is sized to fill the parent width (minus margins).
This sizing should never increase the image, just scale down when needed.
Aspect ratio should ALWAYS be accurate.
Height should be scaled exactly as width is to maintain correct aspect ratio.
In portrait mode the maximum height is however big it can get before scrolling appears (just fill the screen).
This means that the width may not fully stretch to parent width if limited by the height.
In landscape mode the maximum height is irrelevant because scrolling is allowed, width should always be parent width.
Here's the portrait XML layout I am using:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#drawable/gray_background"
android:weightSum="1" >
<!-- Other controls here -->
<ImageView android:id="#+id/imgView"
android:layout_gravity="center"
android:layout_margin="5dip"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:contentDescription="#string/ImageToSend"
android:adjustViewBounds="true"
android:layout_weight="1"
android:scaleType="fitCenter" />
<!-- Other controls here -->
</LinearLayout>
Here's the Landscape XML layout I am using:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true">
<!-- Other controls are here -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#drawable/gray_background" >
<ImageView android:id="#+id/imgView"
android:layout_gravity="center"
android:layout_margin="5dip"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:contentDescription="#string/ImageToSend"
android:adjustViewBounds="true"
android:layout_weight="1"
android:scaleType="fitCenter" />
<!-- Other controls are here -->
</LinearLayout>
</ScrollView>
This works perfectly fine no matter what view I am in. The problem I am having is that when the user rotates the device, the ImageView no longer has the correct size. If I start by selecting the image in landscape mode, rotations look fine. If I start by selecting an image in portrait mode, then switch to landscape mode, the image now displays as 1px by 1px.
Any ideas anyone?
After a while of failed expiraments, I found that calling setContentView() and redoing the layout fixed the issue. Not sure if this is what I was supposed to do, but it has fixed it.
I have an image that I do not want to scale, I am using fitStart. What size do I need to make it to ensure that it covers a 4x2 widget? I currently have an image that is 1024x768 and it doesn't cover.
UPDATE:
Here is the xml and screen shot:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/widget_bg_rl"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/widget_bg"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitStart" />
<LinearLayout
android:id="#+id/current_layout"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusable="true"
android:weightSum="8">
<RelativeLayout
android:id="#+id/city_time_rl"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="2.5"
android:background="#drawable/rounded_corners_top">
<TextView
android:id="#+id/city"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="MECHANICSBURG"
android:textSize="16dp"
android:textColor="#color/dk_blue"
/>
You're using fitStart, which maintains the image aspect ratio, and ensures that one dimension (height, in this case) fits exactly. It sounds like you want one of these two:
If you want to fill the space, showing the entire image (and stretching it as necessary), use fitXY.
If you want to fill the space, maintaining the image's aspect ratio (and cropping it as necessary), use centerCrop.
I am trying to place two images in landscape mode since my app will be in landscape mode through out. When I am doing this following code android works proper in potrait mode but in landscape mode it doest stretch with the ratio but they remains unchanged. WHat should i do it to make it handle by android
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<FrameLayout
android:id="#+id/frameLayout1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:layout_gravity="center_horizontal" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="#drawable/hud" />
</FrameLayout>
<FrameLayout
android:id="#+id/frameLayout2"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:layout_gravity="center_horizontal">
<ImageView
android:id="#+id/imageView2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center_horizontal"
android:src="#drawable/bg" />
</FrameLayout>
</LinearLayout>
If you want your app to always be in landscape mode, set it in the Manifest file like this:
<activity android:screenOrientation="landscape">
Then, instead of using XML to make your layout, do it from the activity. Then you can use this code to get the display size info:
/* First, get the Display from the WindowManager */
Display display = ((WindowManager) getSystemService(WINDOW_SERVICE)).getDefaultDisplay();
/* Now we can retrieve all display-related infos */
int width = display.getWidth();
Then you can use this display width to set the width of your images (i.e. width/2).
You probably need android:scaleType="fitCenter" on your ImageViews.