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

沉默蜂B4A安卓编程

国内资料最全的B4A资料库

 
 
 

日志

 
 

生成图片缩略图  

2016-06-23 20:54:04|  分类: 图形图像 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
'创建位图为缩略图
Sub CreateScaledBitmap(Original As Bitmap, Width As Int, Height As Int) As Bitmap
    Dim r As Reflector
    Dim b As Bitmap
   Dim filter As Boolean: filter=True
   
    b = r.RunStaticMethod("android.graphics.Bitmap", "createScaledBitmap", _
        Array As Object(Original, Width, Height, filter), _
        Array As String("android.graphics.Bitmap", "java.lang.int", "java.lang.int", "java.lang.boolean"))
    Return b
   
End Sub



---------------------------------------------------------------------------------------

Releasing VBBitmap 1.3 beta

  • SmartBitmap
    Permissions:
    • android.permission.WRITE_EXTERNAL_STORAGE
    Properties:
    • GlobalAllocSize As Long
      returns getGlobalAllocSize;
    • GlobalFreedSize As Long
      returns getGlobalFreedSize;
    Methods:
    • FreeMem As Double
      returns free memory available from heap
      double memoryAvailable = Runtime.getRuntime().maxMemory() - (heapSize - heapRemaining) - nativeUsage;
    • FreeMem2 As Long
      returns freeMemory
      long freeMemory = (Runtime.getRuntime().maxMemory()) - (Debug.getNativeHeapAllocatedSize());
    • Garbage
      Runs the Garbage Collector to free memory
      Make sure your code is good first you are not initializing bitmaps aggressively
      No amount of System.gc() calls will help a memory leak 
      You should split up the work and cache your data to file(s) and remove objects accordingly. 
      If you really need more memory you should run your java app with larger heap memory set
    • HowBig (Directory As String, Filename As StringAs String
      HowBig(String Directory, String Filename)
      returns height/width as one combined string variable
    • MaxMem As Double
      returns MaxMemory
      Runtime.getRuntime().maxMemory()
    • heapRemaining As Double
      returns heapRemaining 
      Runtime.getRuntime().freeMemory();
    • heapSize As Double
      returns totalMemory
      Runtime.getRuntime().totalMemory();
    • loadbitmap (Directory As String, Filename As String, reqWidth As Integer, reqHeight As IntegerAs Bitmap
      returns a simple bitmap
      dont use file.dirassets ever, copy image over to another place from project
      example: Activity.loadbitmap(mm.GetSafeBitmap(File.DirInternal,"/moon.jpg",Activity.height,Activity.width))
      If this runs into memory problems, it's going to load a really low res image which will look bad
      but it might not crash the app
    • loadbitmap2 (Directory As String, Filename As String, SampleSize As Integer, RGB565 As BooleanAs Bitmap
      loadbitmap2(String Directory, String Filename, Integer SampleSize, Boolean RGB565)
      returns a safe sized bitmap
      dont use file.dirassets ever, copy image over to another place from project
      example: Activity.SetBackgroundImage(mm.loadbitmap2(File.DirInternal,"/moon.jpg",8))
      SampleSize, can be 2, 4, ,6, 8,....16, 32...etc
      RGB565=True will set options.inPreferredConfig = Config.RGB_565;
    • nativeUsage As Double
      returns free memory available from heap
      Debug.getNativeHeapAllocatedSize();

BitmapLib.HowBig - returns the height/width of an image 
Msgbox(BitmapLib.HowBig(File.DirInternal,"/moon3.jpg"),"SIZE") 



BitmapLib.loadbitmap2 This allows you to pass a SampleSize
Also lets you choose loading the bitmap as 16bit RGB565 which also could save some memory

you can do the math yourself to find the best SampleSize and read up here: Loading Large Bitmaps Efficiently | Android Developers

loadbitmap2(String Directory, String Filename, Integer SampleSize, Boolean RGB565)

*Setting this Boolean RGB565 flag to true, acts like a converter to convert the 24bit image file into a 16bit


BitmapLib.loadbitmap Will act like LoadBitmapSample, but if it runs into an out of memory it will automatically load a really low res image so your app won't crash and you get the bad app review on Google Play Market from one of the 1500+ devices out there. ( You never know what a device has running and how much memory is available when your app is loaded. )

loadbitmap(String Directory, String Filename, Integer Width, Integer Height) 


BitmapLib.Garbage - Runs the Garbage Collection



Information about Memory


BitmapLib.FreeMem - returns free memory available from heap double memoryAvailable = Runtime.getRuntime().maxMemory() - (heapSize - heapRemaining) - nativeUsage

BitmapLib.FreeMem2 - returns freeMemory long freeMemory = (Runtime.getRuntime().maxMemory()) - (Debug.getNativeHeapAllocatedSize());

BitmapLib.heapRemaining - returns heapRemaining Runtime.getRuntime().freeMemory();

BitmapLib.heapSize - returns totalMemory Runtime.getRuntime().totalMemory();

BitmapLib.MaxMem - returns MaxMemory Runtime.getRuntime().maxMemory()

BitmapLib.nativeUsage - returns free memory available from heap Debug.getNativeHeapAllocatedSize();


BitmapLib.GlobalAllocSize
BitmapLib.GlobalFreedSize







Example usage: 
Code:
Dim BitmapLib As SmartBitmap
File.Copy(File.DirAssets,"moon3.jpg",File.DirInternal,"moon3.jpg")
        
Activity.SetBackgroundImage(BitmapLib.loadbitmap2(File.DirInternal,"/moon3.jpg",1,True))
      
'Activity.SetBackgroundImage(BitmapLib.loadbitmap(File.DirInternal,"/moon3.jpg",Activity.Width,Activity.Height))
Msgbox(BitmapLib.HowBig(File.DirInternal,"/moon3.jpg"),"SIZE")
Msgbox(BitmapLib.heapRemaining,"HeapRemaining")
Please test with some big images/bitmaps.


Thanks in Advance

Vb1992

Library Revision History:

Old...... Library is version 1.0 - June 29th, 2012
Old...... Library is version 1.1 - June 29th, 2012
Old...... Library is version 1.2 - July 2nd, 2012
New...... Library is version 1.3 - July 2nd, 2012
improved algorithmic values for loadbitmap
added Log( GlobalAllocSize)
added Log( GlobalFreedSize)
  评论这张
 
阅读(83)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

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

页脚

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