bitbybit
bitbybit

Reputation: 579

What are the advantages of FastBitmapDrawable compared to Bitmap?

package com.android.launcher;

import android.graphics.drawable.Drawable;
import android.graphics.PixelFormat;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.ColorFilter;

class FastBitmapDrawable extends Drawable {
    private Bitmap mBitmap;

    FastBitmapDrawable(Bitmap b) {
    mBitmap = b;
    }

    @Override
    public void draw(Canvas canvas) {
    canvas.drawBitmap(mBitmap, 0.0f, 0.0f, null);
    }

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

    @Override
    public void setAlpha(int alpha) {
    }

    @Override
    public void setColorFilter(ColorFilter cf) {
    }

    @Override
    public int getIntrinsicWidth() {
    return mBitmap.getWidth();
    }

    @Override
    public int getIntrinsicHeight() {
    return mBitmap.getHeight();
    }

    @Override
    public int getMinimumWidth() {
    return mBitmap.getWidth();
    }

    @Override
    public int getMinimumHeight() {
    return mBitmap.getHeight();
    }

    public Bitmap getBitmap() {
    return mBitmap;
    }
}

Upvotes: 1

Views: 1405

Answers (2)

theisenp
theisenp

Reputation: 8719

It's not really fair to compare a FastBitmapDrawable to a Bitmap. Traditional Bitmaps are just a type of Object in Java. FastBitmapDrawables however, are a custom class written to extend the functionality of the Drawable class, not the Bitmap class.

A FastBitmapDrawable contains a traditional Bitmap, and makes a few assumptions that make it convenient to use in certain situations. This is the crucial line:

canvas.drawBitmap(mBitmap, 0.0f, 0.0f, null);

This FastBitmapDrawable assumes that the bitmap will be placed at (0, 0) on the screen, and that no special Paint object will be used to draw it.

Really it's just a convenience. You could get the same performance by manually setting the position to (0, 0) and the Paint to null in a normal Drawable, but this class does that for you automatically.

Upvotes: 3

Caner
Caner

Reputation: 59168

This is a utility class, in case you want to alter how a Bitmap is drawn. As it is, it doesn't add any functionality other than default behavior.

Upvotes: 0

Related Questions