[英]Can't rescale drawable in button
private float iconScale = 0.05f;
// Set size of drawables in buttons
Drawable drawable = getResources().getDrawable(R.drawable.cutlerywhite);
drawable.setBounds(0, 0, (int)(drawable.getIntrinsicWidth()),
(int)(drawable.getIntrinsicHeight()));
ScaleDrawable sd = new ScaleDrawable(drawable, 0, iconScale, iconScale);
fnbButton.setCompoundDrawables(sd, null, null, null); //set drawableLeft for example
我正在尝试按比例缩小可在Button
使用的可绘制对象。 现在,我的按钮没有任何显示:
左侧应该有一个可绘制对象。 这是什么问题 我所做的完全像在其他答案中看到的一样。
XML:
<Button
android:id="@+id/fnbButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/rounded_button_blue"
android:padding="5dp"
android:text="@string/fnb_button"
android:textAllCaps="false"
android:textColor="@color/white"
app:layout_constraintBaseline_toBaselineOf="@id/otherButton"
app:layout_constraintEnd_toEndOf="parent" />
也许这个选项会有所帮助
private void useScaledDrawableLeft(Button buttonView, float scale, @DrawableRes int imageResource) {
Resources res = getResources();
BitmapDrawable bd = (BitmapDrawable) res.getDrawable(imageResource);
Bitmap b = Bitmap.createScaledBitmap(bd.getBitmap(),
(int) (bd.getIntrinsicHeight() * scale),
(int) (bd.getIntrinsicWidth() * scale),
false);
BitmapDrawable scaleDrawable = new BitmapDrawable(b);
buttonView.setCompoundDrawablesWithIntrinsicBounds(scaleDrawable, null, null, null);
}
尝试这个
Drawable drawable = getResources().getDrawable(R.drawable.cutlerywhite);
ScaleDrawable sd = new ScaleDrawable(drawable, Gravity.CENTER, iconScale, iconScale);
Drawable d1 = sd.getDrawable();
d1.setLevel(1);
sd.setLevel(1);
fnbButton.setCompoundDrawablesWithIntrinsicBounds(sd, null, null, null);
我最终这样做:
Drawable drawableFnb = getResources().getDrawable(R.drawable.cutlerywhite);
Drawable drawableSunbed = getResources().getDrawable(R.drawable.logowhite);
ScaleDrawable sdFnb = new ScaleDrawable(drawableFnb, Gravity.LEFT, 1, 1);
ScaleDrawable sdSunbed = new ScaleDrawable(drawableSunbed, Gravity.LEFT, 1, 1);
Drawable d1 = sdFnb.getDrawable();
Drawable d2 = sdSunbed.getDrawable();
Dims fnbDims = getIconDims(drawableFnb);
Dims sunbedDims = getIconDims(drawableSunbed);
d1.setBounds(0, 0, fnbDims.getWidth(), fnbDims.getHeight());
d2.setBounds(0, 0, sunbedDims.getWidth(), sunbedDims.getHeight());
fnbButton.setCompoundDrawables(d1, null, null, null);
sunbedButton.setCompoundDrawables(d2, null, null, null);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.