Guest User

Untitled

a guest
Apr 25th, 2018
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.21 KB | None | 0 0
  1. import android.content.Context;
  2. import android.content.res.TypedArray;
  3. import android.graphics.PorterDuff;
  4. import android.graphics.Typeface;
  5. import android.graphics.drawable.Drawable;
  6. import android.os.Build;
  7. import android.support.v4.graphics.drawable.DrawableCompat;
  8. import android.support.v7.content.res.AppCompatResources;
  9. import android.support.v7.widget.AppCompatTextView;
  10. import android.text.TextUtils;
  11. import android.util.AttributeSet;
  12.  
  13. public class ButtonTextView extends AppCompatTextView {
  14.  
  15. public ButtonTextView(Context context, AttributeSet attrs) {
  16. super(context, attrs);
  17. init(attrs, context);
  18. }
  19.  
  20. public ButtonTextView(Context context, AttributeSet attrs, int defStyleAttr) {
  21. super(context, attrs, defStyleAttr);
  22. init(attrs, context);
  23. }
  24.  
  25. private void init(AttributeSet attrs, Context context) {
  26. if (attrs != null) {
  27. TypedArray a = getContext().getTheme().obtainStyledAttributes(attrs, R.styleable.ButtonTextView, 0, 0);
  28.  
  29. int colorFilter = a.getColor(R.styleable.ButtonTextView_colorFilter, 0xffffffff);
  30.  
  31. Drawable drawableLeft = null;
  32. Drawable drawableRight = null;
  33. Drawable drawableBottom = null;
  34. Drawable drawableTop = null;
  35.  
  36. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
  37.  
  38. drawableLeft = a.getDrawable(R.styleable.ButtonTextView_drawableLeftCompat);
  39.  
  40. drawableRight = a.getDrawable(R.styleable.ButtonTextView_drawableRightCompat);
  41.  
  42. drawableTop = a.getDrawable(R.styleable.ButtonTextView_drawableTopCompat);
  43.  
  44. drawableBottom = a.getDrawable(R.styleable.ButtonTextView_drawableBottomCompat);
  45.  
  46. } else {
  47. final int drawableLeftId = a.getResourceId(R.styleable.ButtonTextView_drawableLeftCompat, 0);
  48.  
  49. final int drawableRightId = a.getResourceId(R.styleable.ButtonTextView_drawableRightCompat, 0);
  50.  
  51. final int drawableTopId = a.getResourceId(R.styleable.ButtonTextView_drawableTopCompat, 0);
  52.  
  53. final int drawableBottomId = a.getResourceId(R.styleable.ButtonTextView_drawableBottomCompat, 0);
  54.  
  55. if (drawableLeftId != 0) {
  56. drawableLeft = AppCompatResources.getDrawable(context, drawableLeftId);
  57. }
  58.  
  59. if (drawableRightId != 0) {
  60. drawableRight = AppCompatResources.getDrawable(context, drawableRightId);
  61. }
  62.  
  63. if (drawableTopId != 0) {
  64. drawableTop = AppCompatResources.getDrawable(context, drawableTopId);
  65. }
  66.  
  67. if (drawableBottomId != 0) {
  68. drawableBottom = AppCompatResources.getDrawable(context, drawableBottomId);
  69. }
  70. }
  71.  
  72. if (drawableLeft != null) {
  73. if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
  74. drawableLeft.mutate().setColorFilter(colorFilter, PorterDuff.Mode.SRC_IN);
  75. } else {
  76. DrawableCompat.setTint(drawableLeft, colorFilter);
  77. }
  78. }
  79.  
  80. if (drawableRight != null) {
  81. if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
  82. drawableRight.mutate().setColorFilter(colorFilter, PorterDuff.Mode.SRC_IN);
  83. } else {
  84. DrawableCompat.setTint(drawableRight, colorFilter);
  85. }
  86. }
  87.  
  88.  
  89. if (drawableTop != null) {
  90. if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
  91. drawableTop.mutate().setColorFilter(colorFilter, PorterDuff.Mode.SRC_IN);
  92. } else {
  93. DrawableCompat.setTint(drawableTop, colorFilter);
  94. }
  95. }
  96.  
  97. if (drawableBottom != null) {
  98. if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
  99. drawableBottom.mutate().setColorFilter(colorFilter, PorterDuff.Mode.SRC_IN);
  100. } else {
  101. DrawableCompat.setTint(drawableBottom, colorFilter);
  102. }
  103. }
  104.  
  105. if (drawableLeft != null) {
  106. drawableLeft.setBounds(0, 0, drawableLeft.getIntrinsicWidth(), drawableLeft.getIntrinsicHeight());
  107. }
  108.  
  109. if (drawableRight != null) {
  110. drawableRight.setBounds(0, 0, drawableRight.getIntrinsicWidth(), drawableRight.getIntrinsicHeight());
  111. }
  112.  
  113. if (drawableTop != null) {
  114. drawableTop.setBounds(0, 0, drawableTop.getIntrinsicWidth(), drawableTop.getIntrinsicHeight());
  115. }
  116.  
  117. if (drawableBottom != null) {
  118. drawableBottom.setBounds(0, 0, drawableBottom.getIntrinsicWidth(), drawableBottom.getIntrinsicHeight());
  119. }
  120.  
  121. setCompoundDrawablesRelative(drawableLeft, drawableTop, drawableRight, drawableBottom);
  122.  
  123. String fontName = a.getString(R.styleable.ButtonTextView_font_name);
  124. if (!TextUtils.isEmpty(fontName)) {
  125. Typeface tf = Typeface.createFromAsset(getContext().getAssets(),
  126. "fonts/".concat(fontName));
  127. setTypeface(tf);
  128. }
  129.  
  130. a.recycle();
  131. }
  132. }
  133. }
Add Comment
Please, Sign In to add comment