{"id":1553,"date":"2021-11-19T17:37:35","date_gmt":"2021-11-19T09:37:35","guid":{"rendered":"https:\/\/badgameshow.com\/fly\/?p=1553"},"modified":"2021-12-17T10:14:07","modified_gmt":"2021-12-17T02:14:07","slug":"android-%e5%90%88%e6%88%90%e5%85%a9%e5%bc%b5%e5%9c%96%e7%89%87%e4%b8%a6%e5%ad%98%e5%88%b0%e6%89%8b%e6%a9%9f%e7%9b%b8%e7%b0%bf","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/fly\/android-%e5%90%88%e6%88%90%e5%85%a9%e5%bc%b5%e5%9c%96%e7%89%87%e4%b8%a6%e5%ad%98%e5%88%b0%e6%89%8b%e6%a9%9f%e7%9b%b8%e7%b0%bf\/","title":{"rendered":"Android \u5408\u6210\u5169\u5f35\u5716\u7247\u4e26\u5b58\u5230\u624b\u6a5f\u76f8\u7c3f"},"content":{"rendered":"<h1>Android \u5408\u6210\u5169\u5f35\u5716\u7247\u4e26\u5b58\u5230\u624b\u6a5f\u76f8\u7c3f<\/h1>\n<h5>\u6211\u5011\u6709\u6642\u5019\u6703\u9700\u8981\u628a\u5169\u5f35\u5716\u7247\u5408\u5728\u4e00\u8d77\uff0c\u9019\u6642\u5019\u6211\u5011\u5c31\u6703\u7528\u5230\u5716\u5c64\u95dc\u4fc2\uff0c\u6211\u5011\u628a\u8981\u7576\u5e95\u5c64\u7684\u5716\u7247\u5148\u653e\u5728Canvas\uff0c\u63a5\u8457\u518d\u628a\u53e6\u5916\u4e00\u5f35\u8986\u84cb\u5728\u4e0a\u9762\uff0c\u524d\u63d0\u662f\u90a3\u5f35\u5716\u7247\u662f\u6709\u900f\u660e\u80cc\u666f\u7684\uff0c\u9019\u6a23\u624d\u80fd\u900f\u5230\u4e0b\u9762\u7684\u5e95\u5c64\u5716\u7247\u3002<\/h5>\n<hr \/>\n<h4>\u6587\u7ae0\u76ee\u9304<\/h4>\n<ol>\n<li><a href=\"#a\">\u7372\u53d6\u5716\u7247\u4e26\u8f49\u6210Bitmap<\/a><\/li>\n<li><a href=\"#b\">\u5408\u6210\u5169\u5f35\u5716\u7247<\/a><\/li>\n<li><a href=\"#c\">\u5132\u5b58\u5230\u624b\u6a5f\u76f8\u7c3f<\/a><\/li>\n<li><a href=\"#d\">\u7a0b\u5f0f\u78bc\u7bc4\u4f8b<\/a><\/li>\n<li><a href=\"#e\">\u6548\u679c\u5c55\u793a<\/a><\/li>\n<li><a href=\"#f\">Github<\/a><\/li>\n<\/ol>\n<hr \/>\n<p><a id=\"a\"><\/a><\/p>\n<h4>1.\u7372\u53d6\u5716\u7247\u4e26\u8f49\u6210Bitmap<\/h4>\n<pre><code class=\"language-Kotlin line-numbers\">\/\/\u80cc\u666f\u5716\u7247\nval bgBitmap = BitmapFactory.decodeResource(resources, R.drawable.bg)\n\/\/\u524d\u666f\u5716\u7247\nval coinBitmap = BitmapFactory.decodeResource(resources, R.drawable.coin)\n<\/code><\/pre>\n<p><a id=\"b\"><\/a><\/p>\n<h4>2.\u5408\u6210\u5169\u5f35\u5716\u7247<\/h4>\n<pre><code class=\"language-Kotlin line-numbers\">\/\/\u5c07\u80cc\u666f\u5716\u5728\u5857\u5c64\u6700\u5e95\u4e0b\nval bitmap = backBitmap.copy(Bitmap.Config.ARGB_8888, true)\nval canvas = Canvas(bitmap)\nval baseRect = Rect(0, 0, backBitmap.width, backBitmap.height)\nval frontRect = Rect(0, 0, frontBitmap.width, frontBitmap.height)\ncanvas.drawBitmap(frontBitmap, frontRect, baseRect, null)\n<\/code><\/pre>\n<p><a id=\"c\"><\/a><\/p>\n<h4>3.\u5132\u5b58\u5230\u624b\u6a5f\u76f8\u7c3f<\/h4>\n<pre><code class=\"language-Kotlin line-numbers\">val contentValues = ContentValues()\ncontentValues.put(MediaStore.Images.Media.DISPLAY_NAME, System.currentTimeMillis())\ncontentValues.put(MediaStore.Images.Media.MIME_TYPE, \"image\/jpeg\")\nval uri = contentResolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues)\nuri?.apply {\n    val ops = contentResolver.openOutputStream(this)\n    \/\/\u628aBitmap\u9664\u5b58\u5230\u624b\u6a5f\u76f8\u7c3f\n    mergeBitmap(bgBitmap, coinBitmap).compress(Bitmap.CompressFormat.JPEG, 100, ops)\n    ops?.close()\n}\n<\/code><\/pre>\n<p><a id=\"d\"><\/a><\/p>\n<h4>4.\u7a0b\u5f0f\u78bc\u7bc4\u4f8b<\/h4>\n<pre><code class=\"language-Kotlin line-numbers\">package com.example.compositepicturedemo\n\nimport android.content.ContentValues\nimport android.graphics.Bitmap\nimport android.graphics.BitmapFactory\nimport android.graphics.Canvas\nimport android.graphics.Rect\nimport androidx.appcompat.app.AppCompatActivity\nimport android.os.Bundle\nimport android.provider.MediaStore\nimport android.view.View\nimport android.widget.ImageView\n\nclass MainActivity : AppCompatActivity() {\n\n    private lateinit var bgBitmap: Bitmap\n    private lateinit var coinBitmap: Bitmap\n\n    override fun onCreate(savedInstanceState: Bundle?) {\n        super.onCreate(savedInstanceState)\n        setContentView(R.layout.activity_main)\n\n        val photoOne = findViewById&lt;ImageView&gt;(R.id.photo_one)\n        val photoResult = findViewById&lt;ImageView&gt;(R.id.photo_two)\n\n        bgBitmap = BitmapFactory.decodeResource(resources, R.drawable.bg)\n        photoOne.setImageBitmap(bgBitmap)\n\n        coinBitmap = BitmapFactory.decodeResource(resources, R.drawable.coin)\n        photoResult.setImageBitmap(coinBitmap)\n    }\n\n    private fun mergeBitmap(backBitmap: Bitmap, frontBitmap: Bitmap): Bitmap {\n        val bitmap = backBitmap.copy(Bitmap.Config.ARGB_8888, true)\n        val canvas = Canvas(bitmap)\n        val baseRect = Rect(0, 0, backBitmap.width, backBitmap.height)\n        val frontRect = Rect(0, 0, frontBitmap.width, frontBitmap.height)\n        canvas.drawBitmap(frontBitmap, frontRect, baseRect, null)\n        return bitmap\n    }\n\n    fun merge(view: View) {\n        val contentValues = ContentValues()\n        contentValues.put(MediaStore.Images.Media.DISPLAY_NAME, System.currentTimeMillis())\n        contentValues.put(MediaStore.Images.Media.MIME_TYPE, \"image\/jpeg\")\n        val uri =\n            contentResolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues)\n        uri?.apply {\n            val ops = contentResolver.openOutputStream(this)\n            mergeBitmap(bgBitmap, coinBitmap).compress(Bitmap.CompressFormat.JPEG, 100, ops)\n            ops?.close()\n        }\n    }\n}\n<\/code><\/pre>\n<p><a id=\"e\"><\/a><\/p>\n<h4>5.\u6548\u679c\u5c55\u793a<\/h4>\n<p><a href=\"https:\/\/badgameshow.com\/fly\/wp-content\/uploads\/2021\/11\/video-1637312564.gif\"><img decoding=\"async\" src=\"https:\/\/badgameshow.com\/fly\/wp-content\/uploads\/2021\/11\/video-1637312564.gif\" width=\"40%\"\/><\/a><\/p>\n<p><a id=\"f\"><\/a><\/p>\n<h4>6.Github<\/h4>\n<p><a class=\"wp-editor-md-post-content-link\" href=\"https:\/\/github.com\/MuHongWeiWei\/CompositePictureDemo\" target=\"_blank\" rel=\"noopener\">Android \u5408\u6210\u5169\u5f35\u5716\u7247\u4e26\u5b58\u5230\u624b\u6a5f\u76f8\u7c3f Github<\/a><\/p>\n\n<div style=\"font-size: 0px; height: 0px; line-height: 0px; margin: 0; padding: 0; clear: both;\"><\/div>","protected":false},"excerpt":{"rendered":"<p>Android \u5408\u6210\u5169\u5f35\u5716\u7247\u4e26\u5b58\u5230\u624b\u6a5f\u76f8\u7c3f \u6211\u5011\u6709\u6642\u5019\u6703\u9700\u8981\u628a\u5169\u5f35\u5716\u7247\u5408\u5728\u4e00\u8d77\uff0c\u9019\u6642\u5019\u6211\u5011\u5c31\u6703\u7528\u5230\u5716\u5c64\u95dc\u4fc2\uff0c\u6211 &hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"pgc_sgb_lightbox_settings":"","footnotes":""},"categories":[5],"tags":[13,210,15],"class_list":["post-1553","post","type-post","status-publish","format-standard","hentry","category-android","tag-android","tag-composite","tag-kotlin"],"_links":{"self":[{"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/posts\/1553","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/comments?post=1553"}],"version-history":[{"count":2,"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/posts\/1553\/revisions"}],"predecessor-version":[{"id":1556,"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/posts\/1553\/revisions\/1556"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/media?parent=1553"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/categories?post=1553"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/tags?post=1553"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}