【问题标题】:Decrease the size of the radio button icon减小单选按钮图标的大小
【发布时间】:2013-09-04 02:43:09
【问题描述】:

我正在以编程方式创建单选按钮,它工作正常,

RadioImageButton RadioImageButton = new RadioImageButton(this);
    RadioImageButton.setGravity(Gravity.CENTER);
    RadioImageButton.setId(buttonId);
    RadioImageButton.setTextColor(Color.BLACK);
   RadioImageButton.setCompoundDrawablesWithIntrinsicBounds(icon, null,null, null)// use this to set the icon
    RadioImageButton.setBackgroundDrawable(drawable);
    RadioGroup.LayoutParams radioImageButtonParams = new RadioGroup.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
        LinearLayout.LayoutParams.MATCH_PARENT, 1f);
    radioImageButtonParams.setMargins(0, 0, 1, 0);

    RadioGroup.addView(RadioImageButton, radioImageButtonParams);

在 RadioImageButton 类中

Drawable image;

    public RadioImageButton(Context context) {
    super(context);
    setButtonDrawable(android.R.color.transparent);
    }

    @Override
    protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    if (image != null) {
        image.setState(getDrawableState());

        final int verticalGravity = getGravity() & Gravity.VERTICAL_GRAVITY_MASK;
        final int height = image.getIntrinsicHeight();

        int y = 0;

        switch (verticalGravity) {
        case Gravity.BOTTOM:
        y = getHeight() - height;
        break;
        case Gravity.CENTER_VERTICAL:
        y = (getHeight() - height) / 2;
        break;
        }

        int buttonWidth = image.getIntrinsicWidth();
        int buttonLeft = (getWidth() - buttonWidth) / 3;
        image.setBounds(buttonLeft, y, buttonLeft + buttonWidth, y + height);
        image.draw(canvas);
    }
    }

但是正在显示的图标很大,我需要减小该图标的大小。 我试过setHeight,但它不起作用。

    RadioImageButton.setCompoundDrawablesWithIntrinsicBounds(icon, null,null, null)
this is used to set the icon

【问题讨论】:

  • Here's 实际解决方案:-)

标签: android radio-button radio-group


【解决方案1】:

好的,我自己修好了,这是答案

private Drawable resize(Drawable image) {
    Bitmap b = ((BitmapDrawable)image).getBitmap();
    Bitmap bitmapResized = Bitmap.createScaledBitmap(b, 50, 50, true);// filter attribute set to true
    return new BitmapDrawable(bitmapResized);
}

上面的代码是修改drawable的大小,并确保filter属性设置为true,这样图标看起来不会模糊。

希望它对将来的某些人有所帮助。快乐编码:)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-06
    • 2012-09-12
    • 2016-11-16
    • 2018-12-30
    • 2019-01-18
    • 2014-07-03
    相关资源
    最近更新 更多