安利一下这个 GitHub 上别人汉化好的 Google Android Training
地址是 http://hukai.me/android-training-course-in-chinese/index.html
这个 GitHub 项目地址是 https://github.com/kesenhoo/android-training-course-in-chinese
----------------------------------------------上面是广告---------------------------------------------------------------------------------------------------------------------------------------------------------------------
在android官网网站上的 Develop > Training > Displaying Bitmaps Efficiently > Loading Large Bitmaps Efficiently 章节看到的计算
BitmapFactory.Options.inSampleSize的算法,没看懂这官方示例代码的计算逻辑(好吧,这其实怪我数学不好~~~ - -b)
然后根据这个值的要求,我写了一段更符合逻辑更容易看懂的代码~~ duang~duang~duang~ 如下所示(至于这个值怎么计算的,有什么用,如果你不造的话,可以去了解下,据说这部分内容在android开发过程中还挺重要的、挺基础的~~据说。。。):
相对应的,官方的计算方法是:// 计算缩放比 // Calculate the largest inSampleSize value that is a power of 2 and keeps both // height and width larger than the requested height and width. int n = 0; int inSampleSize = 1; long startTime = System.nanoTime(); while (imageHeight / Math.pow(2, n) > screenHeight && imageWidth / Math.pow(2, n) > screenWidth) { n++; } inSampleSize = (int) Math.pow(2, n); long endTime = System.nanoTime(); Log.i("----scs", "计算 inSampleSize 耗时 " + (endTime - startTime) + " ns."); Log.i("----scs", "inSampleSize: " + inSampleSize);
long startTime1 = System.nanoTime(); if (imageHeight > screenHeight || imageWidth > screenWidth) { final int halfHeight = imageHeight / 2; final int halfWidth = imageWidth / 2; // Calculate the largest inSampleSize value that is a power of 2 and keeps both // height and width larger than the requested height and width. while ((halfHeight / inSampleSize) > screenHeight && (halfWidth / inSampleSize) > screenWidth) { inSampleSize *= 2; } } long endTime1 = System.nanoTime(); Log.i("----scs", "计算 inSampleSize 耗时 " + (endTime1 - startTime1) + " ns."); Log.i("----scs", "inSampleSize: " + inSampleSize);
然后,对比了一下,耗时如下:
06-24 09:45:57.745 8801-8801/com.jumple.scs.myapp1.app I/----scs﹕ imageWidth:1920, imageHeight: 1200 06-24 09:45:57.746 8801-8801/com.jumple.scs.myapp1.app I/----scs﹕ screenWidth:800, screenHeight: 480 06-24 09:45:57.747 8801-8801/com.jumple.scs.myapp1.app I/----scs﹕ 计算 inSampleSize 耗时 211846 ns. 06-24 09:45:57.747 8801-8801/com.jumple.scs.myapp1.app I/----scs﹕ inSampleSize: 4 06-24 09:45:57.747 8801-8801/com.jumple.scs.myapp1.app I/----scs﹕ 计算 inSampleSize 耗时 3077 ns. 06-24 09:45:57.748 8801-8801/com.jumple.scs.myapp1.app I/----scs﹕ inSampleSize: 4
翻转了一下屏幕: 06-24 09:46:31.276 8801-8801/com.jumple.scs.myapp1.app I/----scs﹕ imageWidth:1920, imageHeight: 1200 06-24 09:46:31.278 8801-8801/com.jumple.scs.myapp1.app I/----scs﹕ screenWidth:480, screenHeight: 800 06-24 09:46:31.278 8801-8801/com.jumple.scs.myapp1.app I/----scs﹕ 计算 inSampleSize 耗时 12846 ns. 06-24 09:46:31.278 8801-8801/com.jumple.scs.myapp1.app I/----scs﹕ inSampleSize: 2 06-24 09:46:31.278 8801-8801/com.jumple.scs.myapp1.app I/----scs﹕ 计算 inSampleSize 耗时 3385 ns. 06-24 09:46:31.278 8801-8801/com.jumple.scs.myapp1.app I/----scs﹕ inSampleSize: 2
果然第一种写法的耗时比较长(估计是因为第一种计算过程中使用的是double 而官方的方法只使用了int值来计算吧),哔哔了这么多,其实只是想说......我需要去补习数学了......