Kotlin高仿微信-第30篇-朋友圈-发布作品(小视频)

 Kotlin高仿微信-项目实践58篇详细讲解了各个功能点,包括:注册、登录、主页、单聊(文本、表情、语音、图片、小视频、视频通话、语音通话、红包、转账)、群聊、个人信息、朋友圈、支付服务、扫一扫、搜索好友、添加好友、开通VIP等众多功能。

Kotlin高仿微信-项目实践58篇,点击查看详情

效果图:

实现代码:

<?xml version="1.0" encoding="utf-8"?>
<layout>

    <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/moment_publish_root"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/wc_base_bg">


        <include layout="@layout/wc_base_top_title"/>

        <androidx.appcompat.widget.AppCompatEditText
            android:id="@+id/moment_publish_content"
            app:layout_constraintTop_toBottomOf="@+id/base_top_root_layout"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="12dp"
            android:maxLines="8"
            android:minLines="4"
            android:gravity="top"
            android:hint="这一刻的想法..."/>

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/moment_publish_recyclerview"
            app:layout_constraintTop_toBottomOf="@+id/moment_publish_content"
            app:layout_constraintStart_toStartOf="parent"
            android:layout_margin="10dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>

        <androidx.appcompat.widget.AppCompatImageView
            android:id="@+id/moment_publish_video"
            app:layout_constraintTop_toBottomOf="@+id/moment_publish_content"
            app:layout_constraintStart_toStartOf="parent"
            android:layout_width="180dp"
            android:layout_height="180dp"
            android:layout_marginLeft="10dp"
            android:scaleType="centerCrop"/>

        <androidx.appcompat.widget.AppCompatImageView
            android:id="@+id/moment_publish_play"
            app:layout_constraintTop_toTopOf="@+id/moment_publish_video"
            app:layout_constraintBottom_toBottomOf="@+id/moment_publish_video"
            app:layout_constraintStart_toStartOf="@+id/moment_publish_video"
            app:layout_constraintEnd_toEndOf="@+id/moment_publish_video"
            android:layout_width="60dp"
            android:layout_height="60dp"
            android:visibility="gone"
            android:src="@android:drawable/ic_media_play"/>


    </androidx.constraintlayout.widget.ConstraintLayout>
</layout>

/**
 * Author : wangning
 * Email : maoning20080809@163.com
 * Date : 2022/5/24 14:13
 * Description : 发布朋友圈信息
 */
class MomentsPublishFragment : BaseDataBindingFragment<WcMomentsPublishBinding>() {

    override fun getLayoutRes() = R.layout.wc_moments_publish

    var type : Int = CommonUtils.Moments.TYPE_PICTURE
    //多张图片
    var imageList = ArrayList<String>()
    //小视频地址
    var videoFilePath = ""
    private var loadingUtils: BaseDialogUtils? = null
    private var navController: NavController? = null

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        navController = findNavController()

        super.builder().showRightButton().hideTitleContent().setOnRightItemClick(object : WcOnItemClickInterface{
            override fun onItemClick(obj: Any) {
                if(!NetWorkUtils.isNetworkConnected()) {
                    ToastUtils.makeText(requireActivity(), BaseUtils.getString(R.string.wc_base_network_tip))
                    return
                }

                SoftInputUtils.hideSoftInput(moment_publish_content)
                showLoadingDialog()

                //点击右上角发表按钮
                CoroutineScope(Dispatchers.IO).launch {
                    when(type){
                        CommonUtils.Moments.TYPE_PICTURE -> {
                            var result = UploadFileUtils.uploadImages(type, moment_publish_content.text.toString(), imageList)
                            publishResult(result)
                        }
                        CommonUtils.Moments.TYPE_VIDEO -> {
                            var result = UploadFileUtils.uploadVideo(type, moment_publish_content.text.toString(), videoFilePath)
                            publishResult(result)
                        }
                    }
                }
            }
        })

        type = arguments?.get(CommonUtils.Moments.TYPE_NAME) as Int
        videoFilePath = arguments?.get(CommonUtils.Moments.TYPE_VIDEO_PATH).toString()
        var images = arguments?.get(CommonUtils.Moments.TYPE_IMAGE_PATH)
        TagUtils.d("朋友圈:${type}, ${videoFilePath} , ${images}")
        if(images != null && !"null".equals(images)){
            if(images is String){
                imageList.add(images)
            } else {
                imageList = images as ArrayList<String>
            }
        }

