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

沉默蜂B4A安卓编程

国内资料最全的B4A资料库

 
 
 

日志

 
 

[库][工具栏]AhaActionBar (工具栏, 导航栏 + 菜单) (三)  

2014-02-17 19:02:15|  分类: 菜单控件 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
下面是ahaActionBar_Source这个例程中的ahaMenu.bas这个菜单类的注解

#Region 代码说明

'版本:2014-02-16
'注解:沉默蜂(QQ793136625)
'博客:http://silentbees.blog.163.com
'说明:这是一个关于动作条、导航菜单、菜单的例程。
'这个例程中创建了两个类模块,一个是ahaActionBar,另一个是ahaMenu

'来源

#End Region


'定义菜单的几个事件
#Event: Click (ReturnValue as Object)
#Event: LongClick (ReturnValue as Object) 
#Event: Menu_Open
#Event: Menu_Closed

Sub Class_Globals

'接下来的变量都是菜单类对象的属性,可以改变这些参数来改变菜单样式
'菜单项目文本字体大小
Private mTextSize As Int = 18
'dark 黑色
Private mDark As Boolean = False
'菜单文本颜色
Private mMenuTextColor As Int = Colors.Black 
'动画菜单持续时间
Private mMenuAnimationDuration As Int = 300
'菜单的背景色为透明色
Private mMenuItemBackColor As Int = Colors.Transparent 
'菜单尺寸缩放
Private mAutoScaleRate As Float = 0
'默认使用菜单图标
Private mUseSymbols As Boolean = True
'菜单项文本的字体名称
Private mTypeFace As Typeface = Typeface.DEFAULT

'缩放比例
Private mScale As Float = 1
'当前在哪个窗体中显示菜单 
Private mActivity As Activity
'菜单类型
Private mModule As Object
'子工具条名称
Private mSubActionBar As String 
'菜单左边、顶边位置
Private mLeft, mTop As Int 
Private inMenuAnimation,outMenuAnimation As Animation
'pnlTransparent是一个透明面板,当工具条上最左边弹出导航菜单时会有一个透明的区域作垫底
Private pnlMenu,pnlTransparent  As Panel
'http://silentbees.blog.163.com/blog/static/22878903120140274322264/
Private lsvMenu As ListView 

End Sub

'初始化菜单
'Activity: Activity, Module=Me, Eventname (e.g. Menu), X,Y Position where the menu should open
Public Sub Initialize(Activity As Activity, Module As Object, EventName As String, X As Int, Y As Int )

mActivity = Activity
mModule = Module
mSubActionBar = EventName
mLeft = X
mTop = Y
pnlMenu.Initialize("")
pnlMenu.Visible = False
pnlTransparent.Initialize("CatchClick")
pnlTransparent.Visible = False
mActivity.AddView (pnlTransparent,0dip,0dip,100%x,100%y)
If mLeft +200dip >= 100%x Then mLeft = (100%x - 200dip) +5dip
If mTop +72dip >= 100%y Then mTop = 100%y - 72dip
mActivity.AddView(pnlMenu,mLeft,mTop,200dip,72dip)
'mActivity.AddView(pnlMenu,pnlActionBar.Width -200dip,mHeight-16dip,200dip * mScale,200dip)
lsvMenu.Initialize ("Menu")
lsvMenu.TwoLinesAndBitmap.SecondLabel.Visible = False
lsvMenu.TwoLinesAndBitmap.ItemHeight = 48dip
lsvMenu.TwoLinesAndBitmap.Label.Gravity = Gravity.CENTER_VERTICAL
lsvMenu.TwoLinesAndBitmap.Label.Height = lsvMenu.TwoLinesAndBitmap.ItemHeight
lsvMenu.TwoLinesAndBitmap.Label.Top = 0
lsvMenu.TwoLinesAndBitmap.ImageView.SetLayout(13dip, 13dip, 24dip, 24dip)
lsvMenu.TwoLinesAndBitmap.Label.TextSize = mTextSize  * mScale
lsvMenu.TwoLinesAndBitmap.Label.TextColor = mMenuTextColor

lsvMenu.SingleLineLayout.ItemHeight = 48dip
lsvMenu.SingleLineLayout.Label.Gravity = Gravity.CENTER_VERTICAL
lsvMenu.SingleLineLayout.Label.Height = lsvMenu.SingleLineLayout.ItemHeight
lsvMenu.SingleLineLayout.Label.Top = 0
lsvMenu.SingleLineLayout.Label.TextSize = mTextSize  * mScale
lsvMenu.SingleLineLayout.Label.TextColor = mMenuTextColor

pnlMenu.AddView(lsvMenu,12dip,12dip,pnlMenu.Width-12dip,pnlMenu.Height-12dip)
inMenuAnimation.InitializeAlpha("",0,1)
outMenuAnimation.InitializeAlpha("",1,0)
inMenuAnimation.Duration = mMenuAnimationDuration
outMenuAnimation.Duration = mMenuAnimationDuration

