Created
December 19, 2018 00:20
-
-
Save fededri/eb57662c7920cda9a7969b979b1a63d8 to your computer and use it in GitHub Desktop.
Heart View made with bezier curves
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
package com.fedetorres.beziercurves | |
import android.content.Context | |
import android.graphics.* | |
import android.util.AttributeSet | |
import android.view.View | |
/** | |
* Federico Torres | |
* Heart view using bezier curves | |
*/ | |
class HeartView : View { | |
constructor(context: Context) : super(context) | |
constructor(context: Context, attrs: AttributeSet) : super(context, attrs) | |
val curvePaint = Paint(Paint.ANTI_ALIAS_FLAG) | |
val textPaint = Paint() | |
val circlePath = Path() | |
var ww: Float = 0f | |
var hh: Float = 0f | |
var center: PointF = PointF() | |
val p0: PointF = PointF() // top position where right and left sides begins | |
//End point of both sides | |
val p4 = PointF() | |
//Right side controller points | |
val p1Control = PointF() | |
val p2Control = PointF() | |
//Left side controller points | |
val p5Control = PointF() | |
val p6Control = PointF() | |
init { | |
curvePaint.style = Paint.Style.FILL | |
curvePaint.color = Color.RED | |
curvePaint.strokeWidth = 8f | |
textPaint.strokeWidth = 1f | |
textPaint.textSize = 55f | |
textPaint.typeface = Typeface.SANS_SERIF | |
} | |
override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) { | |
super.onSizeChanged(w, h, oldw, oldh) | |
this.ww = w.toFloat() | |
this.hh = h.toFloat() | |
center = PointF((ww / 2), (hh / 2)) | |
p0.x = ww * 0.5f | |
p0.y = hh * 0.25f | |
p4.x = center.x | |
p4.y = hh | |
p1Control.x = ww * 0.8f | |
p1Control.y = 0f | |
p2Control.x = ww * 0.75f | |
p2Control.y = hh * 0.8f | |
p5Control.x = ww * 0.2f | |
p5Control.y = 0f | |
p6Control.x = ww * 0.25f | |
p6Control.y = hh * 0.8f | |
} | |
override fun onDraw(canvas: Canvas?) { | |
circlePath.moveTo(p0.x, p0.y) | |
circlePath.cubicTo(p1Control.x, p1Control.y, p2Control.x, p2Control.y, p4.x, p4.y) | |
circlePath.moveTo(p0.x, p0.y) | |
circlePath.cubicTo(p5Control.x, p5Control.y, p6Control.x, p6Control.y, p4.x, p4.y) | |
canvas?.drawColor(Color.WHITE) | |
canvas?.drawPath(circlePath, curvePaint) | |
canvas?.drawTextOnPath("text",circlePath,300f,-40f,textPaint) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment