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);
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);
You can't just "cast" a Drawable
to a BitmapDrawable
To convert a Drawable to a Bitmap
you have to "draw" it, like this:
Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
drawable.draw(canvas);
See the post here:
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.