내 솔루션은 @ over_optimistic의 솔루션에 가까울수록 saveLayer() 호출이 적습니다. 필자는 경로 대신 Drawable 마스크를 사용합니다. 제 경우에는 디스크였습니다. 어딘가의 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>);
마지막으로 onDraw() 메서드는 마스크를 적용합니다.
@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을 참조했다. 그 페이지는 꽤 재미 있습니다. 그런 다음 동일한 종류의 코드를 사용하면 간단한 마스크보다 훨씬 더 많은 것을 습득 할 수 있습니다!
마스크 비트 맵의 검은 부분을 사용하여 다른 비트 맵/드로어 블에있는 해당 픽셀의 알파 채널을 0으로 설정하려고합니다. –