Drawable image on a canvas

183,865

Solution 1

The good way to draw a Drawable on a canvas is not decoding it yourself but leaving it to the system to do so:

Drawable d = getResources().getDrawable(R.drawable.foobar, null);
d.setBounds(left, top, right, bottom);
d.draw(canvas);

This will work with all kinds of drawables, not only bitmaps. And it also means that you can re-use that same drawable again if only the size changes.

Solution 2

You need to load your image as bitmap:

 Resources res = getResources();
 Bitmap bitmap = BitmapFactory.decodeResource(res, R.drawable.your_image);

Then make the bitmap mutable and create a canvas over it:

Canvas canvas = new Canvas(bitmap.copy(Bitmap.Config.ARGB_8888, true));

You then can draw on the canvas.

Solution 3

also you can use this way. it will change your big drawble fit to your canvas:

Resources res = getResources();
Bitmap bitmap = BitmapFactory.decodeResource(res, yourDrawable);
yourCanvas.drawBitmap(bitmap, 0, 0, yourPaint);

Solution 4

Drawable d = ContextCompat.getDrawable(context, R.drawable.***)
d.setBounds(left, top, right, bottom);
d.draw(canvas);

Solution 5

try this

Bitmap mBitmap = Bitmap.createScaledBitmap(Bitmap src, int dstWidth, int dstHeight, boolean filter);

protected void onDraw(Canvas canvas) {
            canvas.drawColor(0xFFAAAAAA);
            canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);

        }
Share:
183,865

Related videos on Youtube

Lana
Author by

Lana

Updated on July 08, 2022

Comments

  • Lana
    Lana almost 2 years

    How can I get an image to the canvas in order to draw on that image?

  • AndroidDev
    AndroidDev about 11 years
    But if i use canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint); it works and it load the saved image on the canvas, but at the same time undo and redo stop working, Check my code pastebin.com/cP9w6stm
  • Sibbs Gambling
    Sibbs Gambling almost 11 years
    What if my .png is big and it gets cut after being loaded? i.e. I don;t want to have it cropped. It can be bigger than the screen, then I will try to make it zoomable and movable. How may I do that? Thank you so so much!
  • Konstantin Burov
    Konstantin Burov almost 11 years
    @perfectionm1ng look at using BitmapRegionDecoder. It allows partial loading for big images. So you should be able to load only the part of image you're rendering at the moment.
  • Ahmad Arslan
    Ahmad Arslan about 10 years
    I have the same problem I am using the code Canvas cs = new Canvas(bitmap); Resources res = getResources(); Bitmap bitmapx = BitmapFactory.decodeResource(res, R.drawable.overlay_good_full); Bitmap bitmapxx = BitmapFactory.decodeResource(res, R.drawable.overlay_bad_full); if(text.equals("Good")) { cs.drawBitmap(bitmapx, 0, 0, tPaint); } else { cs.drawBitmap(bitmapxx, 0, 0, tPaint); }
  • RichieHH
    RichieHH almost 10 years
    How does one use an animation-list (multiple frames) for animation with the canvas approach?
  • Asim
    Asim over 9 years
    Very useful! At least when you need just a background picture with no rocket science involved!
  • AgentKnopf
    AgentKnopf about 9 years
    @RichieHH I have my background on a SurfaceView (propably a waste) and that surface view is embedded in a Framelayout and that framelayout contains ImageViews which I then animate - maybe that's an option for you?
  • Crisic
    Crisic over 8 years
    BitmapFactory class doesn't have method "BitmapFactory.decodeResource(Resource, Drawable);"...
  • Vikram
    Vikram over 7 years
    I create Drawable with gerResource().getDrawable(id);
  • wblaschko
    wblaschko over 7 years
    getDrawable(id) is being deprecated, you should either use the ContextCompat call above, or the getDrawable(id, theme) call. developer.android.com/reference/android/content/res/…
  • wblaschko
    wblaschko over 7 years
    getDrawable(id) is being deprecated, you should either use the ContextCompat call above, or the getDrawable(id, theme) call. developer.android.com/reference/android/content/res/…
  • Admin
    Admin over 7 years
    @Crisic I would assume that yourDrawable is an integer.
  • Admin
    Admin over 7 years
    For me, it was necessary to use setBounds. Without it, nothing got displayed.
  • Gábor
    Gábor over 6 years
    @Saveen: Are you sure? As far as I can remember, this is supposed to work from the earliest versions of Android.
  • Siddarth G
    Siddarth G about 4 years
    How does creating new canvas object draw on the canvas of onDraw ?
  • vrgrg
    vrgrg about 3 years
    setBounds(0, 0, width, height)