Android各种圆角的实现

22 篇文章 0 订阅
8 篇文章 0 订阅

1、普通的控件使用sharp corners ,设置背景实现圆角

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid
        android:width="1dp"
        android:color="#F6F7FC" />
    <corners android:radius="15dp" />
</shape>

2、webview圆角,自定义webview

import android.content.Context
import android.content.res.Configuration
import android.graphics.*
import android.os.Build
import android.util.AttributeSet
import android.webkit.WebView
import androidx.annotation.RequiresApi

class LollipopFixedWebView : WebView {
    constructor(context: Context) : super(
        getFixedContext(
            context
        )
    ) {
        init()
    }

    constructor(context: Context, attrs: AttributeSet?) : super(
        getFixedContext(context),
        attrs
    ) {
        init()
    }

    constructor(
        context: Context,
        attrs: AttributeSet?,
        defStyleAttr: Int
    ) : super(getFixedContext(context), attrs, defStyleAttr) {
        init()
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    constructor(
        context: Context,
        attrs: AttributeSet?,
        defStyleAttr: Int,
        defStyleRes: Int
    ) : super(
        getFixedContext(context),
        attrs,
        defStyleAttr,
        defStyleRes
    ) {
        init()
    }

    companion object {
        fun getFixedContext(context: Context): Context {
            return if (Build.VERSION.SDK_INT in 21..22) {
                context.createConfigurationContext(Configuration())
            } else
                context
        }
    }

    private var vRadius = 15f
    private var vWidth = 0
    private var vHeight = 0
    private var x = 0
    private var y = 0
    private var paint1: Paint? = null
    private var paint2: Paint? = null

    private fun init() {
        paint1 = Paint()
        paint1!!.color = Color.WHITE
        paint1!!.isAntiAlias = true
        paint1!!.xfermode = PorterDuffXfermode(PorterDuff.Mode.DST_OUT)
        //
        paint2 = Paint()
        paint2!!.xfermode = null
    }

    fun setRadius(radius: Float) {
        vRadius = radius
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        vWidth = measuredWidth
        vHeight = measuredHeight
    }

    override fun draw(canvas: Canvas) {
        x = this.scrollX
        y = this.scrollY
        val bitmap = Bitmap.createBitmap(
            x + vWidth, y + vHeight,
            Bitmap.Config.ARGB_8888
        )
        val canvas2 = Canvas(bitmap)
        super.draw(canvas2)
        drawLeftUp(canvas2)
        drawRightUp(canvas2)
        drawLeftDown(canvas2)
        drawRightDown(canvas2)
        canvas.drawBitmap(bitmap, 0f, 0f, paint2)
        bitmap.recycle()
    }

    private fun drawLeftUp(canvas: Canvas) {
        val path = Path()
        path.moveTo(x.toFloat(), vRadius)
        path.lineTo(x.toFloat(), y.toFloat())
        path.lineTo(vRadius, y.toFloat())
        path.arcTo(RectF(x.toFloat(), y.toFloat(), x + vRadius * 2, y + vRadius * 2), -90f, -90f)
        path.close()
        canvas.drawPath(path, paint1!!)
    }


    private fun drawLeftDown(canvas: Canvas) {
        val path = Path()
        path.moveTo(x.toFloat(), y + vHeight - vRadius)
        path.lineTo(x.toFloat(), y.toFloat() + vHeight)
        path.lineTo(x + vRadius, y.toFloat() + vHeight)
        path.arcTo(
            RectF(
                x.toFloat(), y + vHeight - vRadius * 2,
                x + vRadius * 2, (y + vHeight).toFloat()
            ), 90f, 90f
        )
        path.close()
        canvas.drawPath(path, paint1!!)
    }


    private fun drawRightDown(canvas: Canvas) {
        val path = Path()
        path.moveTo(x + vWidth - vRadius, y.toFloat() + vHeight)
        path.lineTo(x.toFloat() + vWidth, y.toFloat() + vHeight)
        path.lineTo(x.toFloat() + vWidth, y + vHeight - vRadius)
        path.arcTo(
            RectF(
                x + vWidth - vRadius * 2, y + vHeight
                        - vRadius * 2, (x + vWidth).toFloat(), (y + vHeight).toFloat()
            ), 0f, 90f
        )
        path.close()
        canvas.drawPath(path, paint1!!)
    }


    private fun drawRightUp(canvas: Canvas) {
        val path = Path()
        path.moveTo(x.toFloat() + vWidth, y + vRadius)
        path.lineTo(x.toFloat() + vWidth, y.toFloat())
        path.lineTo(x + vWidth - vRadius, y.toFloat())
        path.arcTo(
            RectF(
                x + vWidth - vRadius * 2, y.toFloat(), (x + vWidth).toFloat(),
                y + vRadius * 2
            ), -90f, 90f
        )
        path.close()
        canvas.drawPath(path, paint1!!)
    }
}

3、glide网络加载图片,圆角实现

override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val roundedCorners = RoundedCorners(10)
        Glide.with(context)
            .load(list[realPosition].coverImage)
            .apply(
                RequestOptions.diskCacheStrategyOf(DiskCacheStrategy.AUTOMATIC)
                    .transform(roundedCorners)
                    .override(300, 300)
            )
            .into(holder.imageView)
}

4、Android使用ViewOutlineProvider实现圆角Android可以通过设置 View#setOutlineProvider方法来设置轮廓。

/**
 * @param radius 圆角半径
 */
class RoundRectOutlineProvider(private val radius: Float) : ViewOutlineProvider() {

    override fun getOutline(view: View, outline: Outline) {
        outline.setRoundRect(0, 0, view.width, view.height, radius)
    }
}

使用方法如下:

            val coverOutline = RoundRectOutlineProvider(3.px.toFloat())
            view.outlineProvider = coverOutline
            view.clipToOutline = true

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值