End Sub

#Region 设置菜单参数

'获取菜单的背景色
Public Sub getMenuItemBackColor As Int
Return mMenuItemBackColor
End Sub

'设置菜单的背景色
Public Sub setMenuItemBackColor (MenuItemBackColor As Int)
mMenuItemBackColor = MenuItemBackColor
lsvMenu.Color = mMenuItemBackColor
End Sub


'获取当前菜单是否为可视状态
Public Sub isVisible As Boolean
Return pnlMenu.Visible 
End Sub


'设置你想显示的符号集
'<B>Has to be set bevore Items are added </B>
Public Sub setUseSymbols (UseSymbols As Boolean)
mUseSymbols =UseSymbols
End Sub

'获取显示的符号集
Public Sub getUseSymbols As Boolean 
Return mUseSymbols
End Sub

'Sets the Scale. Use the same ratio as in your DesignerScript (AutoScaleRate)
'<B>Has to be called before Items are added</B>
'Use 0 to disable
Public Sub getAutoScaleRate As Int 
Return mAutoScaleRate
End Sub


Public Sub setAutoScaleRate (AutoScaleRate As Int)
mAutoScaleRate = AutoScaleRate 
Dim delta As Float 
delta = ((100%x + 100%y) / (320dip + 430dip) - 1)
mScale = 1 + mAutoScaleRate * delta

lsvMenu.TwoLinesAndBitmap.Label.TextSize = mTextSize  * mScale
lsvMenu.SingleLineLayout.Label.TextSize = mTextSize  * mScale
End Sub
'获取菜单项目中文本的尺寸大小
Public Sub getTextSize As Int 
Return mTextSize
End Sub

'设置菜单项目中文本的尺寸大小
Public Sub setTextSize (TextSize As Int)
mTextSize = TextSize 
lsvMenu.TwoLinesAndBitmap.Label.TextSize = mTextSize  * mScale
lsvMenu.SingleLineLayout.Label.TextSize = mTextSize  * mScale
End Sub


'Loads different drawables for the Dark-Theme
'<B>Has to be called before you set custom colors and add items!</B>
Public Sub getDarkTheme As Boolean
Return mDark
End Sub

'设置菜单是否采用黑色的底,白色的文字
Public Sub setDarkTheme (DarkTheme As Boolean)
mDark = DarkTheme
If mDark = True Then
SetNinePatchDrawable(pnlMenu,"ahaab_menu_dark")
setTextColor(Colors.White)
Else
SetNinePatchDrawable(pnlMenu,"ahaab_menu")
setTextColor(Colors.Black ) 
End If
End Sub


'获取菜单动画的持续时间,以毫秒为单位
Public Sub getMenuAnimationDuration As Int 
Return mMenuAnimationDuration
End Sub

'菜单显示的时候可以控制它是否慢慢由透明变不透明,隐藏时由不透明变透明
'setMenuAnimationDuration就是用来控制这种透明与不透明变化持续的时间(单位毫秒)
'创建一个菜单后,默认只有0.1秒
Public Sub setMenuAnimationDuration (MenuAnimationDuration As Int)
mMenuAnimationDuration = MenuAnimationDuration
inMenuAnimation  .Duration = mMenuAnimationDuration
outMenuAnimation.Duration = mMenuAnimationDuration
End Sub

'获取菜单文本的颜色
Public Sub getTextColor As Int 
Return mMenuTextColor
End Sub

'设置菜单文本的颜色
Public Sub setTextColor (TextColor As Int)
mMenuTextColor = TextColor
lsvMenu.TwoLinesAndBitmap.Label.TextColor = mMenuTextColor
lsvMenu.SingleLineLayout.Label.TextColor = mMenuTextColor
End Sub

'返回的列表视图菜单
Public Sub MenuListView As  ListView
Return lsvMenu 
End Sub

'设置菜单项目文本的字体名称
Public Sub setTypeFace (NewTypeFace As Typeface)
mTypeFace =NewTypeFace
lsvMenu.TwoLinesAndBitmap.Label.Typeface = mTypeFace
lsvMenu.SingleLineLayout.Label.Typeface = mTypeFace
End Sub

'获取菜单项目文本的字体名称
Public Sub getTypeFace As Typeface
Return mTypeFace
End Sub

#End Region 




