APDE How to create BitmapDrawable from Drawable or any Image?

Hey fam,
how can one create a BitmapDrawable to use as tiled background for TextView’s or other’s on APDE?

I managed to create “regular” Drawables, but they don’t have the tiling option. Only BitmapDrawables can do that.
But i can’t figure out how to convert a regular Drawable or any kind of Image into a BitmapDrawable…

There are tons of examples on the web, how to “simply” do that, which don’t work on APDE.
Trust me, I tried them all…

It might be super simple if one has Android Studio or a desktop processing IDE, but if you are stuck using APDE on a mobile, you’re (put_negative_word_of_choice_here).

So does anyone got an idea that works on APDE?

Cheers, AmyS3

Hi @AmyS3,

Would you please show your current approach (simplified) ?

In best case your drawable is already a BitmapDrawable so you need simply a cast ie…

Drawable yourdrawable = <whatever>;
BitmapDrawable bitmapDrawable = (BitmapDrawable)yourdrawable;

or use a Canvas to draw the Drawable to and create one …

Drawable yourdrawable = <whatever>;
Bitmap bitmap= <create the bitmap with proper size and type related to yourdrawable>;
Canvas canvas = new Canvas(bitmap);
yourdrawable.draw(canvas);
BitmapDrawable bitmapDrawable  = new BitmapDrawable(null, bitmap);

Cheers
— mnse

thanks @mnse
i already worked up a solution but I’m gonna try yours as well. Just not today anymore… it’s now 5am and I’m gonna hit the hay…
My solution works on APDE and i built a small sketchpreview into it as well for convenience.

/*
  Convert image from data folder into BitmapDrawable.
  Works on APDE and has sketchpreview detection.
*/

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.BitmapFactory;

Activity act;
Context context;

// expects this file in the data folder.
// when using other folder, adjust accordingly.
String fileName = "test.png";

void setup()
{

  act = this.getActivity();
  context= act.getApplicationContext();
  
  // small automation to check if we are in preview...
  String path[] = split(sketchPath, '/');

  if (!path[4].equals("com.calsignlabs.apde.sketchpreview"))
  {
    try
    { // use this one when its an apk.
      // might works for preview too, but couldn't be bothered to try. 
      BitmapDrawable bitmapdrawable = new BitmapDrawable(BitmapFactory.decodeStream(context.getAssets().open(fileName)));
    }
    catch(IOException e) {
    }
  }
  else
  { // if its a preview, use this one.
    // works for apk only with hardcoded path to external storage.
    BitmapDrawable bitmapdrawable = new BitmapDrawable(BitmapFactory.decodeFile(sketchPath + "/" + fileName));
  }
}

Btw: BitmapDrawable is apparently deprecated.
So who knows what headaches are lying in front of us when it gets removed some day…

But BitmapDrawable’s are currently the only option if you want to tile/repeat a background…
And all that Drawable stuff is way too complicated if you ask me…
Why the hell can’t i just use a PImage as background :neutral_face:

Anyways, good night for now :sleeping:

1 Like