【发布时间】:2016-04-14 06:02:55
【问题描述】:
Fresco 内置了对圆形图像和圆角的支持,但其他形状如菱形或平行四边形等呢?
通过使用 BitmapShader 的自定义可绘制对象来处理标准 ImageView 很简单。例如,下面的自定义 Drawable 接收图像 Bitmap 和坡度高度,使 ImageView 看起来像这样:
public class MaskDrawable extends Drawable {
private Paint mPaint;
private Path mPath;
private int mSlopeHeight;
public MaskDrawable(Bitmap bitmap, int slopeHeight) {
BitmapShader shader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setShader(shader);
mSlopeHeight = slopeHeight;
mPath = new Path();
}
@Override
public void draw(Canvas canvas) {
Rect bounds = getBounds();
mPath.moveTo(0, 0);
mPath.lineTo(0, bounds.bottom);
mPath.lineTo(bounds.right, bounds.bottom - mSlopeHeight);
mPath.lineTo(bounds.right, 0);
canvas.drawPath(mPath, mPaint);
}
要使用 Fresco 执行此操作,我需要图像的位图,但我不确定如何执行此操作。我读到我可以直接从 ImagePipeline 获取位图,但它附带了许多陷阱。在一种情况下,返回的位图是短暂的,不应该用于在屏幕上绘制,而在另一种情况下,我会得到一个 CloseableReference,我需要在某个我不清楚的时候释放它。到目前为止,我在网上看到的是与此类似的用于获取位图的代码:
ImagePipeline imagePipeline = Fresco.getImagePipeline();
ImageRequest imageRequest = ImageRequestBuilder
.newBuilderWithSource(uri)
.setRequestPriority(Priority.HIGH)
.setLowestPermittedRequestLevel(ImageRequest.RequestLevel.FULL_FETCH)
.build();
DataSource<CloseableReference<CloseableBitmap>> dataSource = imagePipeline.fetchDecodedImage(imageRequest, getContext());
DataSubscriber<CloseableReference<CloseableBitmap>> dataSubscriber =
new BaseDataSubscriber<CloseableReference<CloseableBitmap>>() {
@Override
protected void onNewResultImpl(DataSource<CloseableReference<CloseableBitmap>> dataSource) {
mBitmapRef = dataSource.getResult();
// Get the bitmap here and use it in my custom drawable?
}
@Override
protected void onFailureImpl(DataSource<CloseableReference<CloseableBitmap>> dataSource) {
}
};
dataSource.subscribe(dataSubscriber, UiThreadImmediateExecutorService.getInstance());
我还没有尝试过,想知道是否有人可以提供一个可行的解决方案,而不是我迄今为止从不同地方收集的位和字节。它必须正确完成,否则我很容易泄漏内存,这从一开始就打败了使用 Fresco 的整个想法。
【问题讨论】:
标签: android android-imageview android-drawable android-bitmap fresco