|
| 1 | +package com.dvinfosys.widgets.TextView; |
| 2 | + |
| 3 | +import android.annotation.TargetApi; |
| 4 | +import android.content.Context; |
| 5 | +import android.content.res.TypedArray; |
| 6 | +import android.graphics.LinearGradient; |
| 7 | +import android.graphics.Shader; |
| 8 | +import android.graphics.Typeface; |
| 9 | +import android.os.Build; |
| 10 | +import android.util.AttributeSet; |
| 11 | +import android.widget.TextView; |
| 12 | + |
| 13 | +import com.dvinfosys.widgets.R; |
| 14 | +import com.dvinfosys.widgets.Utils.TextFontCache; |
| 15 | + |
| 16 | +/** |
| 17 | + * Created by DV Bhuva on 27/06/19. |
| 18 | + */ |
| 19 | + |
| 20 | +public class GradientTextView extends TextView { |
| 21 | + |
| 22 | + int startColor, endColor; |
| 23 | + Shader shader; |
| 24 | + |
| 25 | + public GradientTextView(Context context) { |
| 26 | + super(context); |
| 27 | + } |
| 28 | + |
| 29 | + public GradientTextView(Context context, AttributeSet attrs) { |
| 30 | + super(context, attrs); |
| 31 | + if (!isInEditMode()) |
| 32 | + createShader(context, attrs, 0); |
| 33 | + } |
| 34 | + |
| 35 | + public GradientTextView(Context context, AttributeSet attrs, int defStyleAttr) { |
| 36 | + super(context, attrs, defStyleAttr); |
| 37 | + if (!isInEditMode()) |
| 38 | + createShader(context, attrs, defStyleAttr); |
| 39 | + } |
| 40 | + |
| 41 | + @TargetApi(Build.VERSION_CODES.LOLLIPOP) |
| 42 | + public GradientTextView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { |
| 43 | + super(context, attrs, defStyleAttr, defStyleRes); |
| 44 | + if (!isInEditMode()) |
| 45 | + createShader(context, attrs, defStyleAttr); |
| 46 | + } |
| 47 | + |
| 48 | + |
| 49 | + public void createShader(Context context, AttributeSet attrs, int defStyleAttr) { |
| 50 | + TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.GradientTextView, defStyleAttr, 0); |
| 51 | + try { |
| 52 | + startColor = a.getInt(R.styleable.GradientTextView_gradientStart, 0); |
| 53 | + endColor = a.getInt(R.styleable.GradientTextView_gradientEnd, 0); |
| 54 | + shader = new LinearGradient(0, 0, 0, getTextSize(), |
| 55 | + new int[]{startColor, endColor}, |
| 56 | + new float[]{0, 1}, Shader.TileMode.CLAMP); |
| 57 | + getPaint().setShader(shader); |
| 58 | + } finally { |
| 59 | + a.recycle(); |
| 60 | + } |
| 61 | + Typeface customFont = TextFontCache.getTypeface("Poppins.ttf", context); |
| 62 | + setTypeface(customFont); |
| 63 | + } |
| 64 | +} |
0 commit comments