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

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

我该怎么做呢?

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); 

Solutions Collecting From Web of "以编程方式设置可绘制的大小"

setBounds()方法不适用于每种types的容器(但是,我的一些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" 

使用setBounds()指定大小,即使用50×50大小

 drawable.setBounds(0, 0, 50, 50); 

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

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

 drawable = new ScaleDrawable(drawable, 0, width, height).getDrawable(); 

之后

 drawable.setBounds(0, 0, width, height); 

将工作

使用post方法来达到预期的效果:

 {your view}.post(new Runnable() { @Override public void run() { Drawable image = context.getResources().getDrawable({drawable image resource id}); image.setBounds(0, 0, {width amount in pixels}, {height amount in pixels}); {your view}.setCompoundDrawables(image, null, null, null); } }); 

大概晚了一点。 但是,在任何情况下,最终都能为我解决问题。

这个想法是创build一个具有固定内在大小的自定义drawable,并将绘图作业传递给原始drawable。

 Drawable icon = new ColorDrawable(){ Drawable iconOrig = resolveInfo.loadIcon(packageManager); @Override public void setBounds(int left, int top, int right, int bottom){ super.setBounds(left, top, right, bottom);//This is needed so that getBounds on this class would work correctly. iconOrig.setBounds(left, top, right, bottom); } @Override public void draw(Canvas canvas){ iconOrig.draw(canvas); } @Override public int getIntrinsicWidth(){ return mPlatform.dp2px(30); } @Override public int getIntrinsicHeight(){ return mPlatform.dp2px(30); } }; 

您可以创build视图types的子类,并覆盖onSizeChanged方法。

我想要在我的文本视图上缩放复合绘图,而不需要我在xml等中定义位图可绘画,这样做:

 public class StatIcon extends TextView { private Bitmap mIcon; public void setIcon(int drawableId) { mIcon = BitmapFactory.decodeResource(RIApplication.appResources, drawableId); } @Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { if ((w > 0) && (mIcon != null)) this.setCompoundDrawablesWithIntrinsicBounds( null, new BitmapDrawable(Bitmap.createScaledBitmap(mIcon, w, w, true)), null, null); super.onSizeChanged(w, h, oldw, oldh); } } 

(请注意,我使用了两次而不是h,因为在这种情况下,我将图标放在文本上方,因此图标不应该与文本视图具有相同的高度)

这可以应用到背景drawables,或者其他任何你想resize相对于你的视图大小。 onSizeChanged()被称为第一次的视图,所以你不需要任何特殊的情况下初始化的大小。

使用

 textView.setCompoundDrawablesWithIntrinsicBounds() 

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

  defaultConfig { applicationId "com.example..." minSdkVersion 17 targetSdkVersion 25 versionCode 1 versionName "1.0" } 

要更改可绘制的大小:

  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); 

我没有时间挖掘为什么setBounds()方法不能像位图一样工作,但是我没有调整@root-studio解决scheme来做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() : 0; } @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; } } 

用法:

 // 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); 

结果

之前: 在这里输入图像说明 后: 在这里输入图像说明

 Button button = new Button(this); Button = (Button) findViewById(R.id.button01); 

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