Sub Globals
'These global variables will be redeclared each time the activity is created.
'These variables can only be accessed from this module.
Dim mainColor As Int = 0xFFC08000 '0xFFE0E5EC
Dim lightColor As Int = 0xFFE0A000 '0xFFFFFFFF
Dim darkColor As Int = 0xFFA06000 '0xFFA3B1C6
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")
Activity.Color = mainColor
Dim P As Label = createLabel("Warmer")
Dim WW As Int = Activity.Width/2
Dim HH As Int = WW/2
Activity.AddView(P, (Activity.Width-WW)/2, Activity.Height/4-HH/2, WW, HH)
GenerateViewShadow(P, WW/6, 0.2)
Dim P2 As Label = createLabel("NEW")
Dim WW As Int = Activity.Width/2
Dim HH As Int = WW
Activity.AddView(P2, (Activity.Width-WW)/2, 2*Activity.Height/4-HH/2, WW, HH)
GenerateViewShadowWithPath(P2, 0.2)
Dim P3 As Label = createLabel("Year!!")
Dim WW As Int = Activity.Width/3
Dim HH As Int = WW/2
Activity.AddView(P3, (Activity.Width-WW)/2, 3*Activity.Height/4-HH/2, WW, HH)
GenerateViewShadow(P3, P.Width/12, 0.2)
End Sub
Sub createLabel(myText As String) As Label
Dim l As Label
l.Initialize("")
l.Gravity = Gravity.CENTER
l.TextColor = Colors.White
l.TextSize = 20
l.Text = myText
Return l
End Sub
Sub GenerateViewShadow( P As B4XView, cornerRadius As Int, insetPercentage As Float)
Dim CVX As B4XCanvas
Dim BigWW As Int = P.Width
Dim BigHH As Int = P.Height
' inset percentage will be according to the smallest dim, but will be the same for both direction
Dim absInset As Float = insetPercentage*Min(BigWW, BigHH)
Dim SmallWW As Int = BigWW - absInset
Dim SmallHH As Int = BigHH - absInset
CVX.Initialize(P)
Dim PX As B4XPath
Dim R0 As B4XRect
Dim dW As Int = (BigWW-SmallWW)/2
Dim dH As Int = (BigHH-SmallHH)/2
Dim d As Int = Max(dW, dH)
' UPPER (light) shadow
For k = 0 To d
Dim kW As Float = k*dW/d
Dim kH As Float = k*dH/d
R0.Initialize( kW , kH, SmallWW+dW-kW, SmallHH+dH-kH)
PX.InitializeRoundedRect(R0, cornerRadius)
Dim pColor As Int = FindSolidColorBetween(mainColor, lightColor, 1.0*k*k*k/(d*d*d))
CVX.DrawPath(PX, pColor, True, 0)
Next
' LOWER (dark) shadow
For k = 0 To d
Dim kW As Float = k*dW/d
Dim kH As Float = k*dH/d
R0.Initialize( 2*dW-kW , 2*dH-kH, 2*dW+SmallWW-kW, 2*dH+SmallHH-kH)
PX.InitializeRoundedRect(R0, cornerRadius)
Dim pColor As Int = FindSolidColorBetween(mainColor, darkColor, 1.0*k*k*k/(d*d*d))
CVX.DrawPath(PX, pColor, True, 0)
Next
' Draw the 'plain' area with the main color.
R0.Initialize( dW , dH, dW+SmallWW, dH+SmallHH)
PX.InitializeRoundedRect(R0, cornerRadius)
CVX.DrawPath(PX, mainColor, True, 0)
End Sub
Sub GenerateViewShadowWithPath( P As B4XView, insetPercentage As Float)
Dim CVX As B4XCanvas
Dim BigWW As Int = P.Width
Dim BigHH As Int = P.Height
' inset percentage will be according to the smallest dim, but will be the same for both direction
Dim absInset As Float = insetPercentage*Min(BigWW, BigHH)
Dim SmallWW As Int = BigWW - absInset
Dim SmallHH As Int = BigHH - absInset
' Let's build an hexagonal (closed) Path
Dim PathPoints(7,2) As Float
For k=0 To 6
PathPoints(k,0) = SmallWW/2*(1+CosD(60*k))
PathPoints(k,1) = SmallHH/2*(1+SinD(60*k))
Next
CVX.Initialize(P)
Dim PX As B4XPath
Dim dW As Int = (BigWW-SmallWW)/2
Dim dH As Int = (BigHH-SmallHH)/2
Dim d As Int = Max(dW, dH)
' UPPER (light) shadow
For k = 0 To d
Dim kW As Float = k*dW/d
Dim kH As Float = k*dH/d
PX.Initialize(PathPoints(0,0)+kW, PathPoints(0,1)+kH)
For c=1 To 6
PX.LineTo(PathPoints(c,0)+kW, PathPoints(c,1)+kH)
Next
Dim pColor As Int = FindSolidColorBetween(mainColor, lightColor, 1.0*k*k*k/(d*d*d))
CVX.DrawPath(PX, pColor, True, 0)
Next
' LOWER (dark) shadow
For k = 0 To d
Dim kW As Float = k*dW/d
Dim kH As Float = k*dH/d
PX.Initialize(PathPoints(0,0)+2*dW-kW, PathPoints(0,1)+2*dH-kH)
For c=1 To 6
PX.LineTo(PathPoints(c,0)+2*dW-kW, PathPoints(c,1)+2*dH-kH)
Next
Dim pColor As Int = FindSolidColorBetween(mainColor, darkColor, 1.0*k*k*k/(d*d*d))
CVX.DrawPath(PX, pColor, True, 0)
Next
' Draw the 'plain' area with the main color.
PX.Initialize(PathPoints(0,0)+dW, PathPoints(0,1)+dH)
For k=0 To 6
PX.LineTo(PathPoints(k,0)+dW, PathPoints(k,1)+dH)
Next
CVX.DrawPath(PX, mainColor, True, 0)
End Sub
' progress=0 --> we get colorA
' progress=1 -> we get colorB
Sub FindSolidColorBetween(colorA As Int, colorB As Int, progress As Float) As Int
Dim weight As Int = 256*(1-Max(0, Min(progress, 1)))
Log("val:"&weight)
Dim finalRed As Int = Bit.ShiftRight(weight*Bit.And(Bit.ShiftRight(colorA,16),0xFF) + (256-weight)*Bit.And(Bit.ShiftRight(colorB,16),0xFF),8)
Dim finalGreen As Int = Bit.ShiftRight(weight*Bit.And(Bit.ShiftRight(colorA, 8),0xFF) + (256-weight)*Bit.And(Bit.ShiftRight(colorB, 8),0xFF),8)
Dim finalBlue As Int = Bit.ShiftRight(weight*Bit.And(Bit.ShiftRight(colorA, 0),0xFF) + (256-weight)*Bit.And(Bit.ShiftRight(colorB, 0),0xFF),8)
Return Colors.RGB(finalRed, finalGreen, finalBlue)
End Sub