简体   繁体   English

如何在不同的Android设备上保持宽高比

[英]how to keep aspect ratio with different android devices

So I'm trying to put an image inside a scrollview and have the image stretch to fit different sized screens. 因此,我试图将图像放入滚动视图中,并拉伸图像以适合不同尺寸的屏幕。 It'll stretch horizontally, but vertically it always comes out messed up. 它会水平拉伸,但在垂直方向上总是弄乱了。

I've read so many pages of people with similar problems, but their solutions never work for me. 我已经阅读了很多页面,遇到类似问题的人,但是他们的解决方案对我没有用。 I've tried every different combination possible using different scaleType , layout_width and layout_height , along with trying changing other things around. 我尝试使用不同的scaleTypelayout_widthlayout_height尝试所有可能的组合,并尝试更改其他内容。 The closest I've gotten to getting it to work is shown in the code below, using scaleType="centerCrop" . 在下面的代码中,使用scaleType="centerCrop"显示了与它最接近的功能。 It's the only one that stretches the image vertically in ratio, but it cuts off a big chunk of the image from the top and bottom, it'll scroll up and down the image, but only the middle part shows. 这是唯一一个按比例垂直拉伸图像的方法,但是它从顶部和底部切下了很大一部分图像,它将在图像中上下滚动,但只显示中间部分。

The image is a 480x5500 jpeg, if that matters. 如果重要的话,图片为480x5500 jpeg。 Originally before I started messing with all that, the app worked just fine on my phone, but then later I realized the image was crunched when I tried it on a tablet. 最初,在我开始弄乱所有这些内容之前,该应用程序在手机上的运行情况就很好,但是后来我意识到,在平板电脑上尝试使用该图像时,图像很混乱。 There's gotta be a way to do this in the xml right? 在xml中一定有办法做到这一点吧? I really don't want to have to do things with the image in the java code part. 我真的不想在Java代码部分中处理图像。 Also I'm hoping to be able to do this using just one image, I don't want to have use a different image for different screen sizes. 我也希望能够仅使用一张图像来完成此操作,但我不想针对不同的屏幕尺寸使用不同的图像。 Please help, thanks. 请帮忙,谢谢。

<?xml version="1.0" encoding="utf-8"?>

<ScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/scrollView1"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true"
        android:scaleType="centerCrop"
        android:src="@drawable/paper" />
</ScrollView>    

may be this help you, make both height and width wrap_content of ImageView 可能对您有帮助,同时使ImageView高度和宽度wrap_content

  android:layout_width="wrap_content"
  android:layout_height="wrap_content"

and no need ScrollView here. 并不需要ScrollView在这里。

Try using custom ImageView like AspectRatioImageView mentioned here: 尝试使用此处提到的自定义ImageView(如AspectRatioImageView):

https://stackoverflow.com/a/4688335/944070 https://stackoverflow.com/a/4688335/944070

the next sample is more than what you ask for : 下一个示例超出您的要求:

hotoview.sample hotoview.sample

it fits the image to the screen , plus it allows to zoom in/out using pinching gestures. 它使图像适合屏幕,并且允许使用捏合手势放大/缩小。

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

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