更改ImageView的drawable属性

问题描述 投票:0回答:1

我正在尝试在我的应用程序中创建一个按钮来更改它的壁纸(不是手机上的壁纸,而是应用程序的壁纸)。但我一生都无法弄清楚如何做到这一点,也找不到任何相关信息。

lateinit var backgroundimg : ImageView
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
    backgroundimg = findViewById<ImageView>(R.id.background)

}
fun change_wallpaper() {
    var wallpapers = arrayOf(wallpapers here)
    var choice = (0..wallpapers.size).random()
    var chosenwallpaper = wallpapers[choice]

    // DONT KNOW WHAT TO DO HERE
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    tools:context=".MainActivity">

    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:scaleType="centerCrop"
        android:src="@drawable/[CHANGE THIS PROPERTY]"
        android:id="@+id/background">
    </ImageView>

    <ImageButton
        android:layout_width="10mm"
        android:layout_height="10mm"
        android:layout_marginTop="100mm"
        android:layout_marginLeft="51mm"
        android:background="@drawable/xmlasset_reloadbutton"
        android:id="@+id/reload_wallpaper"/>

</RelativeLayout>

我大多只是在谷歌上搜索,看看是否能找到任何东西,但正如我所说,我找不到。

xml kotlin android-studio
1个回答
0
投票
lateinit var backgroundimg : ImageView
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
    backgroundimg = findViewById<ImageView>(R.id.background)

}
fun change_wallpaper() {
    var wallpapers = arrayOf(wallpapers here)
    var choice = (0..wallpapers.size).random()
    var chosenwallpaper = wallpapers[choice]

    // DONT KNOW WHAT TO DO HERE
    //if image is bitmap
    backgroundimg.setImageBitmap(chosenwallpaper)
    //if image is drawable
    backgroundimg.setImageDrawable(chosenwallpaper)

}
© www.soinside.com 2019 - 2024. All rights reserved.