我正在尝试在代码中创建一个可绘制对象并根据某些标准更改颜色。当我尝试将 Drawable 设置为 ImageView 的背景时,它会显示但不会让我设置任何填充。我意识到我需要通过 setImageDrawable() 函数设置 ImageView 图像才能设置填充。
我遇到的问题是,当我通过 setImageDrawable() 函数设置它时,什么也没有显示。
这是我写的:
<?xml version="1.0" encoding="utf-8"?>
ImageView icon = (ImageView) row.findViewById(R.id.icon);
ShapeDrawable mDrawable;
int x = 0;
int y = 0;
int width = 50;
int height = 50;
float[] outerR = new float[] { 12, 12, 12, 12, 12, 12, 12, 12 };
mDrawable = new ShapeDrawable(new RoundRectShape(outerR, null, null));
mDrawable.setBounds(x, y+height, x + width, y);
switch(position){
case 0:
mDrawable.getPaint().setColor(0xffff0000); //Red
break;
case 1:
mDrawable.getPaint().setColor(0xffff0000); //Red
break;
case 2:
mDrawable.getPaint().setColor(0xff00c000); //Green
break;
case 3:
mDrawable.getPaint().setColor(0xff00c000); //Green
break;
case 4:
mDrawable.getPaint().setColor(0xff0000ff); //Blue
break;
case 5:
mDrawable.getPaint().setColor(0xff0000ff); //Blue
break;
case 6:
mDrawable.getPaint().setColor(0xff696969); //Gray
break;
case 7:
mDrawable.getPaint().setColor(0xff696969); //Gray
break;
case 8:
mDrawable.getPaint().setColor(0xffffff00); //Yellow
break;
case 9:
mDrawable.getPaint().setColor(0xff8b4513); //Brown
break;
case 10:
mDrawable.getPaint().setColor(0xff8b4513); //Brown
break;
case 11:
mDrawable.getPaint().setColor(0xff8b4513); //Brown
break;
case 12:
mDrawable.getPaint().setColor(0xffa020f0); //Purple
break;
case 13:
mDrawable.getPaint().setColor(0xffff0000); //Red
break;
case 14:
mDrawable.getPaint().setColor(0xffffd700); //Gold
break;
case 15:
mDrawable.getPaint().setColor(0xffff6600); //Orange
break;
}
icon.setImageDrawable(mDrawable);
icon.setPadding(5, 5, 5, 5);
这会导致 ImageView 有空间但没有图像。
谢谢,罗伯