注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

沉默蜂B4A安卓编程

国内资料最全的B4A资料库

 
 
 

日志

 
 

[注解]给控件加边框的一个例程  

2014-08-08 09:52:53|  分类: 界面美化 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |





给视图加边框 - 沉默蜂 - 沉默蜂B4A安卓编程实战资料
运行效果图


#Region 代码说明

'版本:2014-08-08
'注解:沉默蜂(QQ793136625)
'博客:http://silentbees.blog.163.com (B4A技术博客)


#End Region

#Region  Project Attributes 
#ApplicationLabel: B4A Example
#VersionCode: 1
#VersionName: 
'SupportedOrientations possible values: unspecified, landscape or portrait.
#SupportedOrientations: unspecified
#CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes 
#FullScreen: False
#IncludeTitle: True
#End Region

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 Label1 As Label
Dim Label2 As Label
Dim Panel1 As Panel
Dim ProgressBar1 As ProgressBar
Dim EditText1 As EditText
Dim CheckBox1 As CheckBox
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("1")
' SetBorderDrawable(Label1,Colors.Cyan,Colors.White,30,30,2Dip,2Dip)
SetBorderDrawable(Label1,Colors.red,Colors.Blue,30,30,2Dip,0Dip)
SetBorderDrawable(Label2,Colors.White,Colors.DarkGray,30,30,4Dip,4Dip)
SetBorderDrawable(Panel1,Colors.Blue,Colors.Cyan,30,30,2Dip,2dip)
SetBorderDrawable(CheckBox1,Colors.Yellow,Colors.Cyan,30,30,2Dip,2dip)
SetBorderDrawable(ProgressBar1,Colors.Green,Colors.red,30,30,2Dip,2Dip)
SetBorderDrawable(EditText1,Colors.Magenta,Colors.Green,30,30,2Dip,2Dip)
' SetCBDrawable(CheckBox1,Colors.DarkGray,2Dip,Colors.Blue,Chr(8730),Colors.LightGray,30Dip,3dip)
    SetCBDrawable(CheckBox1,Colors.red,2Dip,Colors.Yellow,Colors.White,Colors.Black,30Dip,3dip)
'CheckBox1.Enabled = False

ProgressBar1.Progress = 50
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub


'添加一个边框到一个视图。
'V             将给哪个控件加边框
'FillColor 控件内部填充色
'Stroke    控件边框颜色
'rx和ry     如果这两个值为0,说明控件没有圆角化;若需要圆角化输入相应的值即可,但两个值要一样
'SWidth   边框宽度,值为0表示没有边框
'Padding  控件内部的文本与边框的距离,如标签控件内部的文本与边框的距离
'
'Add a border to a view. Adds Fillcolor, StrokeColor(Border color), rx,ry (rounded corners) SWidth(Border width)
'and padding which is only applied to Labels
Sub SetBorderDrawable (V As View, FillColor As Int, StrokeColor As Int, rx As Float, ry As Float, SWidth As Float, Padding As Int)

Dim BM As Bitmap
Dim BMD As BitmapDrawable
Dim Cnv As Canvas
'We need to draw on the bitmap with a canvas
BM.InitializeMutable(V.Width,V.Height)
Cnv.Initialize2(BM)
'Sizing Variables need to be cast to floats to enable the constructor to be found.
Dim l,t,r,b As Float
l = 0
t = 0
r = V.Width
b = V.Height
'Needs a RectF object not just a Rect
Dim rect1 As JavaObject
rect1.InitializeNewInstance("android.graphics.RectF",Array As Object(l,t,r,b))
'Create a Paint.Style object
Dim PS As JavaObject
PS.InitializeStatic("android.graphics.Paint.Style")
'Create and setup a Paint object
Dim Paint,Paint1 As JavaObject
Paint.InitializeNewInstance("android.graphics.Paint",Null)
Paint.RunMethod("setStrokeWidth",Array As Object(SWidth))
Paint.RunMethod("setColor",Array As Object(StrokeColor))
Paint.RunMethod("setStyle",Array As Object(PS.GetField("STROKE")))
Paint.RunMethod("setAntiAlias",Array As Object(True))
Paint1.InitializeNewInstance("android.graphics.Paint",Null)
Paint1.RunMethod("setColor",Array As Object(FillColor))
Paint1.RunMethod("setAntiAlias",Array As Object(True))
Paint1.RunMethod("setStyle",Array As Object(PS.GetField("FILL")))
'Get the canvas object from the wrapper
Dim CnvJO As JavaObject = Cnv
CnvJO = CnvJO.GetField("canvas")
'Draw the filled round rect on the bitmap using the canvas
CnvJO.RunMethod("drawRoundRect",Array As Object(rect1,rx,ry,Paint1))
'Draw the border round rect on the bitmap using the canvas
CnvJO.RunMethod("drawRoundRect",Array As Object(rect1,rx,ry,Paint))
'Cast the Bitmap to a BitmapDrawable
BMD.Initialize(BM)
If Padding <> -1 AND GetType(V) = "android.widget.TextView" Then
Dim VJO As JavaObject = V
VJO.RunMethod("setPadding",Array As Object(Padding,Padding,Padding,Padding))
End If
'Add it to the view
V.Background = BMD
End Sub

