• Android 生成并分享海报,到微信


    先上效果图:海报二维码 动态生成,背景固定

     分享后,微信看到的效果

    第一步:根据微信文档导包,配置环境(权限、回调Activity,拿到appId、混淆等),这里我就不具体介绍了,根据官方文档来就行了

    https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&id=1417751808&token=&lang=zh_CN

     

    第二步:dialog布局 ,二维码一般就是根据一个URL 地址生成的二维码图片,然后微信用户 长按识别二维码,就能打开链接了

    1. "1.0" encoding="utf-8"?>
    2. "http://schemas.android.com/apk/res/android"
    3. xmlns:tools="http://schemas.android.com/tools"
    4. xmlns:app="http://schemas.android.com/apk/res-auto"
    5. android:layout_width="match_parent"
    6. android:layout_height="wrap_content"
    7. android:background="@color/line4"
    8. tools:ignore="MissingConstraints"
    9. android:paddingTop="@dimen/dp_15"
    10. android:orientation="vertical"
    11. >
    12. android:id="@+id/clImageView"
    13. android:layout_width="wrap_content"
    14. android:layout_height="wrap_content"
    15. android:visibility="visible"
    16. android:background="@color/colorTransparent"
    17. android:layout_marginBottom="@dimen/dp_20"
    18. android:layout_gravity="center_horizontal"
    19. >
    20. android:id="@+id/ivBg"
    21. android:layout_width="match_parent"
    22. android:layout_height="match_parent"
    23. android:src="@mipmap/share_image_bg"
    24. />
    25. android:id="@+id/ivQRCode"
    26. android:layout_width="@dimen/dp_60"
    27. android:layout_height="@dimen/dp_60"
    28. app:layout_constraintLeft_toLeftOf="parent"
    29. app:layout_constraintRight_toRightOf="parent"
    30. android:layout_marginBottom="@dimen/dp_15"
    31. app:layout_constraintBottom_toTopOf="@+id/tvLongClick"/>
    32. android:id="@+id/tvLongClick"
    33. android:layout_width="wrap_content"
    34. android:layout_height="@dimen/dp_20"
    35. android:textColor="@color/colorWhite"
    36. android:text="长按识别二维码"
    37. android:textSize="@dimen/sp_8"
    38. android:gravity="center"
    39. android:paddingLeft="@dimen/dp_10"
    40. android:paddingRight="@dimen/dp_10"
    41. app:layout_constraintLeft_toLeftOf="@+id/ivBg"
    42. app:layout_constraintRight_toRightOf="@+id/ivBg"
    43. android:layout_marginBottom="@dimen/dp_20"
    44. android:background="@drawable/shape_share_image_text_bg"
    45. app:layout_constraintBottom_toBottomOf="@+id/ivBg"/>
    46. android:layout_width="match_parent"
    47. android:layout_height="wrap_content">
    48. android:id="@+id/tv1"
    49. android:layout_width="wrap_content"
    50. android:layout_height="wrap_content"
    51. android:layout_marginLeft="@dimen/dp_16"
    52. android:text="分享到"
    53. android:textColor="@color/colorBlack"
    54. android:textSize="@dimen/sp_18"
    55. app:layout_constraintLeft_toLeftOf="parent" />
    56. android:id="@+id/tvWX"
    57. android:layout_width="wrap_content"
    58. android:layout_height="wrap_content"
    59. android:textSize="@dimen/sp_14"
    60. android:text="微信"
    61. app:layout_constraintHorizontal_chainStyle="spread_inside"
    62. android:gravity="center"
    63. app:layout_constraintLeft_toLeftOf="parent"
    64. android:layout_marginTop="@dimen/dp_30"
    65. android:drawablePadding="@dimen/dp_11"
    66. android:layout_marginLeft="@dimen/dp_16"
    67. app:layout_constraintTop_toBottomOf="@+id/tv1"
    68. app:layout_constraintRight_toLeftOf="@+id/tvWXFriends"
    69. android:drawableTop="@mipmap/share_wx"
    70. android:textColor="@color/colorBlack_59"/>
    71. android:id="@+id/tvWXFriends"
    72. android:layout_width="wrap_content"
    73. android:layout_height="wrap_content"
    74. android:textSize="@dimen/sp_14"
    75. android:text="微信朋友圈"
    76. android:gravity="center"
    77. android:layout_marginTop="@dimen/dp_30"
    78. android:drawablePadding="@dimen/dp_11"
    79. app:layout_constraintLeft_toRightOf="@+id/tvWX"
    80. app:layout_constraintRight_toLeftOf="@+id/tvDownCode"
    81. app:layout_constraintTop_toBottomOf="@+id/tv1"
    82. android:drawableTop="@mipmap/share_wx_friends"
    83. android:textColor="@color/colorBlack_59"/>
    84. android:id="@+id/tvDownCode"
    85. android:layout_width="wrap_content"
    86. android:layout_height="wrap_content"
    87. android:textSize="@dimen/sp_14"
    88. android:text="保存本地"
    89. android:gravity="center"
    90. app:layout_constraintRight_toLeftOf="@+id/tvQQ"
    91. app:layout_constraintLeft_toRightOf="@+id/tvWXFriends"
    92. android:layout_marginTop="@dimen/dp_30"
    93. android:drawablePadding="@dimen/dp_11"
    94. app:layout_constraintTop_toBottomOf="@+id/tv1"
    95. android:drawableTop="@mipmap/qr_code_down"
    96. android:visibility="visible"
    97. android:layout_marginRight="@dimen/dp_30"
    98. android:textColor="@color/colorBlack_59"/>
    99. android:id="@+id/tvQQ"
    100. android:layout_width="wrap_content"
    101. android:layout_height="wrap_content"
    102. android:textSize="@dimen/sp_14"
    103. android:text="QQ"
    104. android:gravity="center"
    105. android:visibility="invisible"
    106. app:layout_constraintLeft_toRightOf="@+id/tvDownCode"
    107. app:layout_constraintRight_toRightOf="parent"
    108. android:layout_marginTop="@dimen/dp_30"
    109. android:drawablePadding="@dimen/dp_11"
    110. app:layout_constraintTop_toBottomOf="@+id/tv1"
    111. android:drawableTop="@mipmap/share_qq"
    112. android:textColor="@color/colorBlack_59"/>
    113. android:id="@+id/tvDismiss"
    114. android:layout_width="match_parent"
    115. android:layout_height="@dimen/dp_50"
    116. android:text="取消"
    117. android:gravity="center"
    118. android:textColor="@color/colorBlack"
    119. android:textSize="@dimen/sp_15"
    120. app:layout_constraintTop_toBottomOf="@+id/tvWX"
    121. android:background="@color/colorWhite"
    122. android:layout_marginTop="@dimen/dp_40"/>

    MyConstraintLayout代码,你也可以不用 ConstraintLayout 根据自己需求继承不同布局就行了

    重要的是,你要分享出去的海报布局必须在这个父布局里面

    1. class MyConstraintLayout:ConstraintLayout {
    2. constructor(context: Context) : super(context)
    3. constructor(context: Context, attributeSet: AttributeSet) : super(context, attributeSet)
    4. constructor(context: Context, attributeSet: AttributeSet, defStyleAttr: Int) : super(context, attributeSet, defStyleAttr)
    5. fun getBitmap():Bitmap{
    6. val bitmap = Bitmap.createBitmap(this.width, this.height,Bitmap.Config.ARGB_8888)
    7. this.draw(Canvas(bitmap))
    8. return bitmap
    9. }
    10. }

     

    第三步:注册到微信

    1. var mIWXApi: IWXAPI? = null
    2. private fun regToWX(){
    3. mIWXApi = WXAPIFactory.createWXAPI(context, "微信appId", true)
    4. mIWXApi?.registerApp("微信appId")
    5. //建议动态监听微信启动广播进行注册到微信
    6. requireActivity().registerReceiver(object : BroadcastReceiver() {
    7. override fun onReceive(context: Context?, intent: Intent?) {
    8. // 将该app注册到微信
    9. mIWXApi?.registerApp(WX_APPID)
    10. }
    11. }, IntentFilter(ConstantsAPI.ACTION_REFRESH_WXAPP))
    12. }

    第四步:点击分享 或者 下载

    1. /**
    2. * 分享图片到微信
    3. *
    4. * @param friendsCircle true:分享到朋友圈 false:分享到微信好友
    5. */
    6. private fun shareWXImage(friendsCircle: Boolean){
    7. //val bmp: Bitmap = BitmapFactory.decodeResource(resources, R.mipmap.share_image)
    8. //val bmp = requireActivity().capture()
    9. val clImageView = view?.find(R.id.clImageView)
    10. val bmp = clImageView?.getBitmap()
    11. if (bmp == null){
    12. T.showShort(requireContext(),"分享失败")
    13. return
    14. }
    15. val imgObj = WXImageObject(bmp)
    16. val msg = WXMediaMessage(imgObj)
    17. //设置缩略图 这里宽高不能设置太大,不然分享会失败 大概250就差不多了
    18. val thumbBmp = Bitmap.createScaledBitmap(bmp, 200, 200, true)
    19. bmp.recycle()
    20. msg.thumbData = thumbBmp.bitToByte()
    21. //构造一个Req
    22. val req = SendMessageToWX.Req()
    23. req.transaction = "image ${System.currentTimeMillis()}"
    24. req.message = msg
    25. req.scene = if (friendsCircle) SendMessageToWX.Req.WXSceneTimeline else SendMessageToWX.Req.WXSceneSession
    26. //调用api接口,发送数据到微信
    27. val shareSuccess = mIWXApi?.sendReq(req)
    28. shareSuccess?.let {
    29. if (!it){
    30. T.showShort(context,"分享调起失败")
    31. }
    32. }
    33. }

    下载代码:

    1. /**
    2. * 保存邀请二维码到本地
    3. */
    4. private fun saveMediaToStorage(context: Context, bitmap: Bitmap) {
    5. val filename = "邀请码.jpg"
    6. var fos: OutputStream? = null
    7. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
    8. context.contentResolver?.also { resolver ->
    9. val contentValues = ContentValues().apply {
    10. put(MediaStore.MediaColumns.DISPLAY_NAME, filename)
    11. put(MediaStore.MediaColumns.MIME_TYPE, "image/jpg")
    12. put(MediaStore.MediaColumns.RELATIVE_PATH, Environment.DIRECTORY_PICTURES)
    13. }
    14. val imageUri: Uri? =
    15. resolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues)
    16. fos = imageUri?.let { resolver.openOutputStream(it) }
    17. }
    18. } else {
    19. val imagesDir =
    20. Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)
    21. val image = File(imagesDir, filename)
    22. fos = FileOutputStream(image)
    23. }
    24. fos?.use {
    25. bitmap.compress(Bitmap.CompressFormat.JPEG, 100, it)
    26. T.showShort(context,"保存成功")
    27. }
    28. }

    扩展:分享链接到微信

    1. /**
    2. * 分享链接到微信
    3. *
    4. * @param friendsCircle true:分享到朋友圈 false:分享到微信好友
    5. */
    6. private fun shareTextWX(friendsCircle: Boolean) {
    7. //用 WXTextObject 对象初始化一个 WXMediaMessage 对象
    8. val webpage = WXWebpageObject()
    9. //分享url
    10. webpage.webpageUrl = "url"
    11. val msg = WXMediaMessage(webpage)
    12. msg.title = "标题"
    13. msg.description = "描述"
    14. //icon
    15. val bit = BitmapFactory.decodeResource(resources, R.mipmap.icon)
    16. //封面图片byte数组
    17. if (bit != null) {
    18. msg.thumbData = bit.bitToByte()
    19. }
    20. val req = SendMessageToWX.Req()
    21. //transaction字段用与唯一标示一个请求
    22. req.transaction = "url ${System.currentTimeMillis()}"
    23. req.message = msg
    24. req.scene = if (friendsCircle) SendMessageToWX.Req.WXSceneTimeline else SendMessageToWX.Req.WXSceneSession
    25. //调用api接口,发送数据到微信
    26. val shareSuccess = mIWXApi?.sendReq(req)
    27. }

  • 相关阅读:
    入门数据库days2
    如何动态修改 spring aop 切面信息?让自动日志输出框架更好用
    C++类型推导
    Django笔记四十四之Nginx+uWSGI部署Django以及Nginx负载均衡操作
    200PLC转以太网通讯远创智控模块在手机平板移动平台中的应用案例题
    HTML+CSS抗疫网页设计 疫情感动人物静态HTML网页 web前端开发技术 web课程设计 网页规划与设计
    Java面试题200+大全(合适各级Java人员)
    Mosaic数据增强
    分布式链路追踪-常用技术选型
    初识RabbitMQ
  • 原文地址:https://blog.csdn.net/qq_27400335/article/details/125877476