以编程方式设置可绘制大小

81 java android

图像(图标)的大小大致相同,但我需要调整它们的大小,以使按钮保持相同的高度.

我该怎么做呢?

Button button = new Button(this);
button.setText(apiEventObject.getTitle());
button.setOnClickListener(listener);

/*
 * set clickable id of button to actual event id
 */
int id = Integer.parseInt(apiEventObject.getId());
button.setId(id);

button.setLayoutParams(new LayoutParams(
        android.view.ViewGroup.LayoutParams.FILL_PARENT,
        android.view.ViewGroup.LayoutParams.WRAP_CONTENT));

Drawable drawable = LoadImageFromWebOperations(apiSizeObject.getSmall());
//?resize drawable here? drawable.setBounds(50, 50, 50, 50);
button.setCompoundDrawablesWithIntrinsicBounds(drawable, null, null, null);
Run Code Online (Sandbox Code Playgroud)

Eri*_*hen 144

setBounds()方法不适用于每种类型的容器(但对我ImageView的某些容器起作用).

尝试以下方法来缩放drawable本身:

// Read your drawable from somewhere
Drawable dr = getResources().getDrawable(R.drawable.somedrawable);
Bitmap bitmap = ((BitmapDrawable) dr).getBitmap();
// Scale it to 50 x 50
Drawable d = new BitmapDrawable(getResources(), Bitmap.createScaledBitmap(bitmap, 50, 50, true));
// Set your new, scaled drawable "d"
Run Code Online (Sandbox Code Playgroud)

  • 不推荐使用BitmapDrawable(Bitmap)构造函数.使用:Drawable d = new BitmapDrawable(getResources(),Bitmap.createScaledBitmap(bitmap,50,50,true)); (8认同)
  • 这将在放大可绘制对象时产生像素化。即使它们是矢量可绘制对象。 (2认同)

jkh*_*uw1 31

指定尺寸setBounds(),即使用50x50尺寸

drawable.setBounds(0, 0, 50, 50);
Run Code Online (Sandbox Code Playgroud)

public void setBounds(int left,int top,int right,int bottom)

  • 有同样的问题,这对我没用. (11认同)
  • 实际上,setBounds适用于GradientDrawables.它只适用于Image Drawables. (5认同)
  • SetBounds 大小后保持不变。可能需要一些无效? (2认同)

iBo*_*Bog 14

在应用之前.setBounds(..)尝试将当前的Drawable转换为ScaleDrawable

drawable = new ScaleDrawable(drawable, 0, width, height).getDrawable();
Run Code Online (Sandbox Code Playgroud)

之后

drawable.setBounds(0, 0, width, height);
Run Code Online (Sandbox Code Playgroud)

将工作

  • 为什么需要这一步?`ScaleDrawable` 中的什么包装可以替代非包装? (2认同)

cep*_*3us 8

我没有时间去挖掘为什么setBounds()方法没有像预期的那样处理位图drawable但是我很少调整@androbean-studio解决方案来做setBounds应该做的事情......

/**
 * Created by ceph3us on 23.05.17.
 * file belong to pl.ceph3us.base.android.drawables
 * this class wraps drawable and forwards draw canvas
 * on it wrapped instance by using its defined bounds
 */
public class WrappedDrawable extends Drawable {

    private final Drawable _drawable;
    protected Drawable getDrawable() {
        return _drawable;
    }

    public WrappedDrawable(Drawable drawable) {
        super();
        _drawable = drawable;
    }

    @Override
    public void setBounds(int left, int top, int right, int bottom) {
        //update bounds to get correctly
        super.setBounds(left, top, right, bottom);
        Drawable drawable = getDrawable();
        if (drawable != null) {
            drawable.setBounds(left, top, right, bottom);
        }
    }

    @Override
    public void setAlpha(int alpha) {
        Drawable drawable = getDrawable();
        if (drawable != null) {
            drawable.setAlpha(alpha);
        }
    }

    @Override
    public void setColorFilter(ColorFilter colorFilter) {
        Drawable drawable = getDrawable();
        if (drawable != null) {
            drawable.setColorFilter(colorFilter);
        }
    }

    @Override
    public int getOpacity() {
        Drawable drawable = getDrawable();
        return drawable != null
                ? drawable.getOpacity()
                : PixelFormat.UNKNOWN;
    }

    @Override
    public void draw(Canvas canvas) {
        Drawable drawable = getDrawable();
        if (drawable != null) {
            drawable.draw(canvas);
        }
    }

    @Override
    public int getIntrinsicWidth() {
        Drawable drawable = getDrawable();
        return drawable != null
                ? drawable.getBounds().width()
                : 0;
    }

    @Override
    public int getIntrinsicHeight() {
        Drawable drawable = getDrawable();
        return drawable != null ?
                drawable.getBounds().height()
                : 0;
    }
}
Run Code Online (Sandbox Code Playgroud)

用法:

// get huge drawable 
final Drawable drawable = resources.getDrawable(R.drawable.g_logo);
// create our wrapper           
WrappedDrawable wrappedDrawable = new WrappedDrawable(drawable);
// set bounds on wrapper 
wrappedDrawable.setBounds(0,0,32,32); 
// use wrapped drawable 
Button.setCompoundDrawablesWithIntrinsicBounds(wrappedDrawable ,null, null, null);
Run Code Online (Sandbox Code Playgroud)

结果

之前: 在此输入图像描述 后: 在此输入图像描述


Sem*_*tih 6

使用

textView.setCompoundDrawablesWithIntrinsicBounds()
Run Code Online (Sandbox Code Playgroud)

你的minSdkVersion应该是build.gradle中的17

    defaultConfig {
    applicationId "com.example..."
    minSdkVersion 17
    targetSdkVersion 25
    versionCode 1
    versionName "1.0"
}
Run Code Online (Sandbox Code Playgroud)

要更改可绘制的大小:

    TextView v = (TextView)findViewById(email);
    Drawable dr = getResources().getDrawable(R.drawable.signup_mail);
    Bitmap bitmap = ((BitmapDrawable) dr).getBitmap();
    Drawable d = new BitmapDrawable(getResources(), Bitmap.createScaledBitmap(bitmap, 80, 80, true));

    //setCompoundDrawablesWithIntrinsicBounds (image to left, top, right, bottom)
    v.setCompoundDrawablesWithIntrinsicBounds(d,null,null,null);
Run Code Online (Sandbox Code Playgroud)


小智 -47

Button button = new Button(this);
Button = (Button) findViewById(R.id.button01);
Run Code Online (Sandbox Code Playgroud)

使用Button.setHeight()Button.setWeight()并设置一个值。

  • 您知道您可以删除答案,不是吗? (26认同)
  • 这只是设置按钮高度,而不是可绘制对象的高度。我想设置可绘制对象的宽度/高度(特别是如果它大于设置的按钮高度)。 (9认同)