Created
August 25, 2019 16:51
-
-
Save IMoHaMeDHaMdYI/78048a1ca90cdf4468610a86808ad5c1 to your computer and use it in GitHub Desktop.
Basic CardView
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class CardView(context: Context, attributeSet: AttributeSet) : View(context, attributeSet) { | |
private var outerWidth = 0f | |
private var outerHeight = 0f | |
private var xCenter = 0f | |
private var yCenter = 0f | |
private var cornerRadius = 10f | |
private var shadowSize = 50f | |
private val shadowStartColor = 0x20000000 | |
private val shadowEndColor = 0x00000000 | |
private val linearGradientPaint = Paint() | |
@SuppressLint("DrawAllocation") | |
override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) { | |
outerWidth = View.MeasureSpec.getSize(widthMeasureSpec).toFloat() | |
outerHeight = View.MeasureSpec.getSize(heightMeasureSpec).toFloat() | |
xCenter = outerWidth / 2f | |
yCenter = outerHeight / 2f | |
radialGradientPaint.apply { | |
isAntiAlias = true | |
strokeJoin = Paint.Join.ROUND | |
style = Paint.Style.FILL | |
val startRatio = (shadowSize / 4 + cornerRadius) / (cornerRadius + shadowSize) | |
Log.d(this::class.java.simpleName, "start ration $startRatio") | |
shader = RadialGradient( | |
shadowSize + cornerRadius, | |
shadowSize + cornerRadius, | |
shadowSize + cornerRadius, | |
intArrayOf(shadowStartColor, shadowStartColor, shadowEndColor), | |
floatArrayOf(0f, startRatio, 1f), | |
Shader.TileMode.CLAMP | |
) | |
isDither = true | |
} | |
linearGradientPaint.apply { | |
isAntiAlias = true | |
strokeJoin = Paint.Join.ROUND | |
style = Paint.Style.FILL | |
shader = LinearGradient( | |
xCenter, | |
shadowSize, | |
xCenter, | |
0f, | |
intArrayOf(shadowStartColor, shadowStartColor, shadowEndColor), | |
floatArrayOf(0f, 0.25f, 1f), | |
Shader.TileMode.CLAMP | |
) | |
isDither = true | |
} | |
super.onMeasure(widthMeasureSpec, heightMeasureSpec) | |
} | |
override fun onDraw(canvas: Canvas) { | |
super.onDraw(canvas) | |
val path = Path() | |
path.apply { | |
moveTo(shadowSize + cornerRadius, shadowSize + cornerRadius) | |
arcTo(0f, 0f, shadowSize * 2 + 2 * cornerRadius, shadowSize * 2 + 2 * cornerRadius, 180f, 90f, false) | |
moveTo(shadowSize + cornerRadius, shadowSize + cornerRadius) | |
arcTo(shadowSize, shadowSize, shadowSize + 2 * cornerRadius, shadowSize + 2 * cornerRadius, 270f, -90f, false) | |
} | |
canvas.drawPath(path, radialGradientPaint) | |
canvas.drawRect(RectF(cornerRadius + shadowSize, 0f, outerWidth - cornerRadius - shadowSize, shadowSize), linearGradientPaint) | |
canvas.save() | |
canvas.rotate(180f, xCenter, yCenter) | |
canvas.drawRect(RectF(cornerRadius + shadowSize, 0f, outerWidth - cornerRadius - shadowSize, shadowSize), linearGradientPaint) | |
canvas.drawPath(path, radialGradientPaint) | |
canvas.restore() | |
canvas.save() | |
canvas.rotate(90f, xCenter, yCenter) | |
canvas.drawRect(RectF(cornerRadius + shadowSize, 0f, outerHeight - cornerRadius - shadowSize, shadowSize), linearGradientPaint) | |
canvas.drawPath(path, radialGradientPaint) | |
canvas.restore() | |
canvas.rotate(270f, xCenter, yCenter) | |
canvas.drawRect(RectF(cornerRadius + shadowSize, 0f, outerHeight - cornerRadius - shadowSize, shadowSize), linearGradientPaint) | |
canvas.drawPath(path, radialGradientPaint) | |
} | |
private val radialGradientPaint: Paint = Paint() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment