【问题标题】:Android background image not showingAndroid背景图片不显示
【发布时间】:2015-05-10 13:42:50
【问题描述】:

我目前正在开发一个 Android 项目,该项目需要我将图像作为某些活动的背景。我正在使用 Android Studio 开发该应用,并已在多台设备上测试了该应用。

起初我把背景图片放在drawable文件夹中。由于重新缩放图像,这导致帧率下降。在此之后,我将图像放在 drawable-nodpi 文件夹中。 我在摩托罗拉 Moto G (Android L)、Nexus 5 (Android L) 和 Nexus 9 (Android L) 上测试了该应用,一切正常。

为了确保该应用适用于多种设备,我通过 Android Studio 启动了一个 mdpi Jellybean 模拟器。令人惊讶的是,背景只加载到 2/4 活动中(都使用相同的资源)。经过一些研究,我决定预先缩放背景并将其放置在 drawable-mdpi、drawable-ldpi 文件夹等中。再次测试,现在背景只是没有出现在 1 个活动中。我决定在多台设备上使用较旧的 api 进行测试。我发现我的应用在每台 API 低于 Android L 的设备上都有这个问题。

有人可以向我解释一下这是怎么可能的吗,因为所有活动都使用相同的资源?

亲切的问候,

朱尔

学生 ICT 软件开发

编辑 我在 Drawable 文件夹中添加了背景。

加载后台的activity源码

<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="nl.beachbash.beachbash.InfoListActivity"
android:background="@drawable/background"
android:backgroundTint="#32dd6800"
android:backgroundTintMode="src_over"
android:id="@+id/background">

<ListView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/listViewInfo"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:drawSelectorOnTop="false"
    android:dividerHeight="2dp"
    android:divider="#FFFFFF" /></RelativeLayout>

不加载后台的activity源代码

<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="nl.beachbash.beachbash.FloorPlanActivity"
android:backgroundTint="#32dd6800"
android:backgroundTintMode="src_over"
android:background="@drawable/background">

<nl.beachbash.customUI.TouchImageView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/imageView"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:nestedScrollingEnabled="false"
    android:contentDescription="@string/Floorplan" /></RelativeLayout>

【问题讨论】:

  • 您能否发布到目前为止的实现...您是否尝试将这些资源文件放在drawable 文件夹中?
  • 无论您以哪种方式挥动它,从计算和内存的角度来看,使用光栅化图像作为活动背景都会很昂贵。也就是说,它不应该在活动中“不稳定”。你是如何设置背景的?您是在使用主题属性android:windowBackground 还是将其设置为每个 Activity 中根视图的背景?
  • 我已经添加了布局的源代码

标签: android background


【解决方案1】:

我自己解决了这个问题。

我尝试创建一个新活动,输入完全相同的代码,构建完全相同的界面并以与上述相同的方式添加背景,但这次它显然有效。 不知道为什么...

【讨论】:

  • 一个常见的原因是内存。即使它正在工作,我强烈建议缩小图像大小,然后在图像上使用压缩算法来减小磁盘大小。干杯!
猜你喜欢
  • 1970-01-01
  • 2020-09-11
  • 1970-01-01
相关资源
最近更新 更多