Added in API level 1

PorterDuff.Mode


public static final enum PorterDuff.Mode
extends Enum<PorterDuff.Mode>

java.lang.Object
   ↳ java.lang.Enum<android.graphics.PorterDuff.Mode>
     ↳ android.graphics.PorterDuff.Mode


The name of the parent class is an homage to the work of Thomas Porter and Tom Duff, presented in their seminal 1984 paper titled "Compositing Digital Images". In this paper, the authors describe 12 compositing operators that govern how to compute the color resulting of the composition of a source (the graphics object to render) with a destination (the content of the render target).

"Compositing Digital Images" was published in Computer Graphics Volume 18, Number 3 dated July 1984.

Because the work of Porter and Duff focuses solely on the effects of the alpha channel of the source and destination, the 12 operators described in the original paper are called alpha compositing modes here.

For convenience, this class also provides several blending modes, which similarly define the result of compositing a source and a destination but without being constrained to the alpha channel. These blending modes are not defined by Porter and Duff but have been included in this class for convenience purposes.

Diagrams

All the example diagrams presented below use the same source and destination images:

Source image
Destination image

The order of drawing operations used to generate each diagram is shown in the following code snippet:

 Paint paint = new Paint();
 canvas.drawBitmap(destinationImage, 0, 0, paint);

 PorterDuff.Mode mode = // choose a mode
 paint.setXfermode(new PorterDuffXfermode(mode));

 canvas.drawBitmap(sourceImage, 0, 0, paint);
 

Alpha compositing modes

Blending modes

Compositing equations

The documentation of each individual alpha compositing or blending mode below provides the exact equation used to compute alpha and color value of the result of the composition of a source and destination.

The result (or output) alpha value is noted αout. The result (or output) color value is noted Cout.

Summary

Enum values

PorterDuff.Mode  ADD

Adds the source pixels to the destination pixels and saturates the result. 

PorterDuff.Mode  CLEAR

Destination pixels covered by the source are cleared to 0. 

PorterDuff.Mode  DARKEN

Retains the smallest component of the source and destination pixels. 

PorterDuff.Mode  DST

The source pixels are discarded, leaving the destination intact. 

PorterDuff.Mode  DST_ATOP

Discards the destination pixels that are not covered by source pixels. 

PorterDuff.Mode  DST_IN

Keeps the destination pixels that cover source pixels, discards the remaining source and destination pixels. 

PorterDuff.Mode  DST_OUT

Keeps the destination pixels that are not covered by source pixels. 

PorterDuff.Mode  DST_OVER

The source pixels are drawn behind the destination pixels. 

PorterDuff.Mode  LIGHTEN

Retains the largest component of the source and destination pixel. 

PorterDuff.Mode  MULTIPLY

Multiplies the source and destination pixels. 

PorterDuff.Mode  OVERLAY

Multiplies or screens the source and destination depending on the destination color. 

PorterDuff.Mode  SCREEN

Adds the source and destination pixels, then subtracts the source pixels multiplied by the destination. 

PorterDuff.Mode  SRC

The source pixels replace the destination pixels. 

PorterDuff.Mode  SRC_ATOP

Discards the source pixels that do not cover destination pixels. 

PorterDuff.Mode  SRC_IN

Keeps the source pixels that cover the destination pixels, discards the remaining source and destination pixels. 

PorterDuff.Mode  SRC_OUT

Keeps the source pixels that do not cover destination pixels. 

PorterDuff.Mode  SRC_OVER

The source pixels are drawn over the destination pixels. 

PorterDuff.Mode  XOR

Discards the source and destination pixels where source pixels cover destination pixels. 

Public methods

static PorterDuff.Mode valueOf(String name)
static final Mode[] values()

Inherited methods

final Object clone()

Throws CloneNotSupportedException.

final int compareTo(E o)

Compares this enum with the specified object for order.

final boolean equals(Object other)

Returns true if the specified object is equal to this enum constant.

final void finalize()

enum classes cannot have finalize methods.

final Class<E> getDeclaringClass()

Returns the Class object corresponding to this enum constant's enum type.

final int hashCode()

Returns a hash code for this enum constant.

final String name()

Returns the name of this enum constant, exactly as declared in its enum declaration.

final int ordinal()

Returns the ordinal of this enumeration constant (its position in its enum declaration, where the initial constant is assigned an ordinal of zero).

String toString()

Returns the name of this enum constant, as contained in the declaration.

static <T extends Enum<T>> T valueOf(Class<T> enumClass, String name)

Returns the enum constant of the specified enum class with the specified name.

Object clone()

Creates and returns a copy of this object.

boolean equals(Object obj)

Indicates whether some other object is "equal to" this one.

void finalize()

Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.

final Class<?> getClass()

Returns the runtime class of this Object.

int hashCode()

Returns a hash code value for the object.

final void notify()

Wakes up a single thread that is waiting on this object's monitor.

final void notifyAll()

Wakes up all threads that are waiting on this object's monitor.

String toString()

Returns a string representation of the object.

final void wait(long timeoutMillis, int nanos)

