2

I'm trying to get a bitmap image to be drawn on my canvas in my android project. I've been at it for over two days now and just can't seem to figure it out. I attached the code where the function's being called.

private void drawLogo(Canvas canvas)
{
    Paint test = new Paint();
    test.setColor(Color.RED);
    test.setStrokeWidth(4);
    test.setStyle(Style.FILL_AND_STROKE);

    //Here was my problem
    //Changed to --> new RectF(scale_x, scale_y, 5*scale_x, 5*scale_y) and works now
    RectF logoSize = new RectF(scale_x, 5*scale_y, 5*scale_x, scale_y);

    Bitmap logoBitmap = getImageMap().get("LOGO");

    canvas.drawRect(logoSize, test);

    canvas.drawBitmap( logoBitmap, null, logoSize, null );
}

The canvas.drawRect( RectF, Paint ) method draws the rectangle correctly, but the bitmap does not show up at all. Any insight would be greatly appreciated, thanks in advance.

EDIT: I added code from my getImageMap() method as requested. It basically just returns all the images in my assets/images/ folder as a Map, so I can easily pull any image I want from the folder.

private Map<String, Bitmap> getImageMap()
{
    if (imageMap == null)
    {
        imageMap = new HashMap<String, Bitmap>();
        try
        {
            String[] files = getContext().getAssets().list("images");

            for (String imageName : files)
            {
                // Construct a BitMap from an asset
                Bitmap bitmap = BitmapFactory.decodeStream(
                    getContext().getAssets().open("images/" + imageName));
                imageMap.put(imageName.replaceFirst("\\..*", ""), bitmap);
            }
        }
        catch (IOException e) {Log.e("assets/images/ is empty", "IOException", e);}
    }
    return imageMap;
}

EDIT2: I found my misstake, I updated it in the code. I just wanna thank those who gave me speedy feedback, I appreciate it.

Kara
  • 6,115
  • 16
  • 50
  • 57
  • Can you give more detail as to what getImageMap().get("LOGO"); is doing? Also, does your bitmap have an alpha channel? Finally, have you tried wrapping your logoBitmap in a BitmapDrawable and making sure it looks as you'd expect it to? – TheIT Dec 12 '13 at 03:16
  • I'm sure the getImageMap().get("LOGO") is returning the correct bitmap I want, I've checked that (I will still post my code above). As for the alpha channel, I don't believe so since I'm using API 10 (I think alpha channel's for bitmaps weren't introduced until API 12). I have not tried BitmapDrawable, but I tried using other bitmaps to test if this one was in some way defected. I'll get back to you soon. –  Dec 12 '13 at 04:10
  • Just some thoughts, could logoBitmap be null? I agree with Junior Buckeridge, in trying to simplify the problem as much as you can. Perhaps simply test by displaying canvas = new Canvas(logoBitmap); and nothing else? – TheIT Dec 12 '13 at 04:22
  • 1
    I've already tested to see if logoBitmap is null. I'm sure that's not the problem, because I used the getImageMap() method for a project last semester and it ran perfectly.. I just can't seem to figure out what's going wrong. –  Dec 12 '13 at 05:13
  • 1
    Thanks for the update! All the best with your project :) – TheIT Dec 12 '13 at 20:36

1 Answers1

0

Try something simple:

canvas.drawBitmap(logoBitmap, logoSize.left, logoSize.top, null);

Regards.

Junior Buckeridge
  • 2,075
  • 2
  • 21
  • 27
  • Nothing in his post indicates that he hasn't tried something "simple". I agree that he should have given more detail as to what exactly he tried over the past couple of days, but in this case there is certainly no need for a comment like "do a little more research next time". – TheIT Dec 12 '13 at 03:45
  • I'm sorry if I hurt somebody's feelings. Wasn't my intention. I meant that a simple look at the android docs would have solved the problem. Also excuse my english (second language). – Junior Buckeridge Dec 12 '13 at 03:59
  • No worries. I've drawn bitmaps to canvas before with the method canvas.drawBitmap(Bitmap,null,RectF,null), it usually works very smoothly and simply.. I just can't figure out the problem this time. But as for your "Try something simple", there's no need for you to include a paint object.. it could just be null to make it even more simple. –  Dec 12 '13 at 04:18
  • Hey, I got the bitmap to show up using the canvas.drawBitmap(bitmap, left, top, null) method, so the problem must be in the RectF logoSize –  Dec 12 '13 at 06:53
  • 1
    So I figured it out and thought you guys would wanna know what was wrong.. it turns out that the "RectF logoSize = new RectF(scale_x, 5*scale_y, 5*scale_x, scale_y);" wasn't creating a real rectangle. I just had to swap two values around (scale_x, scale_y, 5*scale_x, 5*scale_y), and it worked perfectly. Lesson learned! –  Dec 12 '13 at 08:44