SubNames: DrawTextLinearGradient, DrawTextLinearGradient1, DrawLineLinearGradient, DrawLineLinearGradient1
Description:
It is possible to apply gradients directly to objects drawn on a canvas using the Andorid Shader subclasses: LinearGradient, RadialGradient, SweepGradient and JavaObject.
These Subroutines are examples of doing so using LinearGradient:
Copy and paste the whole code block into a new project for a demo.
See the documentation in the above links and the associated Paint and Canvas object documentation for more information.
Depends on: JavaObject
Tags: Canvas, Gradient Draw
Description:
It is possible to apply gradients directly to objects drawn on a canvas using the Andorid Shader subclasses: LinearGradient, RadialGradient, SweepGradient and JavaObject.
These Subroutines are examples of doing so using LinearGradient:
B4X:
Sub Process_Globals
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.
End Sub
Sub Globals
'These global variables will be redeclared each time the activity is created.
'These variables can only be accessed from this module.
Dim Canvas As Canvas
End Sub
Sub Activity_Create(FirstTime As Boolean)
'Do not forget to load the layout file created with the visual designer. For example:
'Activity.LoadLayout("Layout1")
Canvas.Initialize(Activity)
DrawTextLinearGradient(Canvas,100,100,"Test of LinearGradient",50,Colors.Blue,Colors.Cyan,"CLAMP")
DrawTextLinearGradient1(Canvas,100,200,"Second Test of LinearGradient",50,Array As Int(Colors.Blue,Colors.Yellow,Colors.Red,Colors.Cyan),Null,"CLAMP")
DrawLineLinearGradient(Canvas,100,200,300,400,5dip,Colors.Blue,Colors.Cyan,"CLAMP")
DrawLineLinearGradient1(Canvas,100,300,300,400,5dip,Array As Int(Colors.Blue,Colors.Yellow,Colors.Cyan),Null,"CLAMP")
Activity.Invalidate
End Sub
Sub Activity_Resume
End Sub
Sub Activity_Pause (UserClosed As Boolean)
End Sub
Sub DrawTextLinearGradient(Canv As Canvas,X As Float,Y As Float,Text As String,TextSize As Float,StartColor As Int, EndColor As Int,TileMode As String)
Dim x1 As Float
'Arbitrary Y point somewhere in the displayed view
Dim y1 As Float = y + 20
Dim Paint,LinearGradient,CanvJO As JavaObject
CanvJO = Canv
CanvJO = CanvJO.GetField("canvas")
Paint.InitializeNewInstance("android.graphics.Paint",Null)
Paint.RunMethod("setTextSize",Array As Object(TextSize))
'Set Length of shader to match text
x1 = x + (Paint.RunMethod("measureText",Array(Text)))
LinearGradient.InitializeNewInstance("android.graphics.LinearGradient",Array As Object(X,Y,x1,y1,StartColor,EndColor,TileMode))
Paint.RunMethod("setShader",Array As Object(LinearGradient))
CanvJO.RunMethod("drawText",Array As Object(Text,X,Y,Paint))
End Sub
Sub DrawTextLinearGradient1(Canv As Canvas,X As Float,Y As Float,Text As String,TextSize As Float,Color() As Int,Positions() As Float,TileMode As String)
Dim x1 As Float
'Arbitrary Y point somewhere in the displayed view
Dim y1 As Float = Y + 20
Dim Paint,LinearGradient,CanvJO As JavaObject
CanvJO = Canv
CanvJO = CanvJO.GetField("canvas")
Paint.InitializeNewInstance("android.graphics.Paint",Null)
Paint.RunMethod("setTextSize",Array As Object(TextSize))
'Set Length of shader to match text
x1 = x + (Paint.RunMethod("measureText",Array(Text)))
LinearGradient.InitializeNewInstance("android.graphics.LinearGradient",Array As Object(X,Y,x1,y1,Color,Positions,TileMode))
Paint.RunMethod("setShader",Array As Object(LinearGradient))
CanvJO.RunMethod("drawText",Array As Object(Text,X,Y,Paint))
End Sub
Sub DrawLineLinearGradient(Canv As Canvas,X As Float,Y As Float,X1 As Float,Y1 As Float,StrokeWidth As Float,StartColor As Int, EndColor As Int,TileMode As String)
Dim Paint,LinearGradient,CanvJO As JavaObject
CanvJO = Canv
CanvJO = CanvJO.GetField("canvas")
Paint.InitializeNewInstance("android.graphics.Paint",Null)
Paint.RunMethod("setStrokeWidth",Array(StrokeWidth))
LinearGradient.InitializeNewInstance("android.graphics.LinearGradient",Array As Object(X,Y,X1,Y1,StartColor,EndColor,TileMode))
Paint.RunMethod("setShader",Array As Object(LinearGradient))
CanvJO.RunMethod("drawLine",Array As Object(X,Y,X1,Y1,Paint))
End Sub
Sub DrawLineLinearGradient1(Canv As Canvas,X As Float,Y As Float,X1 As Float,Y1 As Float,StrokeWidth As Float,Color() As Int,Positions() As Float,TileMode As String)
Dim Paint,LinearGradient,CanvJO As JavaObject
CanvJO = Canv
CanvJO = CanvJO.GetField("canvas")
Paint.InitializeNewInstance("android.graphics.Paint",Null)
Paint.RunMethod("setStrokeWidth",Array(StrokeWidth))
LinearGradient.InitializeNewInstance("android.graphics.LinearGradient",Array As Object(X,Y,X1,Y1,Color,Positions,TileMode))
Paint.RunMethod("setShader",Array As Object(LinearGradient))
CanvJO.RunMethod("drawLine",Array As Object(X,Y,X1,Y1,Paint))
End Sub
Copy and paste the whole code block into a new project for a demo.
See the documentation in the above links and the associated Paint and Canvas object documentation for more information.
Depends on: JavaObject
Tags: Canvas, Gradient Draw
Last edited: