美文网首页
自定义分享dialog

自定义分享dialog

作者: Method | 来源:发表于2021-05-15 14:36 被阅读0次

自定义dialog布局

class ShareDialog(context: Context) : AlertDialog(context) {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        /*圆角布局*/
        val layout = ConnerFrameLayout(context)
        layout.setBackgroundColor(Color.WHITE)
        layout.setViewOutLine(20,ViewHelper.SIDE_BOTTOM)
        /*分享内容*/
        val mRecyclerView = RecyclerView(context)
        mRecyclerView.apply {
            layoutManager = GridLayoutManager(context, 4)
            adapter = ShareAdapter(context, queryShareItems(), "")
        }
        /*布局参数*/
        val params = FrameLayout.LayoutParams(
            FrameLayout.LayoutParams.MATCH_PARENT,
            FrameLayout.LayoutParams.WRAP_CONTENT
        )
        //item margin
        val margin = dp2px(0)
        params.apply {
            leftMargin = margin
            topMargin = margin
            rightMargin = margin
            bottomMargin = margin
        }
        params.gravity = Gravity.CENTER
        layout.addView(mRecyclerView, params)
        //设置dialog布局
        setContentView(layout)
        //显示在底部
        window!!.setGravity(Gravity.BOTTOM)
        window!!.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
        window!!.setLayout(
            ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.WRAP_CONTENT
        )
    }

    private fun dp2px(dpValue: Int): Int {
        val displayMetrics = context.resources.displayMetrics
        return (displayMetrics.density*dpValue+0.5f).toInt()
    }
    /*布局Adapter*/
    private fun queryShareItems(): List<ResolveInfo> {
        val items = arrayListOf<ResolveInfo>()
        val intent = Intent()
        intent.action = Intent.ACTION_SEND
        intent.type = "text/plain"

        val providers =
            context.packageManager.queryIntentActivities(intent, 0)
        providers.forEach {
            val packageName = it.activityInfo.packageName
            if(TextUtils.equals(packageName,"com.tencent.mm") ||
                TextUtils.equals(packageName,"com.tencent.mobileqq")){
                items.add(it)
            }
        }
        return items
    }

}

布局内容adapter

class ShareAdapter(val context:Context, val data: List<ResolveInfo>, private val shareContent:String) :
    RecyclerView.Adapter<ShareAdapter.InnerViewHolder>() {

    lateinit var mPackManager: PackageManager
    class InnerViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        lateinit var mShareText: TextView
        lateinit var mShareIcon: ImageView

        init {
            mShareIcon = view.findViewById<ImageView>(R.id.share_icon)
            mShareText = view.findViewById<TextView>(R.id.share_text)
        }
    }

    init {
        mPackManager = context.packageManager
    }

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

    override fun getItemCount(): Int {
        return data.size
    }

    override fun onBindViewHolder(holder: InnerViewHolder, position: Int) {
        val resolveInfo = data[position]
        val shareIcon = resolveInfo.loadIcon(mPackManager)
        holder.mShareIcon.setImageDrawable(shareIcon)

        val mShareLabel = resolveInfo.loadLabel(mPackManager)
        holder.mShareText.text = mShareLabel

        holder.itemView.setOnClickListener {
            val pkg = resolveInfo.activityInfo.packageName
            val cls = resolveInfo.activityInfo.name
            val intent = Intent()
            intent.action = Intent.ACTION_SEND
            intent.type = "text/plain"
            intent.component = ComponentName(pkg, cls)
            intent.putExtra(Intent.EXTRA_TEXT, shareContent)
            context.startActivity(intent)
        }
    }
}

设置圆角

object ViewHelper {
    /*圆角大小*/
    var radius: Int = 0

    /*圆角位置*/
    var side: Int = 0
    const val SIDE_ALL = 0
    const val SIDE_LEFT = 1
    const val SIDE_TOP = 2
    const val SIDE_RIGHT = 3
    const val SIDE_BOTTOM = 4

