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代码
ImageView image= (ImageView)findViewById(R.id.start);
LevelListDrawable background=(LevelListDrawable)image.getBackground();
background.setLevel(3);
但它无法改变背景,我已经尝试将android:background更改为android:src,它也没有工作。
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
如何解决它,谢谢。
答案 0 :(得分:3)
使用:
image.setImageLevel(3);
而不是:
LevelListDrawable background =(LevelListDrawable)image.getBackground(); background.setLevel(3);
答案 1 :(得分:0)
这里的问题是再次设置Darawable并调用refreshDrawableState();
这是一个完整的示例:
battery_level.xml (在drawable文件夹中创建):
<?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>
您将从矢量资产中找到所有可绘制电池。我在这里使用了七个不同的状态。
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)
}
}
此处捕获的内容是使用:
imgBatteyLevel.setImageDrawable(ContextCompat.getDrawable(this, R.drawable.battery_level));
imgBatteyLevel.refreshDrawableState();
在方法 onClickBatteryChangedIncrement(视图:视图)中
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>
答案 2 :(得分:0)
您实际上必须使用app:srcCompat="@drawable/start_background"
。
之后,您可以使用:
ImageView image= (ImageView)findViewById(R.id.start);
image.setImageLevel(3);