Created
September 8, 2018 05:21
-
-
Save kavan-mevada/f25bb4f3d4455cecf22c3a50642905a5 to your computer and use it in GitHub Desktop.
Chip like TextView [ Rounded Corners ]
This file contains hidden or 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 /* Package Name */ | |
import android.content.Context | |
import android.graphics.* | |
import android.util.AttributeSet | |
import android.widget.TextView | |
class TextViewChip(context: Context, attrs: AttributeSet) : TextView(context, attrs) { | |
private val path = Path() | |
var newWidth = 0 | |
override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) { | |
super.setPadding((height/2f).toInt(), paddingTop, (height/2f).toInt(), paddingBottom) | |
super.onSizeChanged(w, h, oldw, oldh) | |
// compute the path | |
val radius = h / 2f | |
path.reset() | |
path.addRoundRect(RectF(0F, 0F, width.toFloat(), height.toFloat()), radius, radius, Path.Direction.CW) | |
path.close() | |
} | |
override fun draw(canvas: Canvas) { | |
if (newWidth<=0){ | |
newWidth = measuredWidth+height | |
width = newWidth | |
} | |
val flag = canvas.save() | |
canvas.clipPath(path) | |
super.draw(canvas) | |
canvas.restoreToCount(flag) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment