在活动中的全屏背景图像

Ser*_*o76 140 android background imageview android-framelayout

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

全屏背景图像

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

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

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

你会怎么做?

sti*_*ike 214

有几种方法可以做到.

选项1:

为不同的dpi创建不同的完美图像,并将它们放在相关的可绘制文件夹中.然后设置

android:background="@drawable/your_image

选项2:

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

android:src="@drawable/your_image"
android:scaleType = "centerCrop"
Run Code Online (Sandbox Code Playgroud)

  • 最后一个选项 - >内存不足:D (7认同)
  • 在res-> drawable文件夹中.可能有多个 - 每个代表不同的分辨率(例如低分辨率/高分辨率).如果您没有针对每种分辨率的特定图像,则任何一种都可以使用. (4认同)
  • 答案很好.但我想知道是否有人为每个dpi组找到了典型的手机尺寸,这在准备图像时非常有用.编辑:发现 - http://stackoverflow.com/questions/10574363/android-splash-screen-image-sizes-to-fit-all-devices/15744389#15744389 (4认同)
  • 你在哪里存储'your_image'? (2认同)

axp*_*usb 25

另一个选择是在drawables中添加一个单独的图像(不一定很大)(让我们将其命名为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);
Run Code Online (Sandbox Code Playgroud)

没有裁剪,没有很多不同大小的图像.希望能帮助到你!

  • 这是一个有用的解决方案,但请记住从主线程做位图处理:http://developer.android.com/training/displaying-bitmaps/process-bitmap.html (3认同)

Mun*_*oor 18

您应该将各种尺寸的图像放入以下文件夹中

有关详细信息,请访问此链接

  • LDPI

  • MDPI

  • 华电国际

  • 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>
Run Code Online (Sandbox Code Playgroud)


NoT*_*ast 7

关于什么

android:background="@drawable/your_image"

在你的活动的主要布局?

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


小智 6

用这个

android:background="@drawable/your_image
Run Code Online (Sandbox Code Playgroud)

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


小智 6

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

<item name="android:windowActionBarOverlay">true</item>
Run Code Online (Sandbox Code Playgroud)

请享用!


smu*_*rph 5

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

您可以使用嵌套布局。从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>
Run Code Online (Sandbox Code Playgroud)