简体   繁体   中英

android ImageView with level-list do not work

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>

start_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 Code

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

but it can not change the background, I have try to change android:background to android:src, it didn't work also.

logcat:

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

how to solve it, thanks.

Use:

image.setImageLevel(3);

instead than:

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

The catch here is setting the Darawable again and calling refreshDrawableState();

Here is a complete example:

battery_level.xml (Created in the drawable folder):

<?xml version="1.0" encoding="utf-8"?>
<level-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:drawable="@drawable/ic_battery_20"
        android:maxLevel="0"
        />
    <item
        android:drawable="@drawable/ic_battery_30"
        android:maxLevel="1"
        />
    <item
        android:drawable="@drawable/ic_battery_50"
        android:maxLevel="2"
        />
    <item
        android:drawable="@drawable/ic_battery_60"
        android:maxLevel="3"
        />
    <item
        android:drawable="@drawable/ic_battery_80"
        android:maxLevel="4"
        />
    <item
        android:drawable="@drawable/ic_battery_90"
        android:maxLevel="5"
        />
    <item
        android:drawable="@drawable/ic_battery_full"
        android:maxLevel="6"
        />
</level-list>

You will find all the battery drawables from vector asset.I have used seven different states here.

BatteryLevelActivity.kt:

class BatteryLevelActivity : AppCompatActivity() {

    var levelListDrawable = LevelListDrawable()
    private var batteryLevel = BatteryLevel.LEVEL1        // The active selection.

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_battery)
        Log.d("BatteryLevel" , batteryLevel.toString() )

    }


    // Fan speeds with index of translatable string for label names
    private enum class BatteryLevel(val label: Int) {
        /*LEVEL1(R.drawable.ic_battery_20),
        LEVEL2(R.drawable.ic_battery_30),
        LEVEL3(R.drawable.ic_battery_50),
        LEVEL4(R.drawable.ic_battery_60),
        LEVEL5(R.drawable.ic_battery_80),
        LEVEL6(R.drawable.ic_battery_90),
        LEVEL7(R.drawable.ic_battery_full);*/
        LEVEL1(0),
        LEVEL2(1),
        LEVEL3(2),
        LEVEL4(3),
        LEVEL5(4),
        LEVEL6(5),
        LEVEL7(6);

        fun next() = when (this) {
            LEVEL1 -> LEVEL2
            LEVEL2 -> LEVEL3
            LEVEL3 -> LEVEL4
            LEVEL4 -> LEVEL5
            LEVEL5 -> LEVEL6
            LEVEL6 -> LEVEL7
            LEVEL7 -> LEVEL7
        }

        fun previous() = when (this) {
            LEVEL7 -> LEVEL6
            LEVEL6 -> LEVEL5
            LEVEL5 -> LEVEL4
            LEVEL4 -> LEVEL3
            LEVEL3 -> LEVEL2
            LEVEL2 -> LEVEL1
            LEVEL1 -> LEVEL1
        }
    }

    fun onClickBatteryChangedIncrement(view: View){
        Log.d("BatteryLevel" , batteryLevel.label.toString() )
        batteryLevel = batteryLevel.next()
        Log.d("BatteryLevelChanged" , batteryLevel.label.toString() )
        imgBatteyLevel.setImageLevel(batteryLevel.label)
        imgBatteyLevel.setImageDrawable(ContextCompat.getDrawable(this, R.drawable.battery_level));
        imgBatteyLevel.refreshDrawableState();
    }

    fun onClickBatteryChangedDecrement(view: View){
        Log.d("BatteryLevel" , batteryLevel.label.toString() )
        batteryLevel = batteryLevel.previous()
        Log.d("BatteryLevelChanged" , batteryLevel.label.toString() )
        imgBatteyLevel.setImageLevel(batteryLevel.label)
    }
}

The catch here is using :

 imgBatteyLevel.setImageDrawable(ContextCompat.getDrawable(this, R.drawable.battery_level));
            imgBatteyLevel.refreshDrawableState();

in the method onClickBatteryChangedIncrement(view: View)

activity_battery.xml:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/imgBatteyLevel"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_marginTop="164dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.498"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:background="@drawable/battery_level" />

    <ImageButton
        android:id="@+id/imageButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="64dp"
        android:layout_marginTop="116dp"
        android:onClick="onClickBatteryChangedIncrement"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/imgBatteyLevel"
        app:srcCompat="@drawable/ic_plus" />

    <ImageButton
        android:id="@+id/imageButton2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="116dp"
        android:layout_marginEnd="80dp"
        android:onClick="onClickBatteryChangedDecrement"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/imgBatteyLevel"
        app:srcCompat="@drawable/ic_minus" />

</androidx.constraintlayout.widget.ConstraintLayout>

You actually have to use app:srcCompat="@drawable/start_background" .

Afterwards you can just use:

ImageView image= (ImageView)findViewById(R.id.start);
image.setImageLevel(3);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM