android – 将旋转的drawable设置为TextView的drawableLeft

android – 将旋转的drawable设置为TextView的drawableLeft,第1张

概述我想在TextView中旋转drawableLeft. 我试过这段代码: Drawable result = rotate(degree);setCompoundDrawables(result, null, null, null);private Drawable rotate(int degree){ Bitmap iconBitmap = ((BitmapDrawable)o 我想在TextVIEw中旋转drawableleft.

我试过这段代码:

Drawable result = rotate(degree);setCompoundDrawables(result,null,null);private Drawable rotate(int degree){    Bitmap iconBitmap = ((BitmapDrawable)originalDrawable).getBitmap();    Matrix matrix = new Matrix();    matrix.postRotate(degree);    Bitmap targetBitmap = Bitmap.createBitmap(iconBitmap,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);

这个工作正常:

setCompoundDrawables(originalDrawable,null);
解决方法 根据 the docs,如果你想设置drawableleft,你应该调用 setCompoundDrawablesWithIntrinsicBounds (int left,int top,int right,int bottom).调用setCompoundDrawables()只有在Drawable上调用setBounds()时才有效,这可能是你的originalDrawable工作的原因.

所以将代码更改为:

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

以上是内存溢出为你收集整理的android – 将旋转的drawable设置为TextView的drawableLeft全部内容,希望文章能够帮你解决android – 将旋转的drawable设置为TextView的drawableLeft所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1127748.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-30
下一篇2022-05-30

发表评论

登录后才能评论

评论列表(0条)

    保存