'添加一个菜单项。如果你不想在菜单命令中添加图标,image可以赋给NULL
Public Sub AddMenuItem(Text As String, image As Bitmap,ReturnValue As Object )
Dim AddSpacing As Int = 0
If mUseSymbols = True Then AddSpacing = 110dip
Dim C As Canvas
C.Initialize(pnlMenu)
Dim X As Int
X = C.MeasureStringWidth( Text, Typeface.DEFAULT, mTextSize) '*1dip'+5dip '* 1dip +5dip *1dip
If X > lsvMenu.Width - AddSpacing Then
Dim tmpwidth As Int = pnlMenu.Width
pnlMenu.Width = X+ AddSpacing  
pnlMenu.Left = pnlMenu.Left - (pnlMenu.Width - tmpwidth) +3dip
lsvMenu.Width = pnlMenu.Width-12dip
End If
If mUseSymbols = True Then
If Not(image.IsInitialized) Then
lsvMenu.AddTwoLinesAndBitmap2(Text, "", Null, ReturnValue)
Else
lsvMenu.AddTwoLinesAndBitmap2(Text, "", image, ReturnValue)
End If
Else
lsvMenu.AddSingleLine2(Text, ReturnValue)
End If
pnlMenu.Height = lsvMenu.Size * 49dip + 24dip
lsvMenu.Height = lsvMenu.Size * 49dip 
If pnlMenu.Height > 100%y - (pnlMenu.Top )Then 
pnlMenu.Height = 100%y - (pnlMenu.Top )
lsvMenu.Height = pnlMenu.Height -24dip
End If
End Sub


'点击菜单项目文本触发此事件
Private Sub Menu_ItemClick (Position As Int, Value As Object)
TriggerMenu
If SubExists(mModule, mSubActionBar & "_Click") Then
CallSub2(mModule, mSubActionBar & "_Click", Value)
End If
End Sub

'长按菜单项目文本触发此事件
Private Sub Menu_ItemLongClick (Position As Int, Value As Object)
TriggerMenu
If SubExists(mModule, mSubActionBar & "_LongClick") Then
CallSub2(mModule, mSubActionBar & "_LongClick", Value)
End If
End Sub


'改变菜单的显示位置
Public Sub RepostitionMenu (X As Int, Y As Int)
mLeft = X
mTop = Y

If mLeft + pnlMenu.Width  >= 100%x Then mLeft = 100%x - pnlMenu.Width
If mTop + pnlMenu.Height  >= 100%y Then mTop = 100%y - 72dip
pnlMenu.Top = mTop
pnlMenu.Left = mLeft
End Sub

'打开菜单
Public Sub OpenMenu
If mDark = True Then
SetNinePatchDrawable(pnlMenu,"ahaab_menu_dark")
Else
SetNinePatchDrawable(pnlMenu,"ahaab_menu")
End If

pnlMenu.Visible = True
pnlTransparent.Visible = True
pnlTransparent.BringToFront
pnlMenu.BringToFront 
inMenuAnimation.Start(pnlMenu)
If SubExists(mModule,mSubActionBar &  "_Menu_Open") Then CallSub(mModule, mSubActionBar & "_Menu_Open")
End Sub

'关闭菜单
Public Sub CloseMenu
pnlMenu.Visible = False
pnlTransparent.Visible = False
outMenuAnimation.Start(pnlMenu)
If SubExists(mModule,mSubActionBar &  "_Menu_Closed") Then CallSub(mModule, mSubActionBar & "_Menu_Closed")
End Sub

'打开或关闭菜单 引发此事件
Public Sub TriggerMenu
If pnlMenu.Visible = True Then
CloseMenu
Else
OpenMenu
End If
End Sub

'Filters the Back & Menu Key
'Usage:
'<code>Sub Activity_KeyPress (KeyCode As Int) As Boolean
' Return ActionBar.Keypress(KeyCode)
'End Sub</code>
'过滤菜单键
#Region HelperSubs


'处理硬件中的菜单和回退键
Public Sub Keypress(Keycode As Int)As Boolean

'当菜单弹出后,点击手机硬件中的菜单和回退键,将把菜单隐藏起来
If Keycode = KeyCodes.KEYCODE_BACK OR Keycode = KeyCodes.KEYCODE_MENU Then
If isVisible Then
CloseMenu 
Return True
Else
Return False
End If
End If
Return False
End Sub


Private Sub CatchClick_Click
TriggerMenu
End Sub


Private Sub SetNinePatchDrawable(Control As View, ImageName As String)
    Try
Dim R As Reflector
   Dim package As String
   Dim id As Int
   package = R.GetStaticField("anywheresoftware.b4a.BA", "packageName")
   id = R.GetStaticField(package & ".R$drawable", ImageName)
   R.Target = R.GetContext
   R.Target = R.RunMethod("getResources")
   Control.Background = R.RunMethod2("getDrawable", id, "java.lang.int")
Catch
Dim bg As ColorDrawable 
bg.Initialize(Colors.Red,0dip)
Control.Background = bg 
ToastMessageShow("Drawable " & ImageName & " is missing." & CRLF & "Did you copy it to the Objects/res/drawable/ folder?" & CRLF & CRLF & "Don't forget to set it to read only",False)
End Try
End Sub

#End Region 
  评论这张
 
阅读(380)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

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

页脚

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