Question

I am using a class that handles async loading of images. During the load, the class loads a colordrawable (that will be later assigned to downloaded image). Instead of a color, I would like to use a drawable resource to implement the "loading" image. How is this possible? Doing it "outside" this drawable class would be easier, but as a limitation I have to do it inside the class. Can't figure a way how to reference a resource inside the class.

Currently:

class myDrawable extends ColorDrawable {
  public myDrawable () {
     super (Color.BLACK);
  }
}

Is using BitmapDrawable the right way?

class myDrawable extends BitmapDrawable {
  public myDrawable () {
     super(); // something here to fetch the drawable?
  }
}

BitmapDrawable constructor is as follows:

    public BitmapDrawable(Resources res)

If this can be used, how to get the right "resourcees" indentifier to be passed for it? E.g. super(R.drawable.stubImage);

Was it helpful?

Solution

static class HolderDrawable extends BitmapDrawable {
    private final WeakReference<BitmapDownloaderTask> bitmapDownloaderTaskReference;

    public HolderDrawable(BitmapDownloaderTask bitmapDownloaderTask) {
        super(mDefaultBitmap);
        bitmapDownloaderTaskReference = new WeakReference<BitmapDownloaderTask>(
                bitmapDownloaderTask);

    }

    public BitmapDownloaderTask getBitmapDownloaderTask() {
        return bitmapDownloaderTaskReference.get();
    }

    @Override
    public void draw(Canvas arg0) {
        super.draw(arg0);
    }

    @Override
    public int getOpacity() {
        return super.getOpacity(); 
    }

    @Override
    public void setAlpha(int alpha) {
        super.setAlpha(alpha);
    }

    @Override
    public void setColorFilter(ColorFilter cf) {    
        super.setColorFilter(cf);
    }
}

You can create the mDefaultBitmap from downloading something online, or from a resource. That step is easy, just google it. I recommend you set mDefaultBitmap as a static field so that you don't have to create it every time you create a HolderDrawable.

OTHER TIPS

If you need the reference to Resources, it will be available from the Context.getResource(), which the context is passed as parameters in the constructor of your custom view, or your activity class itself.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top