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

分享后,微信看到的效果

第一步:根据微信文档导包,配置环境(权限、回调Activity,拿到appId、混淆等),这里我就不具体介绍了,根据官方文档来就行了
第二步:dialog布局 ,二维码一般就是根据一个URL 地址生成的二维码图片,然后微信用户 长按识别二维码,就能打开链接了
- "1.0" encoding="utf-8"?>
"http://schemas.android.com/apk/res/android" - xmlns:tools="http://schemas.android.com/tools"
- xmlns:app="http://schemas.android.com/apk/res-auto"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:background="@color/line4"
- tools:ignore="MissingConstraints"
- android:paddingTop="@dimen/dp_15"
- android:orientation="vertical"
- >
-
-
-
- android:id="@+id/clImageView"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:visibility="visible"
- android:background="@color/colorTransparent"
- android:layout_marginBottom="@dimen/dp_20"
- android:layout_gravity="center_horizontal"
- >
-
- android:id="@+id/ivBg"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:src="@mipmap/share_image_bg"
- />
-
-
- android:id="@+id/ivQRCode"
- android:layout_width="@dimen/dp_60"
- android:layout_height="@dimen/dp_60"
- app:layout_constraintLeft_toLeftOf="parent"
- app:layout_constraintRight_toRightOf="parent"
- android:layout_marginBottom="@dimen/dp_15"
- app:layout_constraintBottom_toTopOf="@+id/tvLongClick"/>
-
-
-
- android:id="@+id/tvLongClick"
- android:layout_width="wrap_content"
- android:layout_height="@dimen/dp_20"
- android:textColor="@color/colorWhite"
- android:text="长按识别二维码"
- android:textSize="@dimen/sp_8"
- android:gravity="center"
- android:paddingLeft="@dimen/dp_10"
- android:paddingRight="@dimen/dp_10"
- app:layout_constraintLeft_toLeftOf="@+id/ivBg"
- app:layout_constraintRight_toRightOf="@+id/ivBg"
- android:layout_marginBottom="@dimen/dp_20"
- android:background="@drawable/shape_share_image_text_bg"
- app:layout_constraintBottom_toBottomOf="@+id/ivBg"/>
-
-
-
-
- android:layout_width="match_parent"
- android:layout_height="wrap_content">
-
-
- android:id="@+id/tv1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginLeft="@dimen/dp_16"
- android:text="分享到"
- android:textColor="@color/colorBlack"
- android:textSize="@dimen/sp_18"
- app:layout_constraintLeft_toLeftOf="parent" />
-
-
- android:id="@+id/tvWX"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:textSize="@dimen/sp_14"
- android:text="微信"
- app:layout_constraintHorizontal_chainStyle="spread_inside"
- android:gravity="center"
- app:layout_constraintLeft_toLeftOf="parent"
- android:layout_marginTop="@dimen/dp_30"
- android:drawablePadding="@dimen/dp_11"
- android:layout_marginLeft="@dimen/dp_16"
- app:layout_constraintTop_toBottomOf="@+id/tv1"
- app:layout_constraintRight_toLeftOf="@+id/tvWXFriends"
- android:drawableTop="@mipmap/share_wx"
- android:textColor="@color/colorBlack_59"/>
-
- android:id="@+id/tvWXFriends"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:textSize="@dimen/sp_14"
- android:text="微信朋友圈"
- android:gravity="center"
- android:layout_marginTop="@dimen/dp_30"
- android:drawablePadding="@dimen/dp_11"
- app:layout_constraintLeft_toRightOf="@+id/tvWX"
- app:layout_constraintRight_toLeftOf="@+id/tvDownCode"
- app:layout_constraintTop_toBottomOf="@+id/tv1"
- android:drawableTop="@mipmap/share_wx_friends"
- android:textColor="@color/colorBlack_59"/>
-
-
-
- android:id="@+id/tvDownCode"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:textSize="@dimen/sp_14"
- android:text="保存本地"
- android:gravity="center"
- app:layout_constraintRight_toLeftOf="@+id/tvQQ"
- app:layout_constraintLeft_toRightOf="@+id/tvWXFriends"
- android:layout_marginTop="@dimen/dp_30"
- android:drawablePadding="@dimen/dp_11"
- app:layout_constraintTop_toBottomOf="@+id/tv1"
- android:drawableTop="@mipmap/qr_code_down"
- android:visibility="visible"
- android:layout_marginRight="@dimen/dp_30"
- android:textColor="@color/colorBlack_59"/>
-
-
- android:id="@+id/tvQQ"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:textSize="@dimen/sp_14"
- android:text="QQ"
- android:gravity="center"
- android:visibility="invisible"
- app:layout_constraintLeft_toRightOf="@+id/tvDownCode"
- app:layout_constraintRight_toRightOf="parent"
- android:layout_marginTop="@dimen/dp_30"
- android:drawablePadding="@dimen/dp_11"
- app:layout_constraintTop_toBottomOf="@+id/tv1"
- android:drawableTop="@mipmap/share_qq"
- android:textColor="@color/colorBlack_59"/>
-
-
- android:id="@+id/tvDismiss"
- android:layout_width="match_parent"
- android:layout_height="@dimen/dp_50"
- android:text="取消"
- android:gravity="center"
- android:textColor="@color/colorBlack"
- android:textSize="@dimen/sp_15"
- app:layout_constraintTop_toBottomOf="@+id/tvWX"
- android:background="@color/colorWhite"
- android:layout_marginTop="@dimen/dp_40"/>
-
-
MyConstraintLayout代码,你也可以不用 ConstraintLayout 根据自己需求继承不同布局就行了
重要的是,你要分享出去的海报布局必须在这个父布局里面
- class MyConstraintLayout:ConstraintLayout {
- constructor(context: Context) : super(context)
-
- constructor(context: Context, attributeSet: AttributeSet) : super(context, attributeSet)
-
- constructor(context: Context, attributeSet: AttributeSet, defStyleAttr: Int) : super(context, attributeSet, defStyleAttr)
-
- fun getBitmap():Bitmap{
- val bitmap = Bitmap.createBitmap(this.width, this.height,Bitmap.Config.ARGB_8888)
- this.draw(Canvas(bitmap))
-
- return bitmap
- }
- }
第三步:注册到微信
- var mIWXApi: IWXAPI? = null
-
- private fun regToWX(){
- mIWXApi = WXAPIFactory.createWXAPI(context, "微信appId", true)
- mIWXApi?.registerApp("微信appId")
- //建议动态监听微信启动广播进行注册到微信
- requireActivity().registerReceiver(object : BroadcastReceiver() {
- override fun onReceive(context: Context?, intent: Intent?) {
- // 将该app注册到微信
- mIWXApi?.registerApp(WX_APPID)
- }
- }, IntentFilter(ConstantsAPI.ACTION_REFRESH_WXAPP))
-
- }
第四步:点击分享 或者 下载
- /**
- * 分享图片到微信
- *
- * @param friendsCircle true:分享到朋友圈 false:分享到微信好友
- */
- private fun shareWXImage(friendsCircle: Boolean){
- //val bmp: Bitmap = BitmapFactory.decodeResource(resources, R.mipmap.share_image)
- //val bmp = requireActivity().capture()
-
- val clImageView = view?.find
(R.id.clImageView) - val bmp = clImageView?.getBitmap()
-
- if (bmp == null){
- T.showShort(requireContext(),"分享失败")
- return
- }
-
- val imgObj = WXImageObject(bmp)
-
- val msg = WXMediaMessage(imgObj)
-
- //设置缩略图 这里宽高不能设置太大,不然分享会失败 大概250就差不多了
- val thumbBmp = Bitmap.createScaledBitmap(bmp, 200, 200, true)
- bmp.recycle()
- msg.thumbData = thumbBmp.bitToByte()
-
- //构造一个Req
- val req = SendMessageToWX.Req()
- req.transaction = "image ${System.currentTimeMillis()}"
- req.message = msg
- req.scene = if (friendsCircle) SendMessageToWX.Req.WXSceneTimeline else SendMessageToWX.Req.WXSceneSession
- //调用api接口,发送数据到微信
- val shareSuccess = mIWXApi?.sendReq(req)
- shareSuccess?.let {
- if (!it){
- T.showShort(context,"分享调起失败")
- }
- }
- }
下载代码:
- /**
- * 保存邀请二维码到本地
- */
- private fun saveMediaToStorage(context: Context, bitmap: Bitmap) {
- val filename = "邀请码.jpg"
- var fos: OutputStream? = null
-
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
- context.contentResolver?.also { resolver ->
- val contentValues = ContentValues().apply {
- put(MediaStore.MediaColumns.DISPLAY_NAME, filename)
- put(MediaStore.MediaColumns.MIME_TYPE, "image/jpg")
- put(MediaStore.MediaColumns.RELATIVE_PATH, Environment.DIRECTORY_PICTURES)
- }
-
- val imageUri: Uri? =
- resolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues)
-
- fos = imageUri?.let { resolver.openOutputStream(it) }
- }
- } else {
- val imagesDir =
- Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)
- val image = File(imagesDir, filename)
- fos = FileOutputStream(image)
- }
-
- fos?.use {
- bitmap.compress(Bitmap.CompressFormat.JPEG, 100, it)
- T.showShort(context,"保存成功")
- }
- }
扩展:分享链接到微信
- /**
- * 分享链接到微信
- *
- * @param friendsCircle true:分享到朋友圈 false:分享到微信好友
- */
- private fun shareTextWX(friendsCircle: Boolean) {
- //用 WXTextObject 对象初始化一个 WXMediaMessage 对象
- val webpage = WXWebpageObject()
- //分享url
- webpage.webpageUrl = "url"
- val msg = WXMediaMessage(webpage)
- msg.title = "标题"
- msg.description = "描述"
- //icon
- val bit = BitmapFactory.decodeResource(resources, R.mipmap.icon)
- //封面图片byte数组
- if (bit != null) {
- msg.thumbData = bit.bitToByte()
- }
-
- val req = SendMessageToWX.Req()
- //transaction字段用与唯一标示一个请求
- req.transaction = "url ${System.currentTimeMillis()}"
- req.message = msg
- req.scene = if (friendsCircle) SendMessageToWX.Req.WXSceneTimeline else SendMessageToWX.Req.WXSceneSession
-
- //调用api接口,发送数据到微信
- val shareSuccess = mIWXApi?.sendReq(req)
- }
-
相关阅读:
入门数据库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