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

沉默蜂B4A安卓编程

国内资料最全的B4A资料库

 
 
 

日志

 
 

[图形]截图指定区域的手机屏幕到图片文件  

2016-09-18 21:20:52|  分类: 图形图像 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

[图形]截图指定区域的手机屏幕 - 沉默蜂 - 沉默蜂B4A安卓编程
 布局文件

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

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

Sub Process_Globals
Dim t1 As Timer 
End Sub

Sub Globals
Private Button2 As Button
Private img1 As ImageView
Private pnl1 As Panel
End Sub

Sub Activity_Create(FirstTime As Boolean)
Activity.LoadLayout("main")
t1.Initialize("t1",3000)
t1.Enabled = True
drawsomething
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub drawsomething
   Dim Obj1 As Reflector
   Dim Canv As Canvas
   Canv.Initialize(pnl1) ' initialize on what you want
   Obj1.Target = Canv
   Obj1.Target = Obj1.GetField("paint")
   Obj1.RunMethod2("setAntiAlias", True, "java.lang.boolean")

Dim rect1 As Rect 
rect1.Initialize(0,0,50%x,50%y)
Dim nr As Int = Rnd(0,3) 
If nr = 0 Then Canv.DrawCircle(50dip, 50dip,  25dip, Colors.RGB(Rnd(0,255),Rnd(0,255),Rnd(0,255)), True, 2dip)
If nr = 1 Then Canv.DrawRect(rect1, Colors.RGB(Rnd(0,255),Rnd(0,255),Rnd(0,255)),  True, 5dip)
If nr = 2 Then Canv.DrawText("Panel Capture", 100dip,  100dip, Typeface.DEFAULT,24, Colors.RGB(Rnd(0,255),Rnd(0,255),Rnd(0,255)),"LEFT")

End Sub

Sub t1_tick
drawsomething
End Sub

Sub Button2_Click
'capture panel to imageview
      PanelCapture(pnl1,img1)
End Sub

'Capture Panel to imageview
'将控件pnl 上的所有内容保存到Img2上显示
Sub PanelCapture(pnl As Panel, Img2 As ImageView)

Dim Obj1, Obj2 As Reflector
Dim bmp As Bitmap
Dim c As Canvas
Obj1.Target = Obj1.GetActivityBA
Obj1.Target = Obj1.GetField("vg")
bmp.InitializeMutable(pnl.left + pnl.Width, pnl.Top + pnl.Height) 
c.Initialize2(bmp)
Dim args(1) As Object
Dim types(1) As String
Obj2.Target = c
Obj2.Target = Obj2.GetField("canvas")
args(0) = Obj2.Target
types(0) = "android.graphics.Canvas"
Obj1.RunMethod4("draw", args, types)

'draw from image to canavas
Dim canvas1 As Canvas
canvas1.Initialize(Img2)
Dim scrt As Rect
scrt.Initialize(pnl.left, pnl.top, pnl.left + pnl.Width, pnl.Top + pnl.Height)
Dim rectPanel1 As Rect
rectPanel1.Initialize(0, 0,Img2.Width, Img2.Height)
canvas1.DrawBitmap(bmp, scrt , rectPanel1)
Img2.Invalidate


'用年月日时分秒+随机数来命名一个文件的主名
Dim  vFineMain As String 
vFineMain=DateTime.GetYear(DateTime.Now)   &DateTime.GetMonth(DateTime.Now)&DateTime.GetDayOfMonth(DateTime.Now) 
vFineMain=vFineMain & "_" & DateTime.GetHour(DateTime.Now)&DateTime.GetMinute(DateTime.Now)&DateTime.GetSecond(DateTime.Now)
Dim Out As OutputStream  
' 经测试不保是png还是jpg图片,分享给微信或QQ,发到对方微信或QQ后,显示的图片质量差不多
' 并且png图片比jpg图片文件要小100kb左右
'保存图片为png
Out = File.OpenOutput(picDir, vFineMain & ".png", False)

canvas1.Bitmap.WriteToStream(Out,  80, "PNG")  
Out.Close
Return (vFineMain & ".png")

End Sub

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

历史上的今天

在LOFTER的更多文章

评论

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

页脚

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