Android Layout, scale width to screen resolution - android

I want to resize an image to fit to screen that it fills the complete width. The hight should be set in aspect ratio.
For example the image dimentions are widthxheight 10x5 and the phone is 400 width. the image should be displayed in 200x400.
I played around with some settings
android:scaleType="center" should be the correct setting according to documentation but seems to have no effect,
android:scaleType="centerCrop" fits the hight and makes the width bigger than the screen.
heres my layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical" >
<ImageView
android:id="#+id/imageViewFrage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:adjustViewBounds="true"
android:padding="1dp"
android:scaleType="centerCrop"
android:src="#drawable/bg_striped_img" />
</LinearLayout>
</LinearLayout>
thanks alot

You should try android:scaleType="centerInside".
According to the documentation (http://developer.android.com/reference/android/widget/ImageView.ScaleType.html):
Scale the image uniformly (maintain the image's aspect ratio) so that both dimensions (width and height) of the image will be equal to or less than the corresponding dimension of the view (minus padding). The image is then centered in the view.

ok wont work. that sucks. found this soli
http://argillander.wordpress.com/2011/11/24/scale-image-into-imageview-then-resize-imageview-to-match-the-image/

Related

How to make an ImageView the same height as the LinearLayout, but keep original aspect ratio which makes the width wider than the LinearLayout

I'm in a horizontal LinearLayout (landscape app). I have an image that is 5000x1080px for a scrolling home screen background. I need the image to fill the height of the LinearLayout while maintaining the image's original ratio. This will obviously cause the width to overlap with the outside edges of the Layout, and with that I will be able to animate it with a Translation to achieve a "scrolling" effect.
I've tried a bunch of different combinations of scaleType and adjustViewBounds but I can't seem to get the desired effect. The ImageView seems to constantly fill both the height and the width while maintaining the image's ratio, but not making the height match the parent as desired.
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:theme="#style/AppTheme.NoActionBar"
android:background="#color/black"
tools:context=".MainActivity">
<ImageView
android:id="#+id/homeBackground"
android:src="#drawable/background_home"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:alpha="0.7"/>
Found the answer: constraintLayout on the dimensions with a ratio equal to the image's dimensions:
<ImageView
android:id="#+id/homeBackground"
android:layout_width="0dp"
android:layout_height="0dp"
android:adjustViewBounds="true"
android:alpha="0.7"
android:src="#drawable/background_home"
app:layout_constraintDimensionRatio="4.629629:1"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
Since the image's dimensions are 5000x1080, that comes out to a 125:27 ratio, then simplified to 4.629629:1 or (125/27:1)

Elements inside scrollview shows different size for different screens. How to fix it?

im my code
<ScrollView
android:id="#+id/scroll"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight=".3">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:id="#+id/json"
android:onClick="album"
android:scaleType="fitXY"
android:layout_width="fill_parent"
android:layout_height="140dp" />
<ImageView
android:id="#+id/json1"
android:layout_marginTop="3dp"
android:onClick="live"
android:scaleType="fitXY"
android:layout_width="fill_parent"
android:layout_height="140dp" />
</LinearLayout>
</ScrollView>
how can i set height of the imageviews independent for all size screens...
now it looks fine in mobile phones but on tabs, the imageviews insdie scrollview get stretched.
Those are stretched because of scaleType,
Set it to centerInside and it will work fine.
Three XML attributes are defining how your image appears on the screen.
Height
Width
Scale Type
You've set the height to 140dp, but set the width to be fill_parent and the scale type to fitXY.
Set the Image width to be 140dp or some other predefined value based on the Image Aspect Ratio or change the scale type.

Resizing image to fill width of screen

I am trying to set an Image that I am downloading from the web. I am using a library to help me do this called ion. The problem is that I need to resize the image to fit my screen and keep the aspect ration. The height seems to be working fine however I want to set the width of the bitmap to fit the whole width of the devicewhile keeping the aspect ratio. But it is not working and the image is cut short on both sides...
<ImageView
android:id="#+id/imageView"
android:layout_width="fill_parent"
android:layout_height="#dimen/_height" />
Code:
DisplayMetrics metrics = getResources().getDisplayMetrics();
mWidth = metrics.widthPixels;
mHieght = (int) getResources().getDimension(R.dimen._height);
Ion.with(imageView).resize(mWidth, mHieght).load(url)
Edit:
This is with android:scaleType="centerCrop"
ImageView has scale type feature. Check ImageView.ScaleType. I think centerInside or fitCenter is what you are looking for.
Update
Sorry didn't realize you have a fixed height at first. centerCrop should be the answer.
If you have a known max size for your images, you can set the width of your layout equal to the largest width of your images set, then take all centered. This worked for me:
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<RelativeLayout
android:layout_width="1920px"
android:layout_height="fill_parent"
android:layout_gravity="center">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView"
android:background="#drawable/your_image" />
</RelativeLayout>
</FrameLayout>
Where 1920px is the largest width of your images set.
The result is acceptable, EDIT: Image is perfectly centered.
I achieved this using "paddingLeft" and "paddingRight" on parent layout; while apply a negative value to padding and margins to the child layout / ImageView. "android:clipToPadding" flag must be false to prevent the clipping.
<LinearLayout
...
android:paddingLeft="10sp"
android:paddingRight="10sp"
android:clipToPadding="false"
...>
<ImageView
android:layout_marginLeft="-10sp"
android:layout_marginRight="-10sp"
android:paddingRight="-10sp"
android:paddingLeft="-10sp"
...>

How can I show a photo in an imageview in its original aspect ratio?

I would like to show a photo from my phone in an ImageView. This photo is supposed to be shown in it's original aspect ratio. So it should fill up the whole width, but only use as much height as it needs due to it's ratio.
In short: I want to show a photo in an imageview without scaling.
How can I do that? Right now I am using this layout. The problem is that it always scales the photo to fill up the whole screen.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/root"
android:orientation="vertical"
android:background="#android:color/black">
<ImageView
android:id="#+id/image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:adjustViewBounds="true"
android:scaleType="center" />
</LinearLayout>
You can use ScaleType CENTER_INSIDE for this purpose.
For more detail go through link.
Android ImageView ScaleType
Try put image in drawable-nodpi folder and set wrap_parent for width and height in ImageView and set link to image in src
Try to set width to match parent, height to wrap content and to adjust view bounds:
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:src="#drawable/drawable"/>

Android ImageView Landscape - Portrait

I have a simple application with ImageView
I need image to take all width and scroll y if it doesnt fit.
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:adjustViewBounds="true"
android:scaleType="center"
android:id="#+id/myPic"
android:contentDescription="#string/page_label" />
</RelativeLayout>
Everything is perfect, but when i switch to landscape image wont take all width, there are blank spaces (left, right).
only ImageView.ScaleType CENTER_CROP helps, but it cuts piecies from top and bottom.
Is it possible make it work the same way in landscape?
scaletype fitCenter for the ImageView does the trick. It scales the picture to full width, and allows height to be scrolled while still maintaining aspect ratio.

Categories

Resources