        when(type){
            CommonUtils.Moments.TYPE_PICTURE -> showImage()
            CommonUtils.Moments.TYPE_VIDEO -> showVideo()
        }

    }

    //显示图片
    fun showImage(){
        moment_publish_video.visibility = View.GONE
        moment_publish_play.visibility = View.GONE
        moment_publish_recyclerview.visibility = View.VISIBLE

        var adapter = MomentsPublishAdapter(1, imageList, object : WcOnItemClickInterface{
            override fun onItemClick(obj: Any) {
                var iamgeFilePath = obj as String
                var bundle = bundleOf(CommonUtils.Moments.TYPE_NAME to type, CommonUtils.Moments.TYPE_IMAGE_PATH to iamgeFilePath)
                Navigation.findNavController(moment_publish_recyclerview).navigate(R.id.action_publish_preview, bundle)
            }
        })
        var linearLayoutManager = GridLayoutManager(requireActivity() , 3)
        moment_publish_recyclerview.layoutManager = linearLayoutManager
        moment_publish_recyclerview.adapter = adapter
    }

    //显示小视频
    fun showVideo(){
        TagUtils.d("显示视频路径:$videoFilePath")
        moment_publish_video.visibility = View.VISIBLE
        moment_publish_play.visibility = View.VISIBLE
        moment_publish_recyclerview.visibility = View.GONE
        //var videoFilePath = "/mnt/sdcard/image/1.mp4"
        val retriever = MediaMetadataRetriever()

        var path :String? = videoFilePath
        val uri = Uri.parse(path)
        val scheme = uri.scheme
        if ("file" == scheme) {
            path = uri.getPath()
            TagUtils.d("显示视频路径path = :$path")
            retriever.setDataSource(path)
        } else {
            retriever.setDataSource(videoFilePath)
        }
        val bmp = retriever.getFrameAtTime(0, MediaMetadataRetriever.OPTION_CLOSEST_SYNC)
        moment_publish_video.setImageBitmap(bmp)

        moment_publish_video.setOnClickListener {
            var bundle = bundleOf(CommonUtils.Moments.TYPE_NAME to type, CommonUtils.Moments.TYPE_IMAGE_PATH to videoFilePath)
            Navigation.findNavController(it).navigate(R.id.action_publish_preview, bundle)
        }

    }

    //显示加载对话框
    private fun showLoadingDialog(){
        loadingUtils = BaseDialogUtils(requireActivity())
        loadingUtils!!.builder()
            .hideCancel()
            .hideConfirm()
            .setCancelable(true)
            .setOnLoadingClick(object : BaseDialogUtils.OnLoadingClick{
                override fun onClickCancel() {
                    ToastUtils.makeText(requireActivity(), "对话框取消按钮")
                }

                override fun onClickConfirm() {
                    ToastUtils.makeText(requireActivity(), "对话框确定按钮")
                }
            })
        loadingUtils?.show()
    }

    private fun publishResult(any: Any?){
        CoroutineScope(Dispatchers.Main).launch {
            dismissLoadingDialog()
            if(any == null){
                ToastUtils.makeText(R.string.wc_publish_failure)
                Navigation.findNavController(moment_publish_video).popBackStack()
            } else {
                var momentsBean = any as MomentsBean
                CoroutineScope(Dispatchers.IO).launch {
                    MomentsRepository.insertMomentLocal(momentsBean)
                    CoroutineScope(Dispatchers.Main).launch {
                        ToastUtils.makeText(R.string.wc_publish_success)
                        navController?.previousBackStackEntry?.savedStateHandle?.set(CommonUtils.Moments.PUBLISH_SUCCESS, true)
                        navController?.popBackStack()
                    }
                }
            }
        }
    }

    //隐藏加载对话框
    private fun dismissLoadingDialog(){
        loadingUtils?.dismiss()
    }

}

/**
 * Author : wangning
 * Email : maoning20080809@163.com
 * Date : 2022/5/24 14:28
 * Description :
 */
class MomentsPublishAdapter(var type : Int, var list : List<String>, var onItemClickInterface: WcOnItemClickInterface) : RecyclerView.Adapter<MomentsPublishAdapter.MomentsPublishHolder>() {

    override fun getItemCount() = list?.size

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MomentsPublishHolder {
        var view = LayoutInflater.from(parent.context).inflate(R.layout.wc_moments_publish_item, parent, false)
        return MomentsPublishHolder(view)
    }

    override fun onBindViewHolder(holder: MomentsPublishHolder, position: Int) {
        var imageUrl = list?.get(position)

        if(type == 1){
            //发布
            GlideUtils.load(holder.iconView, imageUrl)
            holder.iconView.setOnClickListener {
               onItemClickInterface.onItemClick(imageUrl)
           }
        } else if(type == 2){
            //朋友圈服务器的图片
            imageUrl = CommonUtils.Moments.getReallyImageUrl(imageUrl)
            //TagUtils.d("朋友圈图片:${imageUrl}")
            //GlideUtils.load(holder.iconView, imageUrl, 90f)
            GlideUtils.load(holder.iconView, imageUrl)
            holder.iconView.setOnClickListener {
                onItemClickInterface.onItemClick(imageUrl)
            }
        }

    }


    class MomentsPublishHolder(itemView : View) : RecyclerView.ViewHolder(itemView) {

        var iconView  = itemView.findViewById<ImageView>(R.id.moments_publish_item_icon)
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

六毛六66

你的鼓励是我创作的最大动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值