I don´t think there is code to mirror a Font-Icon.Also I have a button where the Material icon image needs to be flipped left to right (mirror image).
Does this buttons have a single icon/character on them?For some button images I need to reverse the colours.
Also I have a button where the Material icon image needs to be flipped left to right (mirror image).
What would be the simplest way to do these things?
RBS
OK, will try that.I don´t think there is code to mirror a Font-Icon.
Best i can imagine is to draw it on a canvas and mirror that canvas.
You then can use the canvas to store the mirrored Icon as an Image.
Very nice! That works indeed perfect, thanks.
Not quite.Probably I am missing what you want to obtain, but...
That character has the internal X that is transparent.I am dealing with this Material icon: Chr(0xE14A) 'backspace
Thanks, will have a look at BitmapCreator Effects.B4X? flip and color
[B4X] BitmapCreator Effects
BitmapCreatorEffects class includes all kinds of very simple to use image effects. The class is cross platform and compatible with B4J (v6.3+), B4A (v8.3+) and B4i (v5.0+). Example: 'Greyscale an image: Dim GreyImage As B4XBitmap = effects.GreyScale(ExistingBmp) 'Blur an image: Dim BlurImage As...www.b4x.com FontAwesome To Bitmap
Unlike an older "TextToBitmap" sub that you can find in the forum, this code vertically centers the icon. It can also be used in B4J and B4i (only need to change the font line). Sub Activity_Create(FirstTime As Boolean) 'usage example Activity.AddMenuItem3("Android", "Test"...www.b4x.com
View attachment 157044
Sub TextToB4XBitmap(strText As String, tf As Typeface, fFontSize As Float, iFontColour As Int, bDrawCircle As Boolean) As B4XBitmap
Dim xui As XUI
Dim p As B4XView = xui.CreatePanel("")
Dim iPanelWidth As Int
Dim cvs1 As B4XCanvas
Dim fnt As B4XFont
Dim tTD As tTextDimensions
Select Case tf
Case tf.DEFAULT
fnt = xui.CreateFont(tf.DEFAULT, fFontSize)
Case tf.FONTAWESOME
fnt = xui.CreateFontAwesome(fFontSize)
Case tf.MATERIALICONS
fnt = xui.CreateMaterialIcons(fFontSize)
End Select
tTD = GetTextDimensionsSingleLine(strText, Null, fFontSize, tf, Null)
iPanelWidth = Max(tTD.fHeight, tTD.fWidth)
p.SetLayoutAnimated(0, 0, 0, iPanelWidth, iPanelWidth)
cvs1.Initialize(p)
cvs1.DrawText(strText, cvs1.TargetRect.CenterX, cvs1.TargetRect.CenterY + tTD.fHeight / 2, fnt, iFontColour, "CENTER")
If bDrawCircle Then
cvs1.DrawCircle(cvs1.TargetRect.CenterX, cvs1.TargetRect.CenterY, iPanelWidth / 2, Colors.Black, False, 2)
End If
Dim b As B4XBitmap = cvs1.CreateBitmap
cvs1.Release
Return b
End Sub
Sub GetTextDimensionsSingleLine(strText As String, CSB As CSBuilder, fTextSize2 As Float, tf As Typeface, arrPadding() As Int) As tTextDimensions
Dim cvs2 As Canvas
Dim bmp2 As Bitmap
Dim tTD As tTextDimensions
If CSB.IsInitialized Then
strText = CSB.ToString
End If
bmp2.InitializeMutable(2dip, 2dip)
cvs2.Initialize2(bmp2)
tTD.Initialize
If IsArray(arrPadding) Then
tTD.fWidth = cvs2.MeasureStringWidth(strText, tf, fTextSize2) + arrPadding(0) + arrPadding(2)
tTD.fHeight = cvs2.MeasureStringHeight(strText, tf, fTextSize2) + arrPadding(1) + arrPadding(3)
Else
tTD.fWidth = cvs2.MeasureStringWidth(strText, tf, fTextSize2)
tTD.fHeight = cvs2.MeasureStringHeight(strText, tf, fTextSize2)
End If
Return tTD
End Sub
I probably will need something like this:B4X? flip and color
[B4X] BitmapCreator Effects
BitmapCreatorEffects class includes all kinds of very simple to use image effects. The class is cross platform and compatible with B4J (v6.3+), B4A (v8.3+) and B4i (v5.0+). Example: 'Greyscale an image: Dim GreyImage As B4XBitmap = effects.GreyScale(ExistingBmp) 'Blur an image: Dim BlurImage As...www.b4x.com FontAwesome To Bitmap
Unlike an older "TextToBitmap" sub that you can find in the forum, this code vertically centers the icon. It can also be used in B4J and B4i (only need to change the font line). Sub Activity_Create(FirstTime As Boolean) 'usage example Activity.AddMenuItem3("Android", "Test"...www.b4x.com
View attachment 157044
'Replaces OldColor with NewColor.
'KeepAlphaLevel - If true then the alpha level of the source color is kept.
Sub ReplaceColor(bmp2 As B4XBitmap, OldColor As Int, NewColor As Int, KeepAlphaLevel As Boolean) As B4XBitmap
Dim x As Int
Dim y As Int
Dim BMC As BitmapCreator = CreateBC(bmp2)
Dim oldargb As ARGBColor
Dim newargb As ARGBColor
Dim a As ARGBColor
BMC.ColorToARGB(OldColor, oldargb)
BMC.ColorToARGB(NewColor, newargb)
If KeepAlphaLevel Then
For x = 0 To BMC.mWidth - 1
For y = 0 To BMC.mHeight - 1
BMC.GetARGB(x, y, a)
If a.r = oldargb.r And a.g = oldargb.g And a.b = oldargb.b Then
newargb.a = a.a
BMC.SetARGB(x, y, newargb)
End If
Next
Next
Else
For x = 0 To BMC.mWidth - 1
For y = 0 To BMC.mHeight - 1
BMC.GetARGB(x, y, a)
If a.a = oldargb.a And a.r = oldargb.r And a.g = oldargb.g And a.b = oldargb.b Then
newargb.a = a.a
BMC.SetARGB(x, y, newargb)
End If
Next
Next
End If
Return BMC.Bitmap
End Sub
It is more complex to change the internal color of a font converted to a bitmap, since it has alpha color in its internal outlines.I probably will need something like this:
B4X:'Replaces OldColor with NewColor. 'KeepAlphaLevel - If true then the alpha level of the source color is kept. Sub ReplaceColor(bmp2 As B4XBitmap, OldColor As Int, NewColor As Int, KeepAlphaLevel As Boolean) As B4XBitmap Dim x As Int Dim y As Int Dim BMC As BitmapCreator = CreateBC(bmp2) Dim oldargb As ARGBColor Dim newargb As ARGBColor Dim a As ARGBColor BMC.ColorToARGB(OldColor, oldargb) BMC.ColorToARGB(NewColor, newargb) If KeepAlphaLevel Then For x = 0 To BMC.mWidth - 1 For y = 0 To BMC.mHeight - 1 BMC.GetARGB(x, y, a) If a.r = oldargb.r And a.g = oldargb.g And a.b = oldargb.b Then newargb.a = a.a BMC.SetARGB(x, y, newargb) End If Next Next Else For x = 0 To BMC.mWidth - 1 For y = 0 To BMC.mHeight - 1 BMC.GetARGB(x, y, a) If a.a = oldargb.a And a.r = oldargb.r And a.g = oldargb.g And a.b = oldargb.b Then newargb.a = a.a BMC.SetARGB(x, y, newargb) End If Next Next End If Return BMC.Bitmap End Sub
RBS
Dim Bmp As B4XBitmap = FontToBitmap(Chr(0xE14A), xui.CreateMaterialIcons(100), 100, xui.Color_Black)
Dim NewBmp As B4XBitmap = ChangeColorBasedOnAlphaLevel(Bmp, xui.Color_Red)
B4XImageView1.Bitmap = NewBmp
B4XImageView1.mBackgroundColor = xui.Color_Transparent
B4XImageView1.Update
Public Sub FontToBitmap (Text As String, FontType As B4XFont, FontSize As Float, Color As Int) As B4XBitmap
Dim xui As XUI
Dim p As B4XView = xui.CreatePanel("")
p.SetLayoutAnimated(0, 0, 0, FontSize, FontSize)
Dim cvs1 As B4XCanvas
cvs1.Initialize(p)
Dim r As B4XRect = cvs1.MeasureText(Text, FontType)
Dim BaseLine As Int = cvs1.TargetRect.CenterY - r.Height / 2 - r.Top
cvs1.DrawText(Text, cvs1.TargetRect.CenterX, BaseLine, FontType, Color, "CENTER")
Dim b As B4XBitmap = cvs1.CreateBitmap
cvs1.Release
Return b
End Sub
Public Sub ChangeColorBasedOnAlphaLevel(bmp As B4XBitmap, NewColor As Int) As B4XBitmap
Dim bc As BitmapCreator
bc.Initialize(bmp.Width, bmp.Height)
bc.CopyPixelsFromBitmap(bmp)
Dim a1, a2 As ARGBColor
bc.ColorToARGB(NewColor, a1)
For y = 0 To bc.mHeight - 1
For x = 0 To bc.mWidth - 1
bc.GetARGB(x, y, a2)
If a2.a > 0 Then
a2.r = a1.r
a2.g = a1.g
a2.b = a1.b
bc.SetARGB(x, y, a2)
End If
Next
Next
Return bc.Bitmap
End Sub
I solved this now with an icon file that has the right colours (file attached).I'm looking for a routine that I have that changes the internal color of a font converted to bitmap.
View attachment 157046
'Sets the rotation angle around the Y or X axis of the given view
'v = view
'Angle = rotation angle in degrees
Sub setRotationY(v As View, angle As Float)
Dim jo = v As JavaObject
jo.RunMethod("setRotationY", Array As Object(angle))
End Sub
Sub setRotationX(v As View, angle As Float)
Dim jo = v As JavaObject
jo.RunMethod("setRotationX", Array As Object(angle))
End Sub
'Sets the X pivot point of the given view
'v = view
'X = X coordinate of the pivot in pixels
'reference upper left corner, default pivot middle of the view
Sub setPivotX(v As View, X As Float)
Dim jo = v As JavaObject
jo.RunMethod("setPivotX", Array As Object(X))
End Sub
Sub setPivotY(v As View, X As Float)
Dim jo = v As JavaObject
jo.RunMethod("setPivotY", Array As Object(X))
End Sub
Sub FlipViewImage(v As View, bHorizontal As Boolean)
If bHorizontal Then
setPivotX(v, v.Width / 2)
setRotationY(v, 180)
Else
setPivotY(v, v.Height / 2)
setRotationX(v, 180)
End If
End Sub
Sub BitMapFromFile(strFolder As String, strFile As String, iOldColour As Int, iNewColour As Int, _
bKeepAlphaLevel As Boolean, iWidth As Int, iHeight As Int) As Bitmap
Dim bmp As Bitmap
bmp = LoadBitmapResize(strFolder, strFile, (iWidth / fActivityWidthRatio) / fTextRatio, (iHeight / fActivityHeightRatio) / fTextRatio, True)
If iOldColour <> iNewColour Then
bmp = ReplaceColor(bmp, iOldColour, iNewColour, bKeepAlphaLevel)
End If
Return bmp
End Sub
Sub ReplaceColor(bmp2 As B4XBitmap, OldColor As Int, NewColor As Int, KeepAlphaLevel As Boolean) As B4XBitmap
Dim x As Int
Dim y As Int
Dim BMC As BitmapCreator = CreateBC(bmp2)
Dim oldargb As ARGBColor
Dim newargb As ARGBColor
Dim a As ARGBColor
BMC.ColorToARGB(OldColor, oldargb)
BMC.ColorToARGB(NewColor, newargb)
If KeepAlphaLevel Then
For x = 0 To BMC.mWidth - 1
For y = 0 To BMC.mHeight - 1
BMC.GetARGB(x, y, a)
If a.r = oldargb.r And a.g = oldargb.g And a.b = oldargb.b Then
newargb.a = a.a
BMC.SetARGB(x, y, newargb)
End If
Next
Next
Else
For x = 0 To BMC.mWidth - 1
For y = 0 To BMC.mHeight - 1
BMC.GetARGB(x, y, a)
If a.a = oldargb.a And a.r = oldargb.r And a.g = oldargb.g And a.b = oldargb.b Then
newargb.a = a.a
BMC.SetARGB(x, y, newargb)
End If
Next
Next
End If
Return BMC.Bitmap
End Sub
'All this has to do with a custom keyboard and in the class clsKeyboard:
'flipping the image won't take place here as it is not the default to do a forwards delete,
'but just added this here as an example
'Del button at the right side of the fourth row
'----------------------------------------------
dLeft = pnlKeys.Width - (42dip + 6dip)
Dim b As Button
b.Initialize("KeyboardButton")
b.TextColor = Colors.Black
b.TextSize = fButtonTextSize
b.SingleLine = True
'b.Typeface = Typeface.MATERIALICONS
b.Typeface = Typeface.DEFAULT
b.Tag = 38
'b.Text = Chr(0xE14A) 'backspace
b.Gravity = Gravity.CENTER
b.Padding = Array As Int(0,0,0,0)
Dim CD As ColorDrawable
CD.Initialize(0xFFC8C8C8, 6dip) 'GREY, rounded corners
b.Background = CD
Dim bmp As Bitmap = cMP.BitMapFromFile(File.DirAssets, "back_delete.png", 0, 0, True, 35dip, 30dip)
Dim cs As CSBuilder
cs.Initialize.Image(bmp, 35dip, 30dip,False)
b.Text = cs
pnlKeys.AddView(b, dLeft, dTop, 42dip, 36dip)
'----------------------------------------------------------------------------------
'To mirror flip the button image (do this after the button was added to the panel!)
FlipViewImage(b, True)
'----------------------------------------------------------------------------------
arrKeyboardButtons(38) = b
Forgot to attach the icon file.I solved this now with an icon file that has the right colours (file attached).
This seems simpler than altering colours and works fine now.
Still flipping the image with the Java code.
B4X:'Sets the rotation angle around the Y or X axis of the given view 'v = view 'Angle = rotation angle in degrees Sub setRotationY(v As View, angle As Float) Dim jo = v As JavaObject jo.RunMethod("setRotationY", Array As Object(angle)) End Sub Sub setRotationX(v As View, angle As Float) Dim jo = v As JavaObject jo.RunMethod("setRotationX", Array As Object(angle)) End Sub 'Sets the X pivot point of the given view 'v = view 'X = X coordinate of the pivot in pixels 'reference upper left corner, default pivot middle of the view Sub setPivotX(v As View, X As Float) Dim jo = v As JavaObject jo.RunMethod("setPivotX", Array As Object(X)) End Sub Sub setPivotY(v As View, X As Float) Dim jo = v As JavaObject jo.RunMethod("setPivotY", Array As Object(X)) End Sub Sub FlipViewImage(v As View, bHorizontal As Boolean) If bHorizontal Then setPivotX(v, v.Width / 2) setRotationY(v, 180) Else setPivotY(v, v.Height / 2) setRotationX(v, 180) End If End Sub Sub BitMapFromFile(strFolder As String, strFile As String, iOldColour As Int, iNewColour As Int, _ bKeepAlphaLevel As Boolean, iWidth As Int, iHeight As Int) As Bitmap Dim bmp As Bitmap bmp = LoadBitmapResize(strFolder, strFile, (iWidth / fActivityWidthRatio) / fTextRatio, (iHeight / fActivityHeightRatio) / fTextRatio, True) If iOldColour <> iNewColour Then bmp = ReplaceColor(bmp, iOldColour, iNewColour, bKeepAlphaLevel) End If Return bmp End Sub Sub ReplaceColor(bmp2 As B4XBitmap, OldColor As Int, NewColor As Int, KeepAlphaLevel As Boolean) As B4XBitmap Dim x As Int Dim y As Int Dim BMC As BitmapCreator = CreateBC(bmp2) Dim oldargb As ARGBColor Dim newargb As ARGBColor Dim a As ARGBColor BMC.ColorToARGB(OldColor, oldargb) BMC.ColorToARGB(NewColor, newargb) If KeepAlphaLevel Then For x = 0 To BMC.mWidth - 1 For y = 0 To BMC.mHeight - 1 BMC.GetARGB(x, y, a) If a.r = oldargb.r And a.g = oldargb.g And a.b = oldargb.b Then newargb.a = a.a BMC.SetARGB(x, y, newargb) End If Next Next Else For x = 0 To BMC.mWidth - 1 For y = 0 To BMC.mHeight - 1 BMC.GetARGB(x, y, a) If a.a = oldargb.a And a.r = oldargb.r And a.g = oldargb.g And a.b = oldargb.b Then newargb.a = a.a BMC.SetARGB(x, y, newargb) End If Next Next End If Return BMC.Bitmap End Sub 'All this has to do with a custom keyboard and in the class clsKeyboard: 'flipping the image won't take place here as it is not the default to do a forwards delete, 'but just added this here as an example 'Del button at the right side of the fourth row '---------------------------------------------- dLeft = pnlKeys.Width - (42dip + 6dip) Dim b As Button b.Initialize("KeyboardButton") b.TextColor = Colors.Black b.TextSize = fButtonTextSize b.SingleLine = True 'b.Typeface = Typeface.MATERIALICONS b.Typeface = Typeface.DEFAULT b.Tag = 38 'b.Text = Chr(0xE14A) 'backspace b.Gravity = Gravity.CENTER b.Padding = Array As Int(0,0,0,0) Dim CD As ColorDrawable CD.Initialize(0xFFC8C8C8, 6dip) 'GREY, rounded corners b.Background = CD Dim bmp As Bitmap = cMP.BitMapFromFile(File.DirAssets, "back_delete.png", 0, 0, True, 35dip, 30dip) Dim cs As CSBuilder cs.Initialize.Image(bmp, 35dip, 30dip,False) b.Text = cs pnlKeys.AddView(b, dLeft, dTop, 42dip, 36dip) '---------------------------------------------------------------------------------- 'To mirror flip the button image (do this after the button was added to the panel!) FlipViewImage(b, True) '---------------------------------------------------------------------------------- arrKeyboardButtons(38) = b
RBS
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?