Causes the current thread to wait until it is awakened, typically by being notified or interrupted, or until a certain amount of real time has elapsed.

final void wait(long timeoutMillis)

Causes the current thread to wait until it is awakened, typically by being notified or interrupted, or until a certain amount of real time has elapsed.

final void wait()

Causes the current thread to wait until it is awakened, typically by being notified or interrupted.

abstract int compareTo(E o)

Compares this object with the specified object for order.

Enum values

ADD

Added in API level 11
public static final PorterDuff.Mode ADD

Adds the source pixels to the destination pixels and saturates the result.

αout=max(0,min(αsrc+αdst,1))

Cout=max(0,min(Csrc+Cdst,1))

CLEAR

Added in API level 1
public static final PorterDuff.Mode CLEAR

Destination pixels covered by the source are cleared to 0.

αout=0

Cout=0

DARKEN

Added in API level 1
public static final PorterDuff.Mode DARKEN

Retains the smallest component of the source and destination pixels.

αout=αsrc+αdstαsrcαdst

Cout=(1αdst)Csrc+(1αsrc)Cdst+min(Csrc,Cdst)

DST

Added in API level 1
public static final PorterDuff.Mode DST

The source pixels are discarded, leaving the destination intact.

αout=αdst

Cout=Cdst

DST_ATOP

Added in API level 1
public static final PorterDuff.Mode DST_ATOP

Discards the destination pixels that are not covered by source pixels. Draws remaining destination pixels over source pixels.

αout=αsrc

Cout=αsrcCdst+(1αdst)Csrc

DST_IN

Added in API level 1
public static final PorterDuff.Mode DST_IN

Keeps the destination pixels that cover source pixels, discards the remaining source and destination pixels.

αout=αsrcαdst

Cout=Cdstαsrc

DST_OUT

Added in API level 1
public static final PorterDuff.Mode DST_OUT

Keeps the destination pixels that are not covered by source pixels. Discards destination pixels that are covered by source pixels. Discards all source pixels.

αout=(1αsrc)αdst

Cout=(1αsrc)Cdst

DST_OVER

Added in API level 1
public static final PorterDuff.Mode DST_OVER

The source pixels are drawn behind the destination pixels.

αout=αdst+(1αdst)αsrc

Cout=Cdst+(1αdst)Csrc

LIGHTEN

Added in API level 1
public static final PorterDuff.Mode LIGHTEN

Retains the largest component of the source and destination pixel.

αout=αsrc+αdstαsrcαdst

Cout=(1αdst)Csrc+(1αsrc)Cdst+max(Csrc,Cdst)

MULTIPLY

Added in API level 1
public static final PorterDuff.Mode MULTIPLY

Multiplies the source and destination pixels.

αout=αsrcαdst

Cout=CsrcCdst

OVERLAY

Added in API level 11
public static final PorterDuff.Mode OVERLAY

Multiplies or screens the source and destination depending on the destination color.

αout=αsrc+αdstαsrcαdst

Cout={2CsrcCdst2Cdst<αdstαsrcαdst2(αdstCsrc)(αsrcCdst)otherwise

SCREEN

Added in API level 1
public static final PorterDuff.Mode SCREEN

Adds the source and destination pixels, then subtracts the source pixels multiplied by the destination.

αout=αsrc+αdstαsrcαdst

Cout=Csrc+CdstCsrcCdst

SRC

Added in API level 1
public static final PorterDuff.Mode SRC

The source pixels replace the destination pixels.

αout=αsrc

Cout=Csrc

SRC_ATOP

Added in API level 1
public static final PorterDuff.Mode SRC_ATOP

Discards the source pixels that do not cover destination pixels. Draws remaining source pixels over destination pixels.

αout=αdst

Cout=αdstCsrc+(1αsrc)Cdst

SRC_IN

Added in API level 1
public static final PorterDuff.Mode SRC_IN

Keeps the source pixels that cover the destination pixels, discards the remaining source and destination pixels.

αout=αsrcαdst

Cout=Csrcαdst

SRC_OUT

Added in API level 1
public static final PorterDuff.Mode SRC_OUT

Keeps the source pixels that do not cover destination pixels. Discards source pixels that cover destination pixels. Discards all destination pixels.

αout=(1αdst)αsrc

Cout=(1αdst)Csrc

SRC_OVER

Added in API level 1
public static final PorterDuff.Mode SRC_OVER

The source pixels are drawn over the destination pixels.

αout=αsrc+(1αsrc)αdst

Cout=Csrc+(1αsrc)Cdst

XOR

Added in API level 1
public static final PorterDuff.Mode XOR

Discards the source and destination pixels where source pixels cover destination pixels. Draws remaining source pixels.

αout=(1αdst)αsrc+(1αsrc)αdst

Cout=(1αdst)Csrc+(1αsrc)Cdst

Public methods

valueOf

public static PorterDuff.Mode valueOf (String name)

Parameters
name String

Returns
PorterDuff.Mode

values

public static final Mode[] values ()

Returns
Mode[]