Creare una CustomView Class
- Codice:
'Custom View class
#Event: ChangeValue (Value As Int)
'#DesignerProperty: Key: BooleanExample, DisplayName: Boolean Example, FieldType: Boolean, DefaultValue: True, Description: Example of a boolean property.
#DesignerProperty: Key: MinValue, DisplayName: MinValue, FieldType: Int, DefaultValue: 0, MinRange: 0, Description: Note that MinRange and MaxRange are optional.
#DesignerProperty: Key: MaxValue, DisplayName: MaxValue, FieldType: Int, DefaultValue: 100, MinRange: 0, Description: Note that MinRange and MaxRange are optional.
#DesignerProperty: Key: Value, DisplayName: Value, FieldType: Int, DefaultValue: 0, MinRange: 0, Description: Actual value
'#DesignerProperty: Key: StringWithListExample, DisplayName: String With List, FieldType: String, DefaultValue: Sunday, List: Sunday|Monday|Tuesday|Wednesday|Thursday|Friday|Saturday
'#DesignerProperty: Key: StringExample, DisplayName: String Example, FieldType: String, DefaultValue: Text
#DesignerProperty: Key: Color, DisplayName: Color Example, FieldType: Color, DefaultValue: 0xFFCFDCDC, Description: You can use the built-in color picker to find the color values.
'#DesignerProperty: Key: DefaultColorExample, DisplayName: Default Color Example, FieldType: Color, DefaultValue: Null, Description: Setting the default value to Null means that a nullable field will be displayed.
Sub Class_Globals
Private mEventName As String 'ignore
Private mCallBack As Object 'ignore
Private mBase As Panel
'Private Const DefaultColorConstant As Int = -984833 'ignore
Private ColorSlide As Int
Private MinValue,MaxValue As Int = 0
Private Value As Int = 0
Private Pan As Panel
Private LabMin,LabMax As Label
Private Ball As Label
End Sub
Public Sub Initialize (Callback As Object, EventName As String)
mEventName = EventName
mCallBack = Callback
Pan.Initialize("Panel")
LabMin.Initialize("")
LabMax.Initialize("")
Ball.Initialize("")
End Sub
Public Sub DesignerCreateView (Base As Panel, Lbl As Label, Props As Map)
mBase = Base
If Props.ContainsKey("Color") Then ColorSlide=Props.Get("Color")
If Props.ContainsKey("MinValue") Then MinValue=Props.Get("MinValue")
If Props.ContainsKey("MaxValue") Then MaxValue=Props.Get("MaxValue")
If Props.ContainsKey("Value") Then Value=Props.Get("Value")
mBase.color=Colors.Transparent
Pan.Color=ColorSlide
LabMin.Color=Colors.Transparent
LabMin.TextColor=Colors.White
LabMin.Gravity=Gravity.CENTER
LabMin.Text=MinValue
LabMax.Color=Colors.Transparent
LabMax.TextColor=Colors.White
LabMax.Gravity=Gravity.CENTER
LabMax.Text=MaxValue
Ball.TextColor=Colors.Black
Ball.SetBackgroundImage(MyCorner(ColorSlide,mBase.Height/2))
Ball.Gravity=Gravity.CENTER
Ball.Text=Value
mBase.AddView(Pan,0,mBase.Height/2,mBase.Width,mBase.Height/2)
Pan.AddView(LabMin,0,0,60dip,Pan.Height)
Pan.AddView(LabMax,Pan.Width-60dip,0,60dip,Pan.Height)
Base.AddView(Ball,0,mBase.Height/2,mBase.Height/2,mBase.Height/2)
setValue(Value,False)
End Sub
Public Sub GetBase As Panel
Return mBase
End Sub
private Sub Corner(Color As Int, Radius As Int) As ColorDrawable
Dim cdb As ColorDrawable
cdb.Initialize2(Colors.White,Radius,5dip,Color)
Return cdb
End Sub
private Sub MyCorner(Color As Int, Radius As Int) As Bitmap
Dim B As Bitmap
Dim C As Canvas
Dim R As Int = mBase.Height/2
Dim Gr As Int = mBase.Height
Dim Rect1 As Rect
Dim Spessore As Int= 10dip
B.InitializeMutable(Gr,Gr)
c.Initialize2(B)
Rect1.Initialize(R/4,R,Gr-(R/4),Gr)
'Rect1.Initialize(0,R,(R*2),Gr)
C.DrawRect(Rect1,Color,True,1)
C.DrawCircle(R/8,Gr-(R/5),R/4,Colors.Transparent,True,1)
C.DrawCircle(Gr-(R/8),Gr-(R/5),R/4,Colors.Transparent,True,1)
C.DrawCircle(R,R,R,Colors.White,True,Spessore)
C.DrawCircle(R,R,R-(Spessore/2),ColorSlide,False,Spessore)
Return C.Bitmap
End Sub
public Sub setValue(pos As Int,Click As Boolean)
If pos>=MinValue And pos<=MaxValue Then
Value=pos
Ball.Left=((Value-MinValue)/(MaxValue-MinValue))*(mBase.Width-(mBase.Height/2))
Ball.Text=pos
If Click Then
Ball.SetLayoutAnimated(200,Ball.Left,0,mBase.Height/2,mBase.Height/2)
End If
End If
End Sub
Public Sub getValue As Int
Return Value
End Sub
Private Sub Panel_Touch (Action As Int, X As Float, Y As Float)
Select Action
Case 0 ' ACTION_DOWN
Dim P As Int = ((X/mBase.Width)*(MaxValue-MinValue))+MinValue
setValue(P,True)
If SubExists(mCallBack,mEventName & "_ChangeValue") Then CallSub2(mCallBack,mEventName & "_ChangeValue",p)
Case 1 ' ACTION_UP
Ball.SetLayoutAnimated(200,Ball.Left,mBase.Height/2,mBase.Height/2,mBase.Height/2)
Case 2 ' ACTION_MOVE
Dim P As Int = ((X/mBase.Width)*(MaxValue-MinValue))+MinValue
setValue(P,True)
If SubExists(mCallBack,mEventName & "_ChangeValue") Then CallSub2(mCallBack,mEventName & "_ChangeValue",p)
Case 3 ' Error
End Select
End Sub