{"id":456,"date":"2022-12-03T10:25:17","date_gmt":"2022-12-03T02:25:17","guid":{"rendered":"https:\/\/iichen.cn\/?p=456"},"modified":"2022-12-03T10:25:17","modified_gmt":"2022-12-03T02:25:17","slug":"androiod-bitmap","status":"publish","type":"post","link":"https:\/\/iichen.cn\/?p=456","title":{"rendered":"Androiod-Bitmap"},"content":{"rendered":"<h5>1. \u5360\u7528\u5927\u5c0f<\/h5>\n<blockquote>\n<ul>\n<li>scaledWidth = int( \u539f\u56fe\u5bbd\u5ea6 * (\u8bbe\u5907\u7684 dpi \/ \u76ee\u5f55\u5bf9\u5e94\u7684 dpi ) + 0.5)<\/li>\n<li>scaledHeight = int( \u539f\u56fe\u9ad8\u5ea6 * (\u8bbe\u5907\u7684 dpi \/ \u76ee\u5f55\u5bf9\u5e94\u7684 dpi ) + 0.5)<\/li>\n<li>scaledWidth * scaledHeight * 4\uff08\u5177\u4f53Bitmap \u7684\u683c\u5f0f\u5927\u5c0f\uff0c\u5982\uff1aARGB_8888\u662f4\u5b57\u8282\uff09<\/li>\n<li>0.5\u662f\u4e3a\u4e86\u7cbe\u5ea6\uff0c\u4e00\u822c\u76f4\u63a5\u8bb0\u4e3a \u539f\u56fe\u9ad8\u5ea6 * (\u8bbe\u5907\u7684 dpi \/ \u76ee\u5f55\u5bf9\u5e94\u7684 dpi * \u539f\u56fe\u9ad8\u5ea6 * (\u8bbe\u5907\u7684 dpi \/ \u76ee\u5f55\u5bf9\u5e94\u7684 dpi<\/li>\n<li>ARGB_8888\u662f4\u5b57\u8282, RGB_565\u662f2\u5b57\u8282<\/li>\n<\/ul>\n<\/blockquote>\n<table>\n<tbody>\n<tr>\n<td><strong>density<\/strong><\/td>\n<td><strong>1<\/strong><\/td>\n<td><strong>1.5<\/strong><\/td>\n<td><strong>2<\/strong><\/td>\n<td><strong>3<\/strong><\/td>\n<td><strong>3.5<\/strong><\/td>\n<td><strong>4<\/strong><\/td>\n<\/tr>\n<tr>\n<td>densityDpi<\/td>\n<td>160<\/td>\n<td>240<\/td>\n<td>320<\/td>\n<td>480<\/td>\n<td>560<\/td>\n<td>640<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><a class=\"wp-editor-md-post-content-link\" href=\"https:\/\/juejin.cn\/post\/6844903425918894088\" title=\"Bitmap\u5927\u5c0f\u7684\u6587\u7ae0\">Bitmap\u5927\u5c0f\u7684\u6587\u7ae0<\/a><\/p>\n<h5>2. \u4f18\u5316\u8c03\u8282<\/h5>\n<h6>2.1 decodeResource()\u548cdecodeFile()<\/h6>\n<ul>\n<li>decodeFile()\u7528\u4e8e\u8bfb\u53d6SD\u5361\u4e0a\u7684\u56fe\uff0c\u5f97\u5230\u7684\u662f\u56fe\u7247\u7684\u539f\u59cb\u5c3a\u5bf8<\/li>\n<li>decodeResource()\u7528\u4e8e\u8bfb\u53d6Res\u3001Raw\u7b49\u8d44\u6e90\uff0c\u5f97\u5230\u7684\u662f\u56fe\u7247\u7684\u539f\u59cb\u5c3a\u5bf8 * \u7f29\u653e\u7cfb\u6570<br \/>\n> \u7f29\u653e\u7cfb\u6570 inDensity<br \/>\n> \u5728drawable\u975e xxdpi\u7684\u76ee\u5f55 \u9ed8\u8ba4160\uff0cinTargetDensity \/ 160<br \/>\n> xxdpi\u7684\u76ee\u5f55\uff0c\u6839\u636e\u76f8\u5e94\u7684\u76ee\u5f55density  inTargetDensity \/ xxDensity<\/li>\n<\/ul>\n<pre><code class=\"language-java line-numbers\">\/\/ \u624b\u52a8\u7684\u65b9\u5f0f\nBitmapFactory.Options options = new BitmapFactory.Options();\noptions.inJustDecodeBounds = false;\noptions.inSampleSize = 1;\noptions.inDensity = 160;\noptions.inTargetDensity = 160;\nbitmap = BitmapFactory.decodeResource(getResources(),\n        R.drawable.origin, options);\n\/\/ MX4\u4e0a\uff0c\u867d\u7136density = 3\n\/\/ \u4f46\u662f\u901a\u8fc7\u8bbe\u7f6einTargetDensity \/ inDensity = 160 \/ 160 = 1\n\/\/ \u89e3\u7801\u540e\u56fe\u7247\u5927\u5c0f\u4e3a720x720\nSystem.out.println(\"w:\" + bitmap.getWidth()\n        + \", h:\" + bitmap.getHeight());\n<\/code><\/pre>\n<h6>2.2 inBitmap \u5bf9\u8c61\u7684\u590d\u7528<\/h6>\n<blockquote><p>\n  Android2.3.3 \u4ee5\u4e0b<br \/>\n  \u63a8\u8350\u4f7f\u7528 Bitmap#recycle \u65b9\u6cd5\u8fdb\u884c Bitmap \u5185\u5b58\u56de\u6536<br \/>\n  Android3.0 \u4ee5\u4e0a<br \/>\n  \u63a8\u8350\u7684\u662f Bitmap \u5185\u5b58\u590d\u7528\uff0c\u4e3a\u6b64\u5f15\u5165\u4e86\u4e00\u4e2a BitmapFactory.Options.inBitmap \u5b57\u6bb5\u6765\u8bbe\u7f6e\u6253\u7b97\u590d\u7528\u7684 Bitmap\n<\/p><\/blockquote>\n<h6>2.3 LruCache\u548cDiskLruCache<\/h6>\n<h6>2.4 inSampleSize &#8211; \u9700\u8981\u662f2\u7684\u6b21\u65b9<\/h6>\n<blockquote><p>\n  \u8bbe\u7f6eBitmapFactory.Options\u7684inJustDecodeBounds\u4e3atrue\uff0c\u8fd9\u6837\u7684Bitmap\u53ef\u4ee5\u501f\u52a9decodeFile\u65b9\u6cd5\u628a\u9ad8\u548c\u5bbd\u5b58\u653e\u5230Bitmap.Options\u4e2d\uff0c\u4f46\u662f\u5185\u5b58\u5360\u7528\u4e3a\u7a7a\uff08\u4e0d\u4f1a\u771f\u6b63\u7684\u52a0\u8f7d\u56fe\u7247\uff09\u3002\u6709\u4e86\u5177\u5907\u9ad8\u5bbd\u4fe1\u606f\u7684Options\n<\/p><\/blockquote>\n<pre><code class=\"language-java line-numbers\">    private Bitmap getRealCompressedBitmap(String pathName, int reqWidth, int reqHeight) {\n        Bitmap bitmap;\n        BitmapFactory.Options options = new BitmapFactory.Options();\n        options.inJustDecodeBounds = true;\n        BitmapFactory.decodeFile(pathName, options);\n        int width = options.outWidth \/ 2;\n        int height = options.outHeight \/ 2;\n        int inSampleSize = 1;\n\n        while (width \/ inSampleSize &gt;= reqWidth &amp;&amp; height \/ inSampleSize &gt;= reqHeight) {\n            inSampleSize = inSampleSize * 2;\n        }\n\n        options.inSampleSize = inSampleSize;\n        options.inJustDecodeBounds = false;\n        bitmap = BitmapFactory.decodeFile(pathName, options);\n        showBitmapInfos(pathName);\n        return bitmap;\n    }\n<\/code><\/pre>\n<h6>2.5 Matrix \u5927->\u5c0f<\/h6>\n<h6>2.6 BitmapRegionDecoder \u5927\u56fe\u52a0\u8f7d<\/h6>\n<h6>2.7 \u56fe\u7247\u538b\u7f29<\/h6>\n<ul>\n<li>Bitmap\u81ea\u5e26\u7684\u538b\u7f29<\/li>\n<\/ul>\n<pre><code class=\"language-java line-numbers\">Bitmap bitmap = getZoomImage(BitmapFactory.decodeFile(imagePath), 400);\n\/\/ \u538b\u7f29\u53ea\u662f\u6539\u53d8\u6587\u4ef6\u5b58\u50a8\u7684\u5927\u5c0f\uff0c\u4e0d\u4f1a\u6539\u53d8\u5185\u5b58\u5360\u7528\u5927\u5c0f\nbitmap.compress(Bitmap.CompressFormat.JPEG, 50, fos);\n<\/code><\/pre>\n<ul>\n<li>\u7cfb\u7edf\u88c1\u526a<\/li>\n<\/ul>\n<pre><code class=\"language-java line-numbers\">    private void CropTheImage(Uri imageUrl) {\n        Intent cropIntent = new Intent(\"com.android.camera.action.CROP\");\n        cropIntent.setDataAndType(imageUrl, \"image\/*\");\n        cropIntent.putExtra(\"cropWidth\", \"true\");\n        cropIntent.putExtra(\"outputX\", cropTargetWidth);\n        cropIntent.putExtra(\"outputY\", cropTargetHeight);\n        File copyFile = FileHelper.createFileByType(mContext, destType, String.valueOf(System.currentTimeMillis()));\n        copyUrl = Uri.fromFile(copyFile);\n        cropIntent.putExtra(\"output\", copyUrl);\n        startActivityForResult(cropIntent, REQUEST_CODE_CROP_PIC);\n    }\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>1. \u5360\u7528\u5927\u5c0f scaledWidth = int( \u539f\u56fe\u5bbd\u5ea6 * (\u8bbe\u5907\u7684 dpi \/ \u76ee\u5f55\u5bf9\u5e94\u7684 dpi  [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11,8],"tags":[],"class_list":["post-456","post","type-post","status-publish","format-standard","hentry","category-android","category-8"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.4 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Androiod-Bitmap - IIchen<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/iichen.cn\/?p=456\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Androiod-Bitmap - IIchen\" \/>\n<meta property=\"og:description\" content=\"1. \u5360\u7528\u5927\u5c0f scaledWidth = int( \u539f\u56fe\u5bbd\u5ea6 * (\u8bbe\u5907\u7684 dpi \/ \u76ee\u5f55\u5bf9\u5e94\u7684 dpi [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/iichen.cn\/?p=456\" \/>\n<meta property=\"og:site_name\" content=\"IIchen\" \/>\n<meta property=\"article:published_time\" content=\"2022-12-03T02:25:17+00:00\" \/>\n<meta name=\"author\" content=\"iichen\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"iichen\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/iichen.cn\/?p=456#article\",\"isPartOf\":{\"@id\":\"https:\/\/iichen.cn\/?p=456\"},\"author\":{\"name\":\"iichen\",\"@id\":\"https:\/\/iichen.cn\/#\/schema\/person\/4a47edf85ab49841df9e8f6aee40b77c\"},\"headline\":\"Androiod-Bitmap\",\"datePublished\":\"2022-12-03T02:25:17+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/iichen.cn\/?p=456\"},\"wordCount\":65,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/iichen.cn\/#\/schema\/person\/4a47edf85ab49841df9e8f6aee40b77c\"},\"articleSection\":[\"Android\",\"\u7b14\u8bb0\"],\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/iichen.cn\/?p=456#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/iichen.cn\/?p=456\",\"url\":\"https:\/\/iichen.cn\/?p=456\",\"name\":\"Androiod-Bitmap - IIchen\",\"isPartOf\":{\"@id\":\"https:\/\/iichen.cn\/#website\"},\"datePublished\":\"2022-12-03T02:25:17+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/iichen.cn\/?p=456#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/iichen.cn\/?p=456\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/iichen.cn\/?p=456#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/iichen.cn\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Androiod-Bitmap\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/iichen.cn\/#website\",\"url\":\"https:\/\/iichen.cn\/\",\"name\":\"IIchen\",\"description\":\"Just do it!\",\"publisher\":{\"@id\":\"https:\/\/iichen.cn\/#\/schema\/person\/4a47edf85ab49841df9e8f6aee40b77c\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/iichen.cn\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"zh-Hans\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\/\/iichen.cn\/#\/schema\/person\/4a47edf85ab49841df9e8f6aee40b77c\",\"name\":\"iichen\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/iichen.cn\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/iichen.cn\/wp-content\/uploads\/2025\/01\/avatar.jpg\",\"contentUrl\":\"https:\/\/iichen.cn\/wp-content\/uploads\/2025\/01\/avatar.jpg\",\"width\":940,\"height\":940,\"caption\":\"iichen\"},\"logo\":{\"@id\":\"https:\/\/iichen.cn\/#\/schema\/person\/image\/\"},\"sameAs\":[\"https:\/\/www.iichen.cn\"],\"url\":\"https:\/\/iichen.cn\/?author=1\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Androiod-Bitmap - IIchen","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/iichen.cn\/?p=456","og_locale":"zh_CN","og_type":"article","og_title":"Androiod-Bitmap - IIchen","og_description":"1. \u5360\u7528\u5927\u5c0f scaledWidth = int( \u539f\u56fe\u5bbd\u5ea6 * (\u8bbe\u5907\u7684 dpi \/ \u76ee\u5f55\u5bf9\u5e94\u7684 dpi [&hellip;]","og_url":"https:\/\/iichen.cn\/?p=456","og_site_name":"IIchen","article_published_time":"2022-12-03T02:25:17+00:00","author":"iichen","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"iichen","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"1 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/iichen.cn\/?p=456#article","isPartOf":{"@id":"https:\/\/iichen.cn\/?p=456"},"author":{"name":"iichen","@id":"https:\/\/iichen.cn\/#\/schema\/person\/4a47edf85ab49841df9e8f6aee40b77c"},"headline":"Androiod-Bitmap","datePublished":"2022-12-03T02:25:17+00:00","mainEntityOfPage":{"@id":"https:\/\/iichen.cn\/?p=456"},"wordCount":65,"commentCount":0,"publisher":{"@id":"https:\/\/iichen.cn\/#\/schema\/person\/4a47edf85ab49841df9e8f6aee40b77c"},"articleSection":["Android","\u7b14\u8bb0"],"inLanguage":"zh-Hans","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/iichen.cn\/?p=456#respond"]}]},{"@type":"WebPage","@id":"https:\/\/iichen.cn\/?p=456","url":"https:\/\/iichen.cn\/?p=456","name":"Androiod-Bitmap - IIchen","isPartOf":{"@id":"https:\/\/iichen.cn\/#website"},"datePublished":"2022-12-03T02:25:17+00:00","breadcrumb":{"@id":"https:\/\/iichen.cn\/?p=456#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/iichen.cn\/?p=456"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/iichen.cn\/?p=456#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/iichen.cn\/"},{"@type":"ListItem","position":2,"name":"Androiod-Bitmap"}]},{"@type":"WebSite","@id":"https:\/\/iichen.cn\/#website","url":"https:\/\/iichen.cn\/","name":"IIchen","description":"Just do it!","publisher":{"@id":"https:\/\/iichen.cn\/#\/schema\/person\/4a47edf85ab49841df9e8f6aee40b77c"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/iichen.cn\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"zh-Hans"},{"@type":["Person","Organization"],"@id":"https:\/\/iichen.cn\/#\/schema\/person\/4a47edf85ab49841df9e8f6aee40b77c","name":"iichen","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/iichen.cn\/#\/schema\/person\/image\/","url":"https:\/\/iichen.cn\/wp-content\/uploads\/2025\/01\/avatar.jpg","contentUrl":"https:\/\/iichen.cn\/wp-content\/uploads\/2025\/01\/avatar.jpg","width":940,"height":940,"caption":"iichen"},"logo":{"@id":"https:\/\/iichen.cn\/#\/schema\/person\/image\/"},"sameAs":["https:\/\/www.iichen.cn"],"url":"https:\/\/iichen.cn\/?author=1"}]}},"_links":{"self":[{"href":"https:\/\/iichen.cn\/index.php?rest_route=\/wp\/v2\/posts\/456","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/iichen.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/iichen.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/iichen.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/iichen.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=456"}],"version-history":[{"count":0,"href":"https:\/\/iichen.cn\/index.php?rest_route=\/wp\/v2\/posts\/456\/revisions"}],"wp:attachment":[{"href":"https:\/\/iichen.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=456"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/iichen.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=456"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/iichen.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=456"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}