Adding bitmap support for DefaultCommandItem

This commit is contained in:
NamanPandey 2022-03-23 16:10:08 +05:30 коммит произвёл NamanPandey
Родитель 61cbb5799b
Коммит 78669d2178
4 изменённых файлов: 46 добавлений и 16 удалений

Просмотреть файл

@ -5,26 +5,22 @@
package com.microsoft.fluentuidemo.demos
import android.graphics.Bitmap
import android.graphics.Canvas
import android.os.Bundle
import android.util.TypedValue
import android.view.View
import android.widget.SeekBar
import android.widget.Toast
import androidx.appcompat.content.res.AppCompatResources
import com.microsoft.fluentui.contextualcommandbar.CommandItem
import com.microsoft.fluentui.contextualcommandbar.CommandItemGroup
import com.microsoft.fluentui.contextualcommandbar.ContextualCommandBar
import com.microsoft.fluentui.contextualcommandbar.DefaultCommandItem
import com.microsoft.fluentuidemo.DemoActivity
import com.microsoft.fluentuidemo.R
import kotlinx.android.synthetic.main.activity_contextual_command_bar.contextual_command_bar_default
import kotlinx.android.synthetic.main.activity_contextual_command_bar.contextual_command_bar_dismiss_position_end
import kotlinx.android.synthetic.main.activity_contextual_command_bar.contextual_command_bar_dismiss_position_group
import kotlinx.android.synthetic.main.activity_contextual_command_bar.contextual_command_bar_group_space_seekbar
import kotlinx.android.synthetic.main.activity_contextual_command_bar.contextual_command_bar_group_space_value
import kotlinx.android.synthetic.main.activity_contextual_command_bar.contextual_command_bar_item_space_seekbar
import kotlinx.android.synthetic.main.activity_contextual_command_bar.contextual_command_bar_item_space_value
import kotlinx.android.synthetic.main.activity_contextual_command_bar.insert_item
import kotlinx.android.synthetic.main.activity_contextual_command_bar.update_item
import kotlinx.android.synthetic.main.activity_contextual_command_bar.*
class ContextualCommandBarActivity : DemoActivity() {
override val contentLayoutId: Int
@ -35,9 +31,21 @@ class ContextualCommandBarActivity : DemoActivity() {
val itemGroups = arrayListOf<CommandItemGroup>()
// Used to test bitmap support
var drawable = AppCompatResources.getDrawable(this, R.drawable.ic_fluent_add_24_regular)
val bitmap: Bitmap = Bitmap.createBitmap(
drawable!!.getIntrinsicWidth(),
drawable!!.getIntrinsicHeight(),
Bitmap.Config.ARGB_8888
)
val canvas = Canvas(bitmap)
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight())
drawable.draw(canvas)
itemGroups.add(CommandItemGroup()
.addItem(DefaultCommandItem(
R.drawable.ic_fluent_add_24_regular,
bitmap = bitmap,
contentDescription = getString(R.string.contextual_command_accessibility_add)
))
.addItem(DefaultCommandItem(
@ -129,12 +137,12 @@ class ContextualCommandBarActivity : DemoActivity() {
insert_item.setOnClickListener {
contextual_command_bar_default.addItemGroup(CommandItemGroup()
.addItem(DefaultCommandItem(
R.drawable.ic_fluent_add_24_regular,
getString(R.string.contextual_command_accessibility_add)
icon = R.drawable.ic_fluent_add_24_regular,
contentDescription = getString(R.string.contextual_command_accessibility_add)
))
.addItem(DefaultCommandItem(
R.drawable.ic_fluent_mention_24_regular,
getString(R.string.contextual_command_accessibility_mention),
icon = R.drawable.ic_fluent_mention_24_regular,
contentDescription = getString(R.string.contextual_command_accessibility_mention),
enabled = false
))
)

Просмотреть файл

@ -5,6 +5,7 @@
package com.microsoft.fluentui.contextualcommandbar
import android.graphics.Bitmap
import androidx.annotation.DrawableRes
import android.view.View
@ -25,11 +26,16 @@ interface CommandItem {
fun getTag(): String? = null
/**
* The primary content displayed on the CommandItem
* The drawable based primary content displayed on the CommandItem
*/
@DrawableRes
fun getIcon(): Int = 0
/**
* The bitmap based primary content displayed on the CommandItem
*/
fun getBitmapIcon(): Bitmap? = null
/**
* The secondary content displayed on the CommandItem if [getIcon] is not set
*/

Просмотреть файл

@ -64,6 +64,7 @@ internal class CommandItemAdapter(
val isItemEnabled = commandItem.isEnabled()
val label = commandItem.getLabel()
val icon = commandItem.getIcon()
val bitmapIcon = commandItem.getBitmapIcon()
val description = commandItem.getContentDescription()
if (icon != 0) {
// Using icon as primary display content
@ -76,6 +77,15 @@ internal class CommandItemAdapter(
isSelected = isItemSelected
}
} else if (bitmapIcon != null) {
viewHolder.label.isVisible = false
with(viewHolder.icon) {
isVisible = true
setImageBitmap(bitmapIcon)
contentDescription = description
isEnabled = isItemEnabled
isSelected = isItemSelected
}
} else if (!label.isNullOrEmpty()) {
viewHolder.icon.isVisible = false
with(viewHolder.label) {

Просмотреть файл

@ -5,6 +5,7 @@
package com.microsoft.fluentui.contextualcommandbar
import android.graphics.Bitmap
import androidx.annotation.DrawableRes
open class DefaultCommandItem(
@ -12,7 +13,8 @@ open class DefaultCommandItem(
private var label: String? = null,
private var contentDescription: String? = null,
private var enabled: Boolean = true,
private var selected: Boolean = false
private var selected: Boolean = false,
private var bitmap: Bitmap? = null,
) : CommandItem {
fun setEnabled(enabled: Boolean) {
@ -39,6 +41,10 @@ open class DefaultCommandItem(
return icon
}
override fun getBitmapIcon(): Bitmap? {
return bitmap
}
override fun getContentDescription(): String? {
return contentDescription
}