简体   繁体   English

调整图像大小以填充屏幕宽度

[英]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 . 我正在使用一个图书馆来帮助我完成这一工作,称为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" 编辑:这是与android:scaleType="centerCrop"

在此处输入图片说明

ImageView has scale type feature. ImageView具有缩放类型功能。 Check ImageView.ScaleType . 检查ImageView.ScaleType I think centerInside or fitCenter is what you are looking for. 我认为centerInsidefitCenter是您想要的。

Update 更新资料

Sorry didn't realize you have a fixed height at first. 抱歉,一开始没有意识到您的身高是固定的。 centerCrop should be the answer. centerCrop应该是答案。

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. 1920px是图像集的最大宽度。 The result is acceptable, EDIT: Image is perfectly centered. 结果是可以接受的,编辑:图像完全居中。

I achieved this using "paddingLeft" and "paddingRight" on parent layout; 我在父级布局上使用“ paddingLeft”和“ paddingRight”实现了这一点; while apply a negative value to padding and margins to the child layout / ImageView. 同时为子版面/ ImageView的填充和边距应用负值。 "android:clipToPadding" flag must be false to prevent the clipping. “ android:clipToPadding”标志必须为false,以防止剪切。

<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"
        ...>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM