मेरा समाधान @ over_optimistic के समाधान के करीब है, कम से कम saveLayer() कॉल। मैं पथ के बजाए एक ड्रायबल मास्क का उपयोग करता हूं, मेरे मामले में यह एक डिस्क थी।
मैं क्षेत्रों के रूप में इन चर घोषित (यह OnDraw विधि के बाहर स्मृति को आबंटित अच्छा व्यवहार है):
private Paint maskingPaint = new Paint();
private Drawable mask = <insert your drawable here>
फिर, कहीं OnDraw(), सेटअप वस्तुओं के बाहर:
// Xfermode won't work if hardware accelerated
setLayerType(View.LAYER_TYPE_SOFTWARE, null);
// Using destination shape as a mask
// For a good explanation of PorterDuff transfer modes : http://ssp.impulsetrain.com/porterduff.html
maskingPaint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
maskingPaint.setAntiAlias(true);
// Position the mask
mask.setBounds(<insert your mask bounds here>);
फिर आखिरकार, ऑनड्रा() विधि मास्क पर लागू होती है:
@Override
protected synchronized void onDraw(Canvas canvas)
{
// Draw the mask first, making it the PorterDuff destination
mask.draw(canvas);
// Save the layer with the masking paint, that will be applied on restore()
// Using CLIP_TO_LAYER_SAVE_FLAG to avoid any overflow of the masked image outside the mask bounds.
Rect bounds = mask.getBounds();
canvas.saveLayer(bounds.left, bounds.top, bounds.right, bounds.bottom, maskingPaint,
Canvas.CLIP_TO_LAYER_SAVE_FLAG);
// Draw the shape offscreen, making it the PorterDuff source
super.onDraw(canvas);
// Apply the source to the destination, using SRC_IN transfer mode
canvas.restore();
}
बेहतर समझ के लिए ओ एफ स्थानांतरण मोड, मैंने http://ssp.impulsetrain.com/porterduff.html को संदर्भित किया। वह पृष्ठ पढ़ने के लिए बहुत दिलचस्प है। उसके बाद, उसी तरह के कोड के साथ आप एक साधारण मुखौटा से ज्यादा प्राप्त करने में सक्षम होंगे!
स्रोत
2014-07-30 16:04:05
मैं अन्य बिटमैप/ड्रायबल पर संबंधित पिक्सेल के अल्फा चैनल को सेट करने के लिए मास्क बिटमैप के काले हिस्सों का उपयोग करने की कोशिश कर रहा हूं। –