Android – set the rotated drawable to the drawableleft of textview

I want to rotate drawableleft in 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 area in the drawable position on the left

In fact, even the simplest code will give blank space:

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

This works normally:

 setCompoundDrawables(originalDrawable, null, null, null);

resolvent:

According to the docs, if you want to set drawableleft, you should call setcompounddrawableswithinterinsicbounds (int left, int top, int right, int bottom). Calling setcompounddrawables() is only valid when calling setbounds() on drawable, which may be the reason for your originaldrawable work

So change the code to:

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

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>