首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >活动中的全屏背景图像

活动中的全屏背景图像
EN

Stack Overflow用户
提问于 2013-04-22 04:40:37
回答 8查看 449.3K关注 0票数 155

我看到很多应用程序都使用全屏图像作为背景。这是一个示例:

我想在项目中使用它,到目前为止,我找到的最好的方法是使用一个大尺寸的图像,将它放在ImageView中,然后使用android: adjustViewBounds="true"来调整页边距

问题是,如果屏幕的分辨率非常高,图像就会变差。

我想到的另一个选择是在FrameLayout中使用图像,在width中使用match_parent,并使用height作为背景...这拉长了图像,但我认为结果不是很好。

你会怎么做?

EN

回答 8

Stack Overflow用户

发布于 2014-08-09 21:43:07

另一种选择是在可绘制文件(让我们将其命名为backgroung.jpg)中添加单个图像(不一定很大),在ImageView的根目录下创建一个不带"src“属性的iv_background。然后在相应活动的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);

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

票数 25
EN

Stack Overflow用户

发布于 2017-07-01 09:13:17

自从这篇文章发布以来已经有一段时间了,但这对我有帮助。

可以使用嵌套布局。从一个RelativeLayout开始,将您的ImageView放在其中。

将height和width设置为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>
票数 8
EN

Stack Overflow用户

发布于 2013-04-22 04:54:58

关于

android:background="@drawable/your_image"

在你活动的主布局上?

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

票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16135984

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档