Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set rotated drawable as TextView's drawableLeft

Tags:

android

image

I want to rotate drawableLeft in a TextView.

I tried this code:

Drawable result = rotate(degree);
setCompoundDrawables(result, null, null, null);

private Drawable rotate(int degree)
{
    Bitmap iconBitmap = ((BitmapDrawable)originalDrawable).getBitmap();

    Matrix matrix = new Matrix();
    matrix.postRotate(degree);
    Bitmap targetBitmap = Bitmap.createBitmap(iconBitmap, 0, 0, iconBitmap.getWidth(), iconBitmap.getHeight(), matrix, true);

    return new BitmapDrawable(getResources(), targetBitmap);
}

But it gives me a blank space on the left drawable's place.

Actually, even this simplest code gives blank space:

Bitmap iconBitmap = ((BitmapDrawable)originalDrawable).getBitmap();
Drawable result = new BitmapDrawable(getResources(), iconBitmap);
setCompoundDrawables(result, null, null, null);

This one works fine:

 setCompoundDrawables(originalDrawable, null, null, null);
like image 704
TpoM6oH Avatar asked Jan 15 '15 13:01

TpoM6oH


1 Answers

According to the docs, if you want to set drawableLeft you should call setCompoundDrawablesWithIntrinsicBounds (int left, int top, int right, int bottom). Calling setCompoundDrawables() only works if setBounds() has been called on the Drawable, which is probably why your originalDrawable works.

So change your code to:

Drawable result = rotate(degree);
setCompoundDrawablesWithIntrinsicBounds(result, null, null, null);
like image 76
samgak Avatar answered Nov 02 '22 14:11

samgak