2014-09-26 57 views
0

我有两个图像文件:填充ImageView的筛选,同时保持纵横比

  • 320dpi 768x1280
  • 480dpi 1080×1920

我把每帧图像的xhdpi和xxhdpi可绘制的文件夹我的项目。我试图让这两个图像适当缩放,并填充屏幕没有任何空格。

我的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="com.example.vax.gamurs.LandingPage" 
    android:background="#ffffffff" 
    android:orientation="vertical" 
> 


<ImageView 
    android:id="@+id/LandingBack" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:src="@drawable/background" 
    android:adjustViewBounds="true" 
    android:layout_gravity="center" 
    android:layout_centerVertical="true" 
    android:layout_centerHorizontal="true" /> 

我越来越空白双方的的Nexus 4和Nexus 5的侧面怎样消除空白,并具有图像填充屏幕,同时保持长宽比?

Nexus4

+1

由于图像的宽高比与屏幕的宽高比不同,所以不能。您的选择是在边框上留有空白或裁剪图像。 – GreyBeardedGeek 2014-09-26 02:47:04

+0

@GreyBeardedGeek我应该使用什么样的比例来确保屏幕被填满? – 2014-09-26 02:54:19

+0

取决于设备。没有一个通用长宽比。 – GreyBeardedGeek 2014-09-26 10:04:37

回答

1

使用scaleType="centerCrop"如下:

<ImageView 
     android:id="@+id/LandingBack" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:src="@drawable/background" 
     android:scaleType="centerCrop" 
     android:adjustViewBounds="true" 
     android:layout_gravity="center" 
     android:layout_centerVertical="true" 
     android:layout_centerHorizontal="true" /> 

上述方法将裁剪图像。不幸的是,如果您使用固定大小的绘图并且想要支持不同的屏幕尺寸,这是一个缺点。

另一种方法是使用下面附加的9补丁图像。这将拉伸蓝色背景以填充空白空间。

下载链接9Patch图像:https://dl.dropboxusercontent.com/u/2411239/sampleimg.9.png

下面是一个简单的截图,显示了它是如何工作的:

enter image description here

更新:

您的新ImageView应尽可能如下:

<ImageView 
    android:id="@+id/LandingBack" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:background="@drawable/sampleimg" 
    android:adjustViewBounds="true" 
    android:layout_gravity="center" 
    android:layout_centerVertical="true" 
    android:layout_centerHorizontal="true"/> 

同样为了参考,我使用1px的拉伸贴剂如下所示的图像中:

enter image description here

+0

centreCrop让我因裁剪而失去图像的一部分。 – 2014-09-26 06:57:08

+0

@OsborneCox不幸的是,如果您有一个固定大小的图像(尤其是所有具有不同尺寸的设备),这是缺点。您可以使用可绘制的九个补丁,这将自动缩放蓝色背景以填充白色区域。让我知道你是否需要关于这种方法的更多信息。 – 2014-09-26 07:03:58

+0

在这种情况下,我怎样才能适当地使用九个补丁?缩放蓝色背景将是完美的,因为它会保留文本和按钮的大小/比例。 – 2014-09-26 07:13:26

-1

只要使用的ImageView

 android:scaleType="fitXY" 

的属性,这意味着

<ImageView 
    android:scaleType="fitXY" 
    android:id="@+id/LandingBack" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:src="@drawable/background" 
    android:adjustViewBounds="true" 
    android:layout_gravity="center" 
    android:layout_centerVertical="true" 
    android:layout_centerHorizontal="true" /> 

在Imageview中,你完成了。快乐编码

+0

这会导致图像被拉伸。 – 2014-09-26 06:56:36

0

在保持宽高比时无法填充,因为没有意义。你可以做的是将背景设置为与蓝色相同。(非常简单)或者使用简单的数学方法填充图像的高度或仅填充宽度

w = W; h =(w/W)H;

其中W是屏幕的宽度,H是屏幕的高度,w是图像和h的宽度图像

的高度

交汇处宽度与高度以填充整个高度,而不是宽度

0

尝试像这样设置imageview的背景:

android:background="@drawable/background" 

而不是“src”。如果您使用src,您的图片将不会缩放,但背景将会缩放。

但是这种缩放可能会破坏你的图像,你应该使用九个补丁。

相关问题