FastBitmapDrawable与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;
}
}
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将 FastBitmapDrawable 与位图进行比较并不公平。传统位图只是 Java 中的一种
对象
。然而,FastBitmapDrawables 是一个自定义类,旨在扩展Drawable
类的功能,而不是Bitmap
类的功能。FastBitmapDrawable包含传统的位图,并做出一些假设,使其在某些情况下使用起来很方便。这是关键的一行:
此 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 theDrawable
class, not theBitmap
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:
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.这是一个实用程序类,以防您想要更改
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.