【问题标题】:Full screen background image in an activity活动中的全屏背景图像
【发布时间】:2013-04-14 16:45:26
【问题描述】:

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

我想在项目中使用它,到目前为止我发现的最佳方法是使用大尺寸的图像,将其放入ImageView 并使用android: adjustViewBounds="true" 调整边距

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

我想到的另一个选择是在FrameLayout 中使用图像,在widthheight 中使用match_parent 作为背景...这会拉伸图像,但我认为结果不是很好.

你会怎么做?

【问题讨论】:

标签: android background imageview android-framelayout


【解决方案1】:

有几种方法可以做到。

选项 1:

为不同的dpi创建不同的完美图像并将它们放置在相关的drawable文件夹中。然后设置

android:background="@drawable/your_image"

选项 2:

添加单个大图像。使用框架布局。作为第一个孩子添加ImageView。在 ImageView 中设置以下内容。

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

【讨论】:

  • 你在哪里存储'your_image'?
  • 在 res->drawable 文件夹中。可能有多个 - 每个代表不同的分辨率(例如低分辨率/高分辨率)。如果您没有针对每种分辨率的特定图像,则任何一种都可以。
  • 答案很好。但我想知道是否有人找到了每个 dpi 组的典型手机尺寸,这在准备图像时会非常有用。已编辑:找到 - stackoverflow.com/questions/10574363/…
  • 最后一个选项 -> 内存不足:D
  • OOM 错误我们来了...如果它是一个大图像,您需要根据屏幕尺寸放大或缩小!在这种情况下,您很可能需要通过异步任务加载位图。
【解决方案2】:

另一种选择是在可绘制对象中添加单个图像(不一定很大)(让我们将其命名为 backgroung.jpg),在您的 xml 的根目录中创建一个 ImageView iv_background,而不使用“src”属性。 然后在对应activity的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);

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

【讨论】:

【解决方案3】:

你应该把各种尺寸的图片放到下面的文件夹中

更多详情请访问link

  • ldpi

  • mdpi

  • hdpi

  • xhdpi

  • xxhdpi

并使用RelativeLayout或LinearLayout背景而不是使用ImageView作为以下示例

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

【讨论】:

  • 我收到一个错误,Unexpected namespace prefix "xmlns" found for tag RelativeLayout
  • 这让我的 Fragment 变得如此滞后。不知道为什么。
  • 使用缓存或者用毕加索加载图片(square.github.io/picasso)希望能解决你的问题
【解决方案4】:

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

您可以使用嵌套布局。从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>

【讨论】:

    【解决方案5】:

    怎么样

    android:background="@drawable/your_image"

    在您的活动的主要布局上?

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

    【讨论】:

      【解决方案6】:

      如果您希望图像在透明操作栏后面显示,请将以下内容放入主题的样式定义中:

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

      享受吧!

      【讨论】:

        【解决方案7】:

        使用这个

        android:background="@drawable/your_image"
        

        在您的活动中第一个线性或相对布局。

        【讨论】:

          【解决方案8】:

          根据 NoToast 的回答,您需要在 res/drawable-ldpi、mdpi、hdpi、x-hdpi(用于超大屏幕)中有多个版本的“your_image”,删除 match_parent 并保持 android:adjustViewBounds="true"

          【讨论】:

            【解决方案9】:

            在您的相对布局/线性布局中添加android:background="@drawable/your_image" 工作。

            【讨论】:

              【解决方案10】:

              如果您有 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>
              

              【讨论】:

                【解决方案11】:

                工作。你应该试试这个:

                android:src="@drawable/img"
                

                【讨论】:

                  【解决方案12】:

                  three step for put background

                  1) 你应该选择你喜欢的图片。例如:enter image description here

                  2)然后你把这张图片复制到drawable中。 警告:您应该选择名称图片的缩写类型。

                  enter image description here

                  3)你去页面 xml Intented 并写:

                  android:background="id 图片" 例如我的图片ID是@drawable/download。

                  enter image description here

                  完成。

                  【讨论】:

                    【解决方案13】:

                    最简单的方法:

                    第 1 步:打开 AndroidManifest.xml 文件

                    You can see the file here!

                    第二步:找到android:theme="@style/AppTheme" &gt;

                    第 3 步:更改为 android:theme="@style/Theme.AppCompat.NoActionBar" &gt;

                    第 4 步:然后添加 ImageView 和图像

                    第 4 步:就是这样!

                    【讨论】:

                      猜你喜欢
                      • 1970-01-01
                      • 2016-12-31
                      • 2023-03-16
                      • 1970-01-01
                      • 1970-01-01
                      • 1970-01-01
                      • 1970-01-01
                      • 1970-01-01
                      相关资源
                      最近更新 更多