Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CompoundDrawable size

Tags:

android

Android-Lint gives me this hint in some cases:

This tag and its children can be replaced by one and a compound drawable

I could do it on some places, but on other places where scaling the image is important I am not able to do it. Is there any option that i can set the size for a compound drawable?

like image 994
ligi Avatar asked Feb 06 '12 22:02

ligi


People also ask

How do I change the drawableLeft icon size on a Textview?

You can control the size of the icon by changing scaleX and scaleY. This is useful not only in case of drawables but any place where an icon is used and where it is not easy to find styling solutions to reduce icon size like say an AlertDialogue. Show activity on this post.


2 Answers

I finally released a library for it:

https://github.com/a-tolstykh/textview-rich-drawable

Use this library to set size of compound drawable.

Original answer

You can add custom attribute to your view to work with size of drawable. Of course, this solution requires additional code, but only once! After you can solve the problem using xml only (only once change the control and use it in all project).

See my example:

TextViewDrawableSize.java

 public class TextViewDrawableSize extends TextView {      private int mDrawableWidth;     private int mDrawableHeight;      public TextViewDrawableSize(Context context) {         super(context);         init(context, null, 0, 0);     }      public TextViewDrawableSize(Context context, AttributeSet attrs) {         super(context, attrs);         init(context, attrs, 0, 0);     }      public TextViewDrawableSize(Context context, AttributeSet attrs, int defStyleAttr) {         super(context, attrs, defStyleAttr);         init(context, attrs, defStyleAttr, 0);     }      @TargetApi(Build.VERSION_CODES.LOLLIPOP)     public TextViewDrawableSize(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {         super(context, attrs, defStyleAttr, defStyleRes);         init(context, attrs, defStyleAttr, defStyleRes);     }      private void init(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {         TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.TextViewDrawableSize, defStyleAttr, defStyleRes);          try {             mDrawableWidth = array.getDimensionPixelSize(R.styleable.TextViewDrawableSize_compoundDrawableWidth, -1);             mDrawableHeight = array.getDimensionPixelSize(R.styleable.TextViewDrawableSize_compoundDrawableHeight, -1);         } finally {             array.recycle();         }          if (mDrawableWidth > 0 || mDrawableHeight > 0) {             initCompoundDrawableSize();         }     }      private void initCompoundDrawableSize() {         Drawable[] drawables = getCompoundDrawables();         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 (mDrawableWidth > 0) {                 // save scale factor of image                 if (drawableWidth > mDrawableWidth) {                     drawableWidth = mDrawableWidth;                     drawableHeight = drawableWidth * scaleFactor;                 }             }             if (mDrawableHeight > 0) {                 // save scale factor of image                  if (drawableHeight > mDrawableHeight) {                     drawableHeight = mDrawableHeight;                     drawableWidth = drawableHeight / scaleFactor;                 }             }              realBounds.right = realBounds.left + Math.round(drawableWidth);             realBounds.bottom = realBounds.top + Math.round(drawableHeight);              drawable.setBounds(realBounds);         }         setCompoundDrawables(drawables[0], drawables[1], drawables[2], drawables[3]);     } }

attrs.xml

<resources>     <declare-styleable name="TextViewDrawableSize">         <attr name="compoundDrawableWidth" format="dimension"/>         <attr name="compoundDrawableHeight" format="dimension"/>     </declare-styleable> </resources> 

And usage

    <com.alt.view.TextViewDrawableSize         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:drawableLeft="@drawable/logo"         android:drawableStart="@drawable/logo"         android:drawablePadding="@dimen/space_micro"         app:compoundDrawableHeight="@dimen/small_logo_height"         app:compoundDrawableWidth="@dimen/small_logo_width"         /> 
like image 127
Oleksandr Avatar answered Oct 07 '22 18:10

Oleksandr


If you scale the images in code as Drawable objects, you can then set them in code using the setCompoundDrawables() method of TextView. This will require having called setBounds() on the Drawables.

As far as I know there is no way to set the size in XML.

like image 20
onit Avatar answered Oct 07 '22 18:10

onit