LoginSignup
0
0

More than 3 years have passed since last update.

部分角丸カスタムView

Posted at

自分用のメモなので解説はないです。

RoundedCornersView.kt
import android.content.Context
import android.content.res.Resources
import android.graphics.Canvas
import android.graphics.Path
import android.graphics.RectF
import android.util.AttributeSet
import android.widget.FrameLayout

/**
 * 角丸View.
 */
class RoundedCornersView(context: Context, attrs: AttributeSet?) : FrameLayout(context, attrs) {

    private val round = 5.toPx.toFloat()
    private val path = Path()

    init {
        setWillNotDraw(false)
    }

    override fun onDraw(canvas: Canvas) {
        path.addRoundRect(getRectF(canvas), round, round, Path.Direction.CW)
        // StartTop, EndTopだけ角丸にする.
        path.addRoundRect(getRectF(canvas), floatArrayOf(10f, 10f, 10f, 10f, 0f, 0f, 0f, 0f), Path.Direction.CW)
        canvas.clipPath(path)
        super.onDraw(canvas)
    }

    private fun getRectF(canvas: Canvas): RectF = RectF(canvas.clipBounds)


    private val Int.toPx: Int
        get() = (this * Resources.getSystem().displayMetrics.density).toInt()
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0