FastBitmapDrawable与Bitmap相比有哪些优点?

发布于 2024-12-06 16:06:48 字数 1110 浏览 0 评论 0原文

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;
    }
}
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;
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

混浊又暗下来 2024-12-13 16:06:48

将 FastBitmapDrawable 与位图进行比较并不公平。传统位图只是 Java 中的一种对象。然而,FastBitmapDrawables 是一个自定义类,旨在扩展 Drawable 类的功能,而不是 Bitmap 类的功能。

FastBitmapDrawable包含传统的位图,并做出一些假设,使其在某些情况下使用起来很方便。这是关键的一行:

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

此 FastBitmapDrawable 假定位图将放置在屏幕上的 (0, 0) 处,并且不会使用特殊的 Paint 对象来绘制它。

其实这只是一个方便而已。您可以通过在普通 Drawable 中手动将位置设置为 (0, 0) 并将 Paint 设置为 null 来获得相同的性能,但此类会自动为您执行此操作。

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.

我的黑色迷你裙 2024-12-13 16:06:48

这是一个实用程序类,以防您想要更改 Bitmap 的绘制方式。事实上,除了默认行为之外,它没有添加任何功能。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文