Last active
June 27, 2018 16:14
-
-
Save damianpetla/a9e4d47d708185a10f8f7dd66cde0332 to your computer and use it in GitHub Desktop.
Fading TexView line
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
import android.graphics.Canvas | |
import android.graphics.Color | |
import android.graphics.Paint | |
import android.graphics.Rect | |
import android.graphics.drawable.GradientDrawable | |
import android.text.Layout | |
import android.text.style.LeadingMarginSpan | |
class FadeLineSpan(val fadingLine: Int, color: Int) : LeadingMarginSpan.LeadingMarginSpan2 { | |
val tops = linkedSetOf<Int>() | |
val gradient = GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM, arrayOf(Color.TRANSPARENT, color).toIntArray()) | |
override fun getLeadingMargin(first: Boolean): Int { | |
return 0 | |
} | |
override fun drawLeadingMargin(canvas: Canvas?, p: Paint?, x: Int, dir: Int, top: Int, baseline: Int, bottom: Int, text: CharSequence?, start: Int, end: Int, first: Boolean, layout: Layout?) { | |
tops.add(top) | |
if (tops.size >= fadingLine && tops.elementAt(fadingLine - 1) == top) { | |
canvas?.drawText(text, start, end, x.toFloat(), baseline.toFloat(), p) | |
gradient.bounds = Rect(x, top, canvas?.width!!, bottom) | |
gradient.draw(canvas) | |
canvas?.clipRect(x, 0, canvas.width, top) | |
} | |
} | |
override fun getLeadingMarginLineCount(): Int { | |
return 0 | |
} | |
} |
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
val textView = findViewById(R.id.textView) as TextView | |
val text = getString(R.string.text) | |
val builder = SpannableStringBuilder().append(text, FadeLineSpan(textView.maxLines, Color.WHITE), Spannable.SPAN_PARAGRAPH) | |
textView.text = builder |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
example I did for my app
