简体   繁体   English

如何在按钮Android Studio中添加图像背景

[英]How to add image background in button Android studio

I want to add an image in button view but it is not showing please help and correct me.我想在按钮视图中添加一个图像,但它没有显示,请帮助并纠正我。 here is my mainActivity code:这是我的主要活动代码:

select_image_button.setOnClickListener {
        Log.d("MainActivity", "try to show Photo")

        val intent = Intent(Intent.ACTION_PICK)
        intent.type = "image/*"
        startActivityForResult(intent,0)
    }

Override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    super.onActivityResult(requestCode, resultCode, data)

    if (requestCode == 0 && resultCode == Activity.RESULT_OK && data != null){
        Log.d("MainActivity", "Photo was selected")

        val uri = data.data
        val bit = MediaStore.Images.Media.getBitmap(contentResolver,uri)

        val bitmapDrawable = BitmapDrawable(bit)
        select_image_button.setBackgroundDrawable(bitmapDrawable)
    }

here is my XML code:这是我的 XML 代码:

<Button
        android:textColor="@android:color/white"
        android:text="Select Photo"
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:id="@+id/select_image_button"
        />
select_image_button.background = bitmapDrawable

EDIT: After your clarify, here is what you looking for:编辑:在您澄清之后,这就是您要查找的内容:

 override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    val intent = Intent(Intent.ACTION_GET_CONTENT)
    intent.type = "image/*"
    if (intent.resolveActivity(packageManager) != null) {
        startActivityForResult(intent, REQUEST_SELECT_IMAGE_IN_ALBUM)
    }
}


override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    super.onActivityResult(requestCode, resultCode, data)

    if (requestCode === REQUEST_SELECT_IMAGE_IN_ALBUM && resultCode === Activity.RESULT_OK) {
        val selectedImage = data?.data
        decodeUri(selectedImage!!);
    }
}

fun decodeUri(uri: Uri) {
    var parcelFD: ParcelFileDescriptor? = null
    try {
        parcelFD = contentResolver.openFileDescriptor(uri, "r")
        val imageSource = parcelFD!!.fileDescriptor

        // Decode image size
        val o = BitmapFactory.Options()
        o.inJustDecodeBounds = true
        BitmapFactory.decodeFileDescriptor(imageSource, null, o)

        // the new size we want to scale to
        val REQUIRED_SIZE = 1024

        // Find the correct scale value. It should be the power of 2.
        var width_tmp = o.outWidth
        var height_tmp = o.outHeight
        var scale = 1
        while (true) {
            if (width_tmp < REQUIRED_SIZE && height_tmp < REQUIRED_SIZE) {
                break
            }
            width_tmp /= 2
            height_tmp /= 2
            scale *= 2
        }

        // decode with inSampleSize
        val o2 = BitmapFactory.Options()
        o2.inSampleSize = scale
        val bitmap = BitmapFactory.decodeFileDescriptor(imageSource, null, o2)

        imageButton.setImageBitmap(bitmap)

    } catch (e: FileNotFoundException) {
        // handle errors
    } catch (e: IOException) {
        // handle errors
    } finally {
        if (parcelFD != null)
            try {
                parcelFD.close()
            } catch (e: IOException) {
                // ignored
            }

    }
}

I tried myself too.我自己也试过了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在android studio的背景图片上添加按钮和其他元素 - how to add a button and other elements on a background image in android studio 如何在android studio中添加图标图像按钮 - How to add icon image button in android studio 如何在Android Studio中添加带有图像作为背景的自定义标题栏 - How to add a custom title bar with an image as background in android studio 如何在view pager android studio的背景中添加gif图像? - How to add gif image in the background of view pager android studio? 如何在Android应用程序中只添加背景图像和链接按钮? - how to add just an background image and a link button in android application? 如何在android studio中添加按钮? - how to add button in android studio? 如何在Android Studio中添加按钮 - How to add a button in android studio 如何在Android Studio中使用以我的图像文件为背景的按钮制作一个圆形按钮? - How do I make a round edged button in Android Studio with the button using my image file as background? 如何在 android studio 中更改按钮的背景和文本颜色? - how to alternate background and textcolor of a button in android studio? 如何在 Android Studio 中制作具有透明背景的图像资产? (带有选项添加图像资产) - How to make image asset with transparent background in Android studio? (with option add image asset)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM