安卓设备无法显示背景图片

6
我创建了一个项目,目标为android 4.4 - API Level 19,如Eclipse建议。
无论如何,在模拟器上一切看起来都完美无缺 - 这是一个截图:

app screenshot

然而,当我在我的真实设备上安装它 - 运行android 4.1.2时,问题就来了。除了背景图片之外,一切都像魔法般地完美运作。它显示出一个白色的背景,就好像图片不存在一样。
这是我的xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" 
    android:orientation="vertical"
    android:gravity="center"
    android:padding="30dp"
    android:background="@drawable/wg_blurred_backgrounds_12"
>

<Button
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/btnTopJokes" 
    android:onClick="showTopJokes"   
    android:gravity="center" 
/>

<Button
    android:layout_marginTop="10dp"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/btnNewJokes" 
    android:onClick="showNewJokes" 
    android:gravity="center"
/>

<Button
    android:layout_marginTop="10dp"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/btnAllJokes" 
    android:onClick="showAllJokes" 
    android:gravity="center"
/>

<Button
    android:layout_marginTop="10dp"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/btnMyFavorites" 
    android:onClick="showFavorites" 
    android:gravity="center"
/>

<Button
    android:layout_marginTop="10dp"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/btnCategories" 
    android:onClick="showCategories"
    android:gravity="center"
/>

<Button
    android:layout_marginTop="10dp"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/btnAddNewJoke" 
    android:gravity="center"
/>

</LinearLayout>

在安卓4.1.2上,android:background="@drawable/wg_blurred_backgrounds_12无法正常工作吗?

我需要更改权限吗?这是我现在拥有的:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

我知道我可能错过了一些微小的东西,但作为初学者,我无法发现它。


2
你是否将drawable添加到相应的drawable文件夹中了?请查看http://developer.android.com/guide/practices/screens_support.html。 - Raghunandan
@Raghunandan 是的,一切都很好。 - chility
9个回答

33

尝试使用分辨率更低的背景图。你正在尝试使用的背景可能太大了。你可以在logcat中检查。


我在模拟器上没有问题。我可以在那里看到背景。但是在我的安卓4.1.2的Nexus S上没有显示出来。 - chility
3
你是否尝试过创建drawable文件夹,并将低分辨率的背景图放在里面?我曾遇到过这个问题,但我不记得具体怎么解决了。不过大概是这样做的。 - shemsu
一些棒棒糖设备无法正常显示高分辨率图片,这个修复方法可以解决这个问题。如果您是通过随机生成背景或使用一组图像加载它们到背景中,则也可以通过编程手动调整位图大小。 - Akah

15

在您的日志(logcat)中,您会看到这样的一条消息:

Bitmap太大,无法上传到纹理(2496x4437,最大值=4096x4096)

最大值为4096x4096,这是正在设备上运行的OpenGL版本的GL_MAX_TEXTURE_SIZE的值。


6

你可能需要将以下内容添加到你的清单文件中。 android:hardwareAccelerated="false",android:largeHeap="true"

 <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/logo"
        android:supportsRtl="true"
        android:hardwareAccelerated="false"
        android:largeHeap="true"
        android:theme="@style/AppTheme">

0

0

确保在您的drawable文件中,图像文件旁边不要显示(24)(v 24),重新复制图像并将其保存为没有(24)的类型,以便为API 24及以上版本做好准备,我的手机使用的是这个版本,这就是为什么它在我使用API 23时失败的原因。


0

尝试删除给可绘制对象命名的下划线。


0

将图像保存在每个文件夹中,例如xhdpi、xxhdpi、xxxhdpi,以其分辨率为基准。


0

您也可以尝试禁用硬件加速来设置背景视图

myView.setLayerType(View.LAYER_TYPE_SOFTWARE, null)

然后再设置背景


0

减小图像的高度和宽度。这将解决问题。您还可以在logcat中看到生成的图像过大错误。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接