壁畫內置支持圓形圖像和圓角,但其他形狀如鑽石或平行四邊形等呢?Android壁畫:繪製不同種類的圖像形狀
通過使用BitmapShader的自定義可繪製對標準ImageView執行操作很簡單。例如,下面的自定義繪製對象接收圖像的位圖和邊坡高度作出的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);
}
要做到這一點與壁畫,我需要的圖像的位圖,但我不知道該怎麼做。我讀過,我可以直接從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的整個想法。
我已經更新了答案,讓具體的解決方案實現,以防其他人碰到這個問題。 –