0

我正在尝试在代码中创建一个可绘制对象并根据某些标准更改颜色。当我尝试将 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 有空间但没有图像。

谢谢,罗伯

4

1 回答 1

0

如果您实现自定义 ImageView 的 draw 方法,您可以在图像上绘制一个色调。要获得漂亮的玫瑰色光泽,请尝试:

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

        Rect                frame;
        Paint               paint = new Paint();

        paint.setStyle( Style.FILL );
        paint.setARGB( 102 , 255 , 51 , 51 );
        frame = new Rect( 0, 0, getWidth(), getHeight() );
        canvas.drawRect( frame , paint );
    }
于 2010-05-02T22:06:02.127 回答