Skip to content

Instantly share code, notes, and snippets.

@hrules6872
Last active September 8, 2022 12:22
Show Gist options
  • Star 16 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save hrules6872/578b52fe90c30c7445a2 to your computer and use it in GitHub Desktop.
Save hrules6872/578b52fe90c30c7445a2 to your computer and use it in GitHub Desktop.
TextViewDrawableSize - CompoundDrawable size
<resources>
<declare-styleable name="TextViewDrawableSize">
<attr name="compoundDrawableWidth" format="dimension"/>
<attr name="compoundDrawableHeight" format="dimension"/>
</declare-styleable>
</resources>
public class TextViewDrawableSize extends TextView {
private static final int DEFAULT_COMPOUND_DRAWABLE_SIZE = -1;
private int compoundDrawableWidth;
private int compoundDrawableHeight;
public TextViewDrawableSize(Context context) {
this(context, null);
}
public TextViewDrawableSize(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public TextViewDrawableSize(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context, attrs);
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public TextViewDrawableSize(Context context, AttributeSet attrs, int defStyleAttr,
int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
init(context, attrs);
}
private void init(Context context, AttributeSet attrs) {
final TypedArray typedArray =
context.obtainStyledAttributes(attrs, R.styleable.TextViewDrawableSize);
compoundDrawableWidth =
typedArray.getDimensionPixelSize(R.styleable.TextViewDrawableSize_compoundDrawableWidth,
DEFAULT_COMPOUND_DRAWABLE_SIZE);
compoundDrawableHeight =
typedArray.getDimensionPixelSize(R.styleable.TextViewDrawableSize_compoundDrawableHeight,
DEFAULT_COMPOUND_DRAWABLE_SIZE);
typedArray.recycle();
resizeCompoundDrawables();
}
private void resizeCompoundDrawables() {
Drawable[] drawables = getCompoundDrawables();
if (compoundDrawableWidth > 0 || compoundDrawableHeight > 0) {
for (Drawable drawable : drawables) {
if (drawable == null) {
continue;
}
Rect realBounds = drawable.getBounds();
float scaleFactor = realBounds.height() / (float) realBounds.width();
float drawableWidth = realBounds.width();
float drawableHeight = realBounds.height();
if (this.compoundDrawableWidth > 0) {
if (drawableWidth > this.compoundDrawableWidth) {
drawableWidth = this.compoundDrawableWidth;
drawableHeight = drawableWidth * scaleFactor;
}
}
if (this.compoundDrawableHeight > 0) {
if (drawableHeight > this.compoundDrawableHeight) {
drawableHeight = this.compoundDrawableHeight;
drawableWidth = drawableHeight / scaleFactor;
}
}
realBounds.right = realBounds.left + Math.round(drawableWidth);
realBounds.bottom = realBounds.top + Math.round(drawableHeight);
drawable.setBounds(realBounds);
}
}
super.setCompoundDrawables(drawables[0], drawables[1], drawables[2], drawables[3]);
}
public int getCompoundDrawableWidth() {
return compoundDrawableWidth;
}
public void setCompoundDrawableWidth(int compoundDrawableWidth) {
this.compoundDrawableWidth = compoundDrawableWidth;
}
public int getCompoundDrawableHeight() {
return compoundDrawableHeight;
}
public void setCompoundDrawableHeight(int compoundDrawableHeight) {
this.compoundDrawableHeight = compoundDrawableHeight;
}
@Override
public void setCompoundDrawables(Drawable left, Drawable top, Drawable right, Drawable bottom) {
super.setCompoundDrawables(left, top, right, bottom);
resizeCompoundDrawables();
}
@Override
public void setCompoundDrawablesWithIntrinsicBounds(int left, int top, int right, int bottom) {
super.setCompoundDrawablesWithIntrinsicBounds(left, top, right, bottom);
resizeCompoundDrawables();
}
@Override
public void setCompoundDrawablesWithIntrinsicBounds(Drawable left, Drawable top, Drawable right,
Drawable bottom) {
super.setCompoundDrawablesWithIntrinsicBounds(left, top, right, bottom);
resizeCompoundDrawables();
}
@Override public void setCompoundDrawablesRelative(Drawable start, Drawable top, Drawable end,
Drawable bottom) {
super.setCompoundDrawablesRelative(start, top, end, bottom);
resizeCompoundDrawables();
}
@Override public void setCompoundDrawablesRelativeWithIntrinsicBounds(int start, int top, int end,
int bottom) {
super.setCompoundDrawablesRelativeWithIntrinsicBounds(start, top, end, bottom);
resizeCompoundDrawables();
}
@Override
public void setCompoundDrawablesRelativeWithIntrinsicBounds(Drawable start, Drawable top,
Drawable end, Drawable bottom) {
super.setCompoundDrawablesRelativeWithIntrinsicBounds(start, top, end, bottom);
resizeCompoundDrawables();
}
}
@hrules6872
Copy link
Author

Usage activity_main.xml:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

  <com.hrules.TextViewDrawableSize
      android:text="@string/hello_world"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:gravity="center_vertical"
      android:drawableLeft="@drawable/ic_launcher"
      android:drawableStart="@drawable/ic_launcher"
      android:drawablePadding="8dp"
      app:compoundDrawableHeight="20dp"
      app:compoundDrawableWidth="20dp"
      />
</LinearLayout>

+info: CompoundDrawable size - StackOverFlow

@dukerok
Copy link

dukerok commented Feb 17, 2016

resizeCompoundDrawables(); calls multiple times when u call one of setCompound methods. Look at super.

@mishaoshuai
Copy link

mishaoshuai commented Jul 12, 2016

I 've tried this,but it didn't work very well when I set the "background" with a "selector",the selector set the "drawable" two different effects,clicked and normal,but every time ,the picture won't resize util I click it .would you please help me

@AbandonedCart
Copy link

AbandonedCart commented Mar 10, 2020

  public TextViewDrawableSize(Context context) {
    this(context, null);
  }

  public TextViewDrawableSize(Context context, AttributeSet attrs) {
    this(context, attrs, 0);
  }

  public TextViewDrawableSize(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    init(context, attrs);
  }

This saves 2 lines of code at the cost of causing a "leaky faucet" so to speak.

  public TextViewDrawableSize(Context context) {
    super(context);
    init(context, null);
  }

  public TextViewDrawableSize(Context context, AttributeSet attrs) {
    super(context, attrs);
    init(context, attrs);
  }

  public TextViewDrawableSize(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    init(context, attrs);
  }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment