2013-04-21 173 views
122

我看到许多使用全屏图像作为背景的应用程序。 这是一个例子:活动中的全屏背景图像

Full screen background image

我想在一个项目中,我发现,到目前为止,做到这一点是使用图像具有大尺寸,把它放在一个最好的方式来使用这个ImageView并使用android: adjustViewBounds="true"调整边距

问题是,如果屏幕分辨率非常高,则图像不足。

我想到了另一种选择是在widthheight使用图像中的FrameLayout,与match_parent作为背景......这绵延的形象,但我觉得结果不是很好。

你会怎么做?

+2

不要相信这适用于背景,但它应该适用于图像。 'android:scaleType =“centerCrop”' – EGHDK 2013-04-21 20:59:42

+0

详细的博客:http://sforsuresh.in/set-background-image-android-xml/ – 2017-07-20 08:51:53

回答

184

有几种方法可以做到这一点。

选项1:

不同的DPI创建不同的完美的图像,并将它们在相关绘制文件夹。然后设置

android:background="@drawable/your_image

选项2:

添加一个大的图像。使用FrameLayout。作为第一个孩子添加一个ImageView。在ImageView中设置以下内容。

android:src="@drawable/your_image" 
android:scaleType = "centerCrop" 
+1

你在哪里存储'your_image'? – 2014-05-18 13:20:18

+4

在res-> drawable文件夹中。可能有多个 - 每个都代表不同的分辨率(例如低分辨率/高分辨率)。如果您没有每个分辨率的特定图像,任何人都可以使用。 – krodmannix 2014-05-27 23:52:28

+3

答案很好。但是,我想知道是否有人发现每个dpi组的典型手机尺寸,这在准备图像时非常有用。编辑:发现 - > http://stackoverflow.com/questions/10574363/android-splash-screen-image-sizes-to-fit-all-devices/15744389#15744389 – fox 2014-06-19 13:15:46

7

什么

android:background="@drawable/your_image"

您的活动主要布局

这样,通过将不同的屏幕密度放置在适当的res/drawable-**dpi文件夹中,您也可以获得不同的图像。

1

与NoToast的答案线,你需要在你的RES /绘制-LDPI,MDPI,华电国际,X-华电国际(用于Xtra的大屏幕) “your_image” 的多个版本,删除match_parent并保持机器人:adjustViewBounds =“真”

13

你应该把各种尺寸的图像到以下文件夹

的更多详情,请这个link

  • LDPI

  • MDPI

  • HDPI

  • xhdpi

  • xxhdpi

和使用的RelativeLayout或LinearLayout中背景代替使用的ImageView作为follwoing例子

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 
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/your_image"> 

</RelativeLayout> 
+0

我收到一个错误,指出'标签RelativeLayout发现'意外命名空间前缀'xmlns' – CodyBugstein 2014-06-18 11:59:17

+0

这是在mitmap中吗? – 2015-11-11 18:43:47

+0

@丰富的可绘制 – 2015-11-12 02:58:11

23

另一种选择是在可绘制(我们将其命名为backgroung.jpg)在你的XML的根添加一个图像(不一定大),创建一个ImageView的iv_background没有“src”属性。 然后,在相应的活动的onCreate方法:

/* create a full screen window */ 
    requestWindowFeature(Window.FEATURE_NO_TITLE); 
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
      WindowManager.LayoutParams.FLAG_FULLSCREEN); 
    setContentView(R.layout.your_activity); 

    /* adapt the image to the size of the display */ 
    Display display = getWindowManager().getDefaultDisplay(); 
    Point size = new Point(); 
    display.getSize(size); 
    Bitmap bmp = Bitmap.createScaledBitmap(BitmapFactory.decodeResource(
     getResources(),R.drawable.background),size.x,size.y,true); 

    /* fill the background ImageView with the resized image */ 
    ImageView iv_background = (ImageView) findViewById(R.id.iv_background); 
    iv_background.setImageBitmap(bmp); 

不裁剪,没有许多不同大小的图像。 希望它有帮助!

+2

这是一个有用的解决方案,但记住要做的位图处理关闭主线程:http:// developer.android.com/training/displaying-bitmaps/process-bitmap.html – 2014-09-22 21:47:35

5

使用本

android:background="@drawable/your_image 
您的活动非常第一线或相对布局

0

如果你只是bg.png作为背景图片,然后:

<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" 
    android:background="@drawable/bg" 
    tools:context=".MainActivity" > 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerHorizontal="true" 
     android:layout_centerVertical="true" 
     android:text="@string/hello_world"/> 
</RelativeLayout> 
3

如果你希望你的形象示后面的透明行动吧,把下面的内容主题的样式定义:

<item name="windowActionBarOverlay">true</item> 
<item name="android:windowActionBarOverlay">true</item> 

享受!

+0

这就是我一直在寻找 – user2759156 2017-09-28 10:20:13

-1

工作。你应该试训这样的:

android:src="@drawable/img" 
1

添加android:background="@drawable/your_image"的工作你的RelativeLayout/LinearLayout中 内。

2

自发布之后已经有一段时间了,但这对我有帮助。

您可以使用嵌套布局。从一个RelativeLayout开始,并把你的ImageView。

将高度和宽度设置为match_parent以填充屏幕。

设置scaleType =“centreCrop”,使图像适合屏幕,不拉伸。

然后,您可以像平常一样放置任何其他布局,如下面的LinearLayout。

您可以使用android:alpha来设置图像的透明度。

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

    <ImageView 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:scaleType="centerCrop" 
    android:src="@drawable/image" 
    android:alpha="0.6"/> 

    <LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    tools:context=".MainActivity"> 

     <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Hello"/> 

     <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="There"/> 

    </LinearLayout> 
</RelativeLayout>