    /*获取自定义属性*/
    fun setOutline(view: View, attributeSet: AttributeSet?) {
        val array =
            view.context.obtainStyledAttributes(attributeSet, R.styleable.ViewOutLine)
        radius = array.getDimensionPixelSize(R.styleable.ViewOutLine_clip_radius, 0)
        side = array.getIndex(R.styleable.ViewOutLine_clip_side)
        array.recycle()
        setOutlineView(view, radius, side)
    }

    /*设置view轮廓*/
    fun setOutlineView(view: View, radius: Int, side: Int) {
        view.outlineProvider = object : ViewOutlineProvider() {
            override fun getOutline(view: View, outline: Outline?) {
                val w = view.width
                val h = view.height
                /*判断view宽高*/
                if(w <= 0 || h <=0){
                    return
                }
                var left = 0
                var top = 0
                var right = w
                var bottom = h
                /*判断圆角位置*/
                if(side != SIDE_ALL){
                   when(side){
                       SIDE_LEFT -> right += radius
                       SIDE_TOP -> bottom += radius
                       SIDE_RIGHT -> left -= radius
                       SIDE_BOTTOM -> top -= radius
                   }
                    outline!!.setRoundRect(left,top,right,bottom,radius.toFloat())
                    return
                }

                if(radius <= 0){
                    outline!!.setRect(left,top,right,bottom)
                }else{
                    outline!!.setRoundRect(left,top,right,bottom, radius.toFloat())
                }
            }
        }
        view.clipToOutline = radius>0
        view.invalidate()
    }
}

自定义圆角view

class ConnerFrameLayout(context: Context, attrs: AttributeSet?, defStyleAttr: Int) :
    FrameLayout(context, attrs, defStyleAttr) {

    constructor(context: Context, attrs: AttributeSet?):this(context,attrs,-1)
    constructor(context: Context):this(context,null,-1)
    init {
        ViewHelper.setOutline(this,attrs)
    }

    fun setViewOutLine(radius: Int, side: Int){
        ViewHelper.setOutlineView(this,radius,side)
    }
}

使用

mShare.setOnClickListener {
            ShareDialog(this).show()
        }

自定义属性

  <!--view outline-->
    <declare-styleable name="ViewOutLine">
        <attr name="clip_radius" format="integer"/>
        <!--圆角方向-->
        <attr name="clip_side" format="enum">
            <enum name="all" value="0"/>
            <enum name="left" value="1"/>
            <enum name="top" value="2"/>
            <enum name="right" value="3"/>
            <enum name="bottom" value="4"/>
        </attr>
    </declare-styleable>

相关文章

  • Dialog

    安卓dialog的使用+如何自定义dialog自定义Dialog自定义Dialog 自定义

  • 自定义Dialog

    自定义Dialog的主题 自定义Dialog的布局文件 继承Dialog 并在onCreate方法中将布局设置给D...

  • 实现图片Dialog中带ViewPager

    效果图 实现思路 自定义Dialog,为Dialog添加自定义布局,自定义PagerAdapter以及PageTr...

  • 【Android】自定义全屏dialog

    一、在themes.xml中添加自定义dialog的样式 二、创建dialog基类 三、创建自定义dialog的布...

  • Android圆角对话框Dialog

    需求:模仿iOS样式Dialog对话框。 自定义Dialog 核心代码: Dialog样式: Dialog布局文件...

  • Android自定义Dialog及其点击事件

    在项目开发中,经常要用到dialog。但是系统的dialog太丑,所有我们要自定义dialog。下面的先介绍自定义...

  • 一个漂亮的自定义Dialog

    这是一个自定义的dialog项目 自定义的dialog,具有如下特点 圆角的dialog View 圆形图片的ti...

  • Flutter Dialog 动画

    本文对 Dialog 做一次系统性学习记录,包括系统 Dialog,自定义 Dialog,Dialog 动画。 A...

  • 自定义Dialog

    仿IOS自定义的Dialog: 1、Util帮助类创建dialog 2、布局文件 :loading_dialog....

  • 自定义Dialog实现透明无遮罩进度框

    效果图: 自定义Dialog继承自Dialog params.dimAmount=0:设置dialog弹出后,背景...

网友评论

      本文标题:自定义分享dialog

      本文链接:https://www.haomeiwen.com/subject/gcohjltx.html