开发者

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() {
    retu开发者_C百科rn mBitmap;
    }
}


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