具有级别列表的 android ImageView 不起作用

标签 android drawable

start_background.xml

<?xml version="1.0" encoding="utf-8"?>
<level-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
    android:drawable="@color/grey"
    android:maxLevel="1"
    />
<item
    android:drawable="@color/grey"
    android:maxLevel="2"
    />
<item
    android:drawable="@color/grey"
    android:maxLevel="3"
    />

</level-list>

开始 Activity .xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"

android:id="@+id/page"
>
<ImageView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/start_background"
    android:id="@+id/start"
    />
</LinearLayout>

Java代码

ImageView image= (ImageView)findViewById(R.id.start);
LevelListDrawable background=(LevelListDrawable)image.getBackground();
background.setLevel(3);

但是它不能改变背景,我试过把android:background改成android:src,也没用。

日志:

2690-2705/com.jifa.runandcatch2 W/EGL_emulation﹕     eglSurfaceAttrib not implemented
2690-2705/com.jifa.runandcatch2 W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xae0e1de0, error=EGL_SUCCESS

如何解决,谢谢。

最佳答案

使用:

image.setImageLevel(3);

而不是:

LevelListDrawable 背景=(LevelListDrawable)image.getBackground(); background.setLevel(3);

关于具有级别列表的 android ImageView 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28868531/

相关文章:

android - eclipse android 通用 xml 编辑器停止正确(随机)呈现文本

android - 如何通过 Android 应用程序在电子邮件正文中发送图像

android - 如何在 android jetpack compose 的非全屏横向模式下使用背景颜色为切口/缺口区域着色

android - 创建一个包含另一个可绘制对象和边距的可绘制对象

java - 设置 SurfaceView 的背景图像

android - 如何使用 firebase-server 在 android 下模拟 firebase 云消息传递

Android 可绘制图像 int

android - 如何在文本中设置背景图像

android - Eclipse Export Signed Package 两次添加 drawables 目录,使 apk 大小增加一倍!

android - 在 Android 中为 View 设置全局样式