我想在TextView中旋转drawableLeft。
我试过这段代码:
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);
}
但是它给了我左侧drawable的位置一个空白区域。
实际上,即使这个最简单的代码也会给出空白:
Bitmap iconBitmap = ((BitmapDrawable)originalDrawable).getBitmap();
Drawable result = new BitmapDrawable(getResources(), iconBitmap);
setCompoundDrawables(result, null, null, null);
这个很好用:
setCompoundDrawables(originalDrawable, null, null, null);
答案 0 :(得分:4)
根据the docs,如果您想设置drawableLeft,请拨打setCompoundDrawablesWithIntrinsicBounds (int left, int top, int right, int bottom)。调用setCompoundDrawables()只有在Drawable上调用setBounds()时才有效,这可能就是你的originalDrawable可以工作的原因。
所以将代码更改为:
Drawable result = rotate(degree);
setCompoundDrawablesWithIntrinsicBounds(result, null, null, null);
答案 1 :(得分:0)
您不能只将Drawable
“投射”到BitmapDrawable
要将Drawable转换为Bitmap
,您必须“绘制”它,如下所示:
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);
见这里的帖子: