美文网首页
文字气泡背景

文字气泡背景

作者: WLHere | 来源:发表于2020-09-10 09:07 被阅读0次

实现效果

image.png

xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:elevation="0dp"
    android:gravity="center"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingLeft="7dp"
        android:paddingTop="2dp"
        android:paddingRight="7dp"
        android:paddingBottom="5dp"
        android:text="Hello world" />

</LinearLayout>

code

findViewById<View>(R.id.view).background = PopupTextBg()
class PopupTextBg : Drawable() {
    private val mPath = Path()
    private val mPaint = Paint(Paint.ANTI_ALIAS_FLAG).also {
        it.style = Paint.Style.FILL
    }
    private val mStartColor = 0xffaaff00.toInt()
    private val mEndColor = 0xff00ff00.toInt()
    private val mGapWidth = DensityUtil.dip2px(App.instance, 8f)
    private val mGapHeight = DensityUtil.dip2px(App.instance, 5f)

    override fun draw(canvas: Canvas) {
        val bottom = (canvas.height - mGapHeight).toFloat()
        val radius = bottom / 2f
        mPath.reset()
        mPath.moveTo(radius, 0f)
        mPath.lineTo(canvas.width - radius, 0f)
        mPath.arcTo(RectF(canvas.width - radius * 2, 0f, canvas.width.toFloat(), bottom), 270f, 180f)
        // 三角
        mPath.lineTo((canvas.width + mGapWidth) / 2f, bottom)
        mPath.lineTo((canvas.width) / 2f, bottom + mGapHeight)
        mPath.lineTo((canvas.width - mGapWidth) / 2f, bottom)

        mPath.lineTo(radius, bottom)
        mPath.arcTo(RectF(0f, 0f, radius * 2, bottom), 90f, 180f)
        mPath.close()

        mPaint.shader = LinearGradient(0f, 0f, canvas.width.toFloat(), 0f, mStartColor, mEndColor, Shader.TileMode.CLAMP)
        canvas.drawPath(mPath, mPaint)
    }

    override fun setAlpha(alpha: Int) {
    }

    override fun getOpacity(): Int {
        return PixelFormat.OPAQUE
    }

    override fun setColorFilter(colorFilter: ColorFilter?) {
    }

相关文章

网友评论

      本文标题:文字气泡背景

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