feat: added secondary icon attribute to key.
This commit is contained in:
parent
35b4c656c7
commit
d797ba33c3
|
@ -138,6 +138,9 @@ class MyKeyboard {
|
|||
/** Icon to display instead of a label. Icon takes precedence over a label */
|
||||
var icon: Drawable? = null
|
||||
|
||||
/** Icon to display top left of an icon.*/
|
||||
var secondaryIcon: Drawable? = null
|
||||
|
||||
/** Width of the key, not including the gap */
|
||||
var width: Int
|
||||
|
||||
|
@ -204,6 +207,9 @@ class MyKeyboard {
|
|||
icon = a.getDrawable(R.styleable.MyKeyboard_Key_keyIcon)
|
||||
icon?.setBounds(0, 0, icon!!.intrinsicWidth, icon!!.intrinsicHeight)
|
||||
|
||||
secondaryIcon = a.getDrawable(R.styleable.MyKeyboard_Key_secondaryKeyIcon)
|
||||
secondaryIcon?.setBounds(0, 0, secondaryIcon!!.intrinsicWidth, secondaryIcon!!.intrinsicHeight)
|
||||
|
||||
label = a.getText(R.styleable.MyKeyboard_Key_keyLabel) ?: ""
|
||||
topSmallNumber = a.getString(R.styleable.MyKeyboard_Key_topSmallNumber) ?: ""
|
||||
|
||||
|
@ -230,10 +236,12 @@ class MyKeyboard {
|
|||
fun isInside(x: Int, y: Int): Boolean {
|
||||
val leftEdge = edgeFlags and EDGE_LEFT > 0
|
||||
val rightEdge = edgeFlags and EDGE_RIGHT > 0
|
||||
return ((x >= this.x || leftEdge && x <= this.x + width)
|
||||
&& (x < this.x + width || rightEdge && x >= this.x)
|
||||
&& (y >= this.y && y <= this.y + height)
|
||||
&& (y < this.y + height && y >= this.y))
|
||||
return (
|
||||
(x >= this.x || leftEdge && x <= this.x + width) &&
|
||||
(x < this.x + width || rightEdge && x >= this.x) &&
|
||||
(y >= this.y && y <= this.y + height) &&
|
||||
(y < this.y + height && y >= this.y)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -249,7 +257,7 @@ class MyKeyboard {
|
|||
mDefaultHorizontalGap = 0
|
||||
mDefaultWidth = mDisplayWidth / 10
|
||||
mDefaultHeight = mDefaultWidth
|
||||
mKeyboardHeightMultiplier = getKeyboardHeightMultiplier(context.config.keyboardHeightMultiplier);
|
||||
mKeyboardHeightMultiplier = getKeyboardHeightMultiplier(context.config.keyboardHeightMultiplier)
|
||||
mKeys = ArrayList()
|
||||
mEnterKeyType = enterKeyType
|
||||
loadKeyboard(context, context.resources.getXml(xmlLayoutResId))
|
||||
|
@ -273,7 +281,7 @@ class MyKeyboard {
|
|||
row.defaultHeight = mDefaultHeight
|
||||
row.defaultWidth = keyWidth
|
||||
row.defaultHorizontalGap = mDefaultHorizontalGap
|
||||
mKeyboardHeightMultiplier = getKeyboardHeightMultiplier(context.config.keyboardHeightMultiplier);
|
||||
mKeyboardHeightMultiplier = getKeyboardHeightMultiplier(context.config.keyboardHeightMultiplier)
|
||||
|
||||
characters.forEachIndexed { index, character ->
|
||||
val key = Key(row)
|
||||
|
@ -386,7 +394,7 @@ class MyKeyboard {
|
|||
}
|
||||
|
||||
private fun getKeyboardHeightMultiplier(multiplierType: Int): Float {
|
||||
return when(multiplierType) {
|
||||
return when (multiplierType) {
|
||||
KEYBOARD_HEIGHT_MULTIPLIER_SMALL -> 1.0F
|
||||
KEYBOARD_HEIGHT_MULTIPLIER_MEDIUM -> 1.2F
|
||||
KEYBOARD_HEIGHT_MULTIPLIER_LARGE -> 1.4F
|
||||
|
|
|
@ -612,13 +612,33 @@ class MyKeyboardView @JvmOverloads constructor(context: Context, attrs: Attribut
|
|||
} else if (code == KEYCODE_DELETE || code == KEYCODE_SHIFT || code == KEYCODE_EMOJI) {
|
||||
key.icon!!.applyColorFilter(mTextColor)
|
||||
}
|
||||
val keyIcon = key.icon!!
|
||||
val secondaryIcon = key.secondaryIcon
|
||||
|
||||
val drawableX = (key.width - key.icon!!.intrinsicWidth) / 2
|
||||
val drawableY = (key.height - key.icon!!.intrinsicHeight) / 2
|
||||
canvas.translate(drawableX.toFloat(), drawableY.toFloat())
|
||||
key.icon!!.setBounds(0, 0, key.icon!!.intrinsicWidth, key.icon!!.intrinsicHeight)
|
||||
key.icon!!.draw(canvas)
|
||||
canvas.translate(-drawableX.toFloat(), -drawableY.toFloat())
|
||||
if (secondaryIcon != null) {
|
||||
val keyIconWidth = keyIcon.intrinsicWidth * 1
|
||||
val keyIconHeight = keyIcon.intrinsicWidth * 1
|
||||
val secondaryIconWidth = (secondaryIcon.intrinsicWidth * 0.6).toInt()
|
||||
val secondaryIconHeight = (secondaryIcon.intrinsicHeight * 0.6).toInt()
|
||||
|
||||
secondaryIcon.setBounds(key.width - secondaryIconWidth, 0, key.width, secondaryIconHeight)
|
||||
secondaryIcon.draw(canvas)
|
||||
|
||||
val drawableX = (key.width - keyIconWidth) / 2
|
||||
val drawableY = (key.height - keyIconHeight) / 2
|
||||
canvas.translate(drawableX.toFloat(), drawableY.toFloat() / 1.5f)
|
||||
|
||||
keyIcon.setBounds(0, 0, keyIconWidth, keyIconHeight)
|
||||
keyIcon.draw(canvas)
|
||||
canvas.translate(-drawableX.toFloat(), -drawableY.toFloat())
|
||||
} else {
|
||||
val drawableX = (key.width - keyIcon.intrinsicWidth) / 2
|
||||
val drawableY = (key.height - keyIcon.intrinsicHeight) / 2
|
||||
canvas.translate(drawableX.toFloat(), drawableY.toFloat())
|
||||
keyIcon.setBounds(0, 0, keyIcon.intrinsicWidth, keyIcon.intrinsicHeight)
|
||||
keyIcon.draw(canvas)
|
||||
canvas.translate(-drawableX.toFloat(), -drawableY.toFloat())
|
||||
}
|
||||
}
|
||||
canvas.translate(-key.x.toFloat(), -key.y.toFloat())
|
||||
}
|
||||
|
|
|
@ -33,6 +33,8 @@
|
|||
<attr name="keyLabel" format="string" />
|
||||
<!-- The icon to display on the key instead of the label. -->
|
||||
<attr name="keyIcon" format="reference" />
|
||||
<!-- The icon to display on the top left of a key with icon. -->
|
||||
<attr name="secondaryKeyIcon" format="reference" />
|
||||
<!-- Top small number shown above letters of the first row. -->
|
||||
<attr name="topSmallNumber" format="string" />
|
||||
</declare-styleable>
|
||||
|
|
Loading…
Reference in New Issue