开发者

Setting an ImageView resource as a Drawable

开发者 https://www.devze.com 2022-12-28 17:34 出处:网络
I am trying to create a drawable in code and change the color based on some criteria. When I try and set the Drawable as the background of the ImageView it displays but won\'t let me set any padding.

I am trying to create a drawable in code and change the color based on some criteria. When I try and set the Drawable as the background of the ImageView it displays but won't let me set any padding. I realized I need to set the ImageView image via the setImageDrawable() function in order to be able to set the padding.

The problem I am running into is that when I set it via the setImageDrawable() function nothing is displayed.

Here is what I have written:

<?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().set开发者_开发技巧Color(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);

This results in a space for the ImageView but no image.

Thanks, Rob


If you implement the draw method of a custom ImageView you can just draw a tint over the image. For a nice rosy sheen try:

    @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 );
    }
0

精彩评论

暂无评论...
验证码 换一张
取 消