'改变一个复选框控件的大小和颜色。
'CB             要加边框的检查框控件
'BoxColor  检查框控件左边的正方形方框的颜色
'BoxWidth 检查框控件左边的正方形方框线条的粗度
'TickColor  控件内部的填充色,也就是控件内部区域的背景色
'TickChar   加框颜色
'DisabledColor 禁用时的颜色
'Size           检查框控件左边的正方形方框大小
'Padding     文本离边框的距离
'
'Change the size and color of a Checkbox graphic. Set the tick character and color, as well as the box size and color 
'and padding (distance from the box to the edge of the graphic) and a disabled fill color
'Pass "Fill" as the TickChar to fill the box with TickColor when selected.
Sub SetCBDrawable(CB As CheckBox,BoxColor As Int,BoxWidth As Int,TickColor As Int,TickChar As String,DisabledColor As Int,Size As Int,Padding As Int)
Dim SLD As StateListDrawable
SLD.Initialize
Dim BMEnabled,BMChecked,BMDisabled As Bitmap
BMEnabled.InitializeMutable(Size,Size)
BMChecked.InitializeMutable(Size,Size)
BMDisabled.InitializeMutable(Size,Size)
'Draw Enabled State
Dim CNV As Canvas
CNV.Initialize2(BMEnabled)
Dim Rect1 As Rect
Rect1.Initialize(Padding ,Padding ,Size - Padding ,Size - Padding)
CNV.DrawRect(Rect1,BoxColor,False,BoxWidth)
Dim Enabled,Checked,Disabled As BitmapDrawable
Enabled.Initialize(BMEnabled)
'Draw Selected state
Dim CNV1 As Canvas
CNV1.Initialize2(BMChecked)
If TickChar = "Fill" Then 
CNV1.DrawRect(Rect1,TickColor,True,BoxWidth)
CNV1.DrawRect(Rect1,BoxColor,False,BoxWidth)
Else
CNV1.DrawRect(Rect1,BoxColor,False,BoxWidth)
'Start small and find the largest font that allows the tick to fit in the box
Dim FontSize As Int = 6
Do While CNV.MeasureStringHeight(TickChar,Typeface.DEFAULT,FontSize) < Size - (BoxWidth * 2) - (Padding * 2)
FontSize = FontSize + 1
Loop
FontSize = FontSize - 1
'Draw the TickChar centered in the box
CNV1.DrawText(TickChar,Size/2,(Size + CNV.MeasureStringHeight(TickChar,Typeface.DEFAULT,FontSize))/2,Typeface.DEFAULT,FontSize,TickColor,"CENTER")
End If
Checked.Initialize(BMChecked)
'Draw disabled State
Dim CNV2 As Canvas
CNV2.Initialize2(BMDisabled)
CNV2.DrawRect(Rect1,DisabledColor,True,BoxWidth)
CNV2.DrawRect(Rect1,BoxColor,False,BoxWidth)
Disabled.Initialize(BMDisabled)
'Add to the StateList Drawable
SLD.AddState(SLD.State_Disabled,Disabled)
SLD.AddState(SLD.State_Checked,Checked)
SLD.AddState(SLD.State_Enabled,Enabled)
SLD.AddCatchAllState(Enabled)
'Add SLD to the Checkbox
Dim JO As JavaObject = CB
JO.RunMethod("setButtonDrawable",Array As Object(SLD))
End Sub

 

  评论这张
 
阅读(335)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017