java.lang.Object | |
↳ | android.graphics.Paint |
Known Direct Subclasses |
The Paint class holds the style and color information about how to draw geometries, text and bitmaps.
Nested Classes | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Paint.Align | Align specifies how drawText aligns its text relative to the [x,y] coordinates. | ||||||||||
Paint.Cap | The Cap specifies the treatment for the beginning and ending of stroked lines and paths. | ||||||||||
Paint.FontMetrics | Class that describes the various metrics for a font at a given text size. | ||||||||||
Paint.FontMetricsInt | Convenience method for callers that want to have FontMetrics values as integers. | ||||||||||
Paint.Join | The Join specifies the treatment where lines and curve segments join on a stroked path. | ||||||||||
Paint.Style | The Style specifies if the primitive being drawn is filled, stroked, or both (in the same color). |
Constants | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
int | ANTI_ALIAS_FLAG | bit mask for the flag enabling antialiasing | |||||||||
int | DEV_KERN_TEXT_FLAG | bit mask for the flag enabling device kerning for text | |||||||||
int | DITHER_FLAG | bit mask for the flag enabling dithering | |||||||||
int | FAKE_BOLD_TEXT_FLAG | bit mask for the flag enabling fake-bold text | |||||||||
int | FILTER_BITMAP_FLAG | bit mask for the flag enabling bitmap filtering | |||||||||
int | HINTING_OFF | Option for setHinting(int) : disable hinting. |
|||||||||
int | HINTING_ON | Option for setHinting(int) : enable hinting. |
|||||||||
int | LINEAR_TEXT_FLAG | bit mask for the flag enabling linear-text (no caching) | |||||||||
int | STRIKE_THRU_TEXT_FLAG | bit mask for the flag enabling strike-thru text | |||||||||
int | SUBPIXEL_TEXT_FLAG | bit mask for the flag enabling subpixel-text | |||||||||
int | UNDERLINE_TEXT_FLAG | bit mask for the flag enabling underline text |
Public Constructors | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Create a new paint with default settings.
| |||||||||||
Create a new paint with the specified flags.
| |||||||||||
Create a new paint, initialized with the attributes in the specified
paint parameter.
|
Public Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Return the distance above (negative) the baseline (ascent) based on the
current typeface and text size.
| |||||||||||
Measure the text, stopping early if the measured width exceeds maxWidth.
| |||||||||||
Measure the text, stopping early if the measured width exceeds maxWidth.
| |||||||||||
Measure the text, stopping early if the measured width exceeds maxWidth.
| |||||||||||
Clear the shadow layer.
| |||||||||||
Return the distance below (positive) the baseline (descent) based on the
current typeface and text size.
| |||||||||||
Helper to getColor() that just returns the color's alpha value.
| |||||||||||
Return the paint's color.
| |||||||||||
Get the paint's colorfilter (maybe be null).
| |||||||||||
Applies any/all effects (patheffect, stroking) to src, returning the
result in dst.
| |||||||||||
Return the paint's flags.
| |||||||||||
Return the font's recommended interline spacing, given the Paint's
settings for typeface, textSize, etc.
| |||||||||||
Allocates a new FontMetrics object, and then calls getFontMetrics(fm)
with it, returning the object.
| |||||||||||
Return the font's interline spacing, given the Paint's settings for
typeface, textSize, etc.
| |||||||||||
Return the recommend line spacing based on the current typeface and
text size.
| |||||||||||
Return the paint's hinting mode.
| |||||||||||
Get the paint's maskfilter object.
| |||||||||||
Get the paint's patheffect object.
| |||||||||||
Get the paint's rasterizer (or null).
| |||||||||||
Get the paint's shader object.
| |||||||||||
Return the paint's Cap, controlling how the start and end of stroked
lines and paths are treated.
| |||||||||||
Return the paint's stroke join type.
| |||||||||||
Return the paint's stroke miter value.
| |||||||||||
Return the width for stroking.
| |||||||||||
Return the paint's style, used for controlling how primitives'
geometries are interpreted (except for drawBitmap, which always assumes
FILL_STYLE).
| |||||||||||
Return the paint's Align value for drawing text.
| |||||||||||
Return in bounds (allocated by the caller) the smallest rectangle that
encloses all of the characters, with an implied origin at (0,0).
| |||||||||||
Return in bounds (allocated by the caller) the smallest rectangle that
encloses all of the characters, with an implied origin at (0,0).
| |||||||||||
Get the text Locale.
| |||||||||||
Return the path (outline) for the specified text.
| |||||||||||
Return the path (outline) for the specified text.
| |||||||||||
Return the paint's horizontal scale factor for text.
| |||||||||||
Return the paint's text size.
| |||||||||||
Return the paint's horizontal skew factor for text.
| |||||||||||
Return the advance widths for the characters in the string.
| |||||||||||
Return the advance widths for the characters in the string.
| |||||||||||
Return the advance widths for the characters in the string.
| |||||||||||
Return the advance widths for the characters in the string.
| |||||||||||
Get the paint's typeface object.
| |||||||||||
Get the paint's xfermode object.
| |||||||||||
Helper for getFlags(), returning true if ANTI_ALIAS_FLAG bit is set
AntiAliasing smooths out the edges of what is being drawn, but is has
no impact on the interior of the shape.
| |||||||||||
Helper for getFlags(), returning true if DITHER_FLAG bit is set
Dithering affects how colors that are higher precision than the device
are down-sampled.
| |||||||||||
Helper for getFlags(), returning true if FAKE_BOLD_TEXT_FLAG bit is set
| |||||||||||
Whether or not the bitmap filter is activated.
| |||||||||||
Helper for getFlags(), returning true if LINEAR_TEXT_FLAG bit is set
| |||||||||||
Helper for getFlags(), returning true if STRIKE_THRU_TEXT_FLAG bit is set
| |||||||||||
Helper for getFlags(), returning true if SUBPIXEL_TEXT_FLAG bit is set
| |||||||||||
Helper for getFlags(), returning true if UNDERLINE_TEXT_FLAG bit is set
| |||||||||||
Return the width of the text.
| |||||||||||
Return the width of the text.
| |||||||||||
Return the width of the text.
| |||||||||||
Return the width of the text.
| |||||||||||
Restores the paint to its default settings.
| |||||||||||
Copy the fields from src into this paint.
| |||||||||||
Helper to setColor(), that takes a,r,g,b and constructs the color int
| |||||||||||
Helper to setColor(), that only assigns the color's alpha value,
leaving its r,g,b values unchanged.
| |||||||||||
Helper for setFlags(), setting or clearing the ANTI_ALIAS_FLAG bit
AntiAliasing smooths out the edges of what is being drawn, but is has
no impact on the interior of the shape.
| |||||||||||
Set the paint's color.
| |||||||||||
Set or clear the paint's colorfilter, returning the parameter.
| |||||||||||
Helper for setFlags(), setting or clearing the DITHER_FLAG bit
Dithering affects how colors that are higher precision than the device
are down-sampled.
| |||||||||||
Helper for setFlags(), setting or clearing the FAKE_BOLD_TEXT_FLAG bit
| |||||||||||
Helper for setFlags(), setting or clearing the FILTER_BITMAP_FLAG bit.
| |||||||||||
Set the paint's flags.
| |||||||||||
Set the paint's hinting mode.
| |||||||||||
Helper for setFlags(), setting or clearing the LINEAR_TEXT_FLAG bit
| |||||||||||
Set or clear the maskfilter object.
| |||||||||||
Set or clear the patheffect object.
| |||||||||||
Set or clear the rasterizer object.
| |||||||||||
Set or clear the shader object.
| |||||||||||
This draws a shadow layer below the main layer, with the specified
offset and color, and blur radius.
| |||||||||||
Helper for setFlags(), setting or clearing the STRIKE_THRU_TEXT_FLAG bit
| |||||||||||
Set the paint's Cap.
| |||||||||||
Set the paint's Join.
| |||||||||||
Set the paint's stroke miter value.
| |||||||||||
Set the width for stroking.
| |||||||||||
Set the paint's style, used for controlling how primitives'
geometries are interpreted (except for drawBitmap, which always assumes
Fill).
| |||||||||||
Helper for setFlags(), setting or clearing the SUBPIXEL_TEXT_FLAG bit
| |||||||||||
Set the paint's text alignment.
| |||||||||||
Set the text locale.
| |||||||||||
Set the paint's horizontal scale factor for text.
| |||||||||||
Set the paint's text size.
| |||||||||||
Set the paint's horizontal skew factor for text.
| |||||||||||
Set or clear the typeface object.
| |||||||||||
Helper for setFlags(), setting or clearing the UNDERLINE_TEXT_FLAG bit
| |||||||||||
Set or clear the xfermode object.
|
Protected Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Invoked when the garbage collector has detected that this instance is no longer reachable.
|
[Expand]
Inherited Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
From class
java.lang.Object
|
bit mask for the flag enabling antialiasing
bit mask for the flag enabling device kerning for text
bit mask for the flag enabling dithering
bit mask for the flag enabling fake-bold text
bit mask for the flag enabling bitmap filtering
Option for setHinting(int)
: disable hinting.
Option for setHinting(int)
: enable hinting.
bit mask for the flag enabling linear-text (no caching)
bit mask for the flag enabling strike-thru text
bit mask for the flag enabling subpixel-text
bit mask for the flag enabling underline text
Create a new paint with the specified flags. Use setFlags() to change these after the paint is created.
flags | initial flag bits, as if they were passed via setFlags(). |
---|
Create a new paint, initialized with the attributes in the specified paint parameter.
paint | Existing paint used to initialized the attributes of the new paint. |
---|
Return the distance above (negative) the baseline (ascent) based on the current typeface and text size.
Measure the text, stopping early if the measured width exceeds maxWidth. Return the number of chars that were measured, and if measuredWidth is not null, return in it the actual width measured.
text | The text to measure. Cannot be null. |
---|---|
start | The offset into text to begin measuring at |
end | The end of the text slice to measure. |
measureForwards | If true, measure forwards, starting at start. Otherwise, measure backwards, starting with end. |
maxWidth | The maximum width to accumulate. |
measuredWidth | Optional. If not null, returns the actual width measured. |
Measure the text, stopping early if the measured width exceeds maxWidth. Return the number of chars that were measured, and if measuredWidth is not null, return in it the actual width measured.
text | The text to measure. Cannot be null. |
---|---|
measureForwards | If true, measure forwards, starting with the first character in the string. Otherwise, measure backwards, starting with the last character in the string. |
maxWidth | The maximum width to accumulate. |
measuredWidth | Optional. If not null, returns the actual width measured. |
Measure the text, stopping early if the measured width exceeds maxWidth. Return the number of chars that were measured, and if measuredWidth is not null, return in it the actual width measured.
text | The text to measure. Cannot be null. |
---|---|
index | The offset into text to begin measuring at |
count | The number of maximum number of entries to measure. If count is negative, then the characters are measured in reverse order. |
maxWidth | The maximum width to accumulate. |
measuredWidth | Optional. If not null, returns the actual width measured. |
Return the distance below (positive) the baseline (descent) based on the current typeface and text size.
Helper to getColor() that just returns the color's alpha value. This is the same as calling getColor() >>> 24. It always returns a value between 0 (completely transparent) and 255 (completely opaque).
Return the paint's color. Note that the color is a 32bit value containing alpha as well as r,g,b. This 32bit value is not premultiplied, meaning that its alpha can be any value, regardless of the values of r,g,b. See the Color class for more details.
Get the paint's colorfilter (maybe be null).
Applies any/all effects (patheffect, stroking) to src, returning the result in dst. The result is that drawing src with this paint will be the same as drawing dst with a default paint (at least from the geometric perspective).
src | input path |
---|---|
dst | output path (may be the same as src) |
Return the paint's flags. Use the Flag enum to test flag values.
Return the font's recommended interline spacing, given the Paint's settings for typeface, textSize, etc. If metrics is not null, return the fontmetric values in it.
metrics | If this object is not null, its fields are filled with the appropriate values given the paint's text attributes. |
---|
Allocates a new FontMetrics object, and then calls getFontMetrics(fm) with it, returning the object.
Return the font's interline spacing, given the Paint's settings for typeface, textSize, etc. If metrics is not null, return the fontmetric values in it. Note: all values have been converted to integers from floats, in such a way has to make the answers useful for both spacing and clipping. If you want more control over the rounding, call getFontMetrics().
Return the recommend line spacing based on the current typeface and text size.
Return the paint's hinting mode. Returns either
HINTING_OFF
or HINTING_ON
.
Get the paint's maskfilter object.
Get the paint's patheffect object.
Get the paint's rasterizer (or null).
The raster controls/modifies how paths/text are turned into alpha masks.Get the paint's shader object.
Return the paint's Cap, controlling how the start and end of stroked lines and paths are treated.
Return the paint's stroke join type.
Return the paint's stroke miter value. Used to control the behavior of miter joins when the joins angle is sharp.
Return the width for stroking.
A value of 0 strokes in hairline mode. Hairlines always draws a single pixel independent of the canva's matrix.Return the paint's style, used for controlling how primitives' geometries are interpreted (except for drawBitmap, which always assumes FILL_STYLE).
Return the paint's Align value for drawing text. This controls how the text is positioned relative to its origin. LEFT align means that all of the text will be drawn to the right of its origin (i.e. the origin specifieds the LEFT edge of the text) and so on.
Return in bounds (allocated by the caller) the smallest rectangle that encloses all of the characters, with an implied origin at (0,0).
text | Array of chars to measure and return their unioned bounds |
---|---|
index | Index of the first char in the array to measure |
count | The number of chars, beginning at index, to measure |
bounds | Returns the unioned bounds of all the text. Must be allocated by the caller. |
Return in bounds (allocated by the caller) the smallest rectangle that encloses all of the characters, with an implied origin at (0,0).
text | String to measure and return its bounds |
---|---|
start | Index of the first char in the string to measure |
end | 1 past the last char in the string measure |
bounds | Returns the unioned bounds of all the text. Must be allocated by the caller. |
Get the text Locale.
Return the path (outline) for the specified text. Note: just like Canvas.drawText, this will respect the Align setting in the paint.
text | The text to retrieve the path from |
---|---|
start | The first character in the text |
end | 1 past the last charcter in the text |
x | The x coordinate of the text's origin |
y | The y coordinate of the text's origin |
path | The path to receive the data describing the text. Must be allocated by the caller. |
Return the path (outline) for the specified text. Note: just like Canvas.drawText, this will respect the Align setting in the paint.
text | The text to retrieve the path from |
---|---|
index | The index of the first character in text |
count | The number of characterss starting with index |
x | The x coordinate of the text's origin |
y | The y coordinate of the text's origin |
path | The path to receive the data describing the text. Must be allocated by the caller. |
Return the paint's horizontal scale factor for text. The default value is 1.0.
Return the paint's text size.
Return the paint's horizontal skew factor for text. The default value is 0.
Return the advance widths for the characters in the string.
text | The text to measure |
---|---|
widths | array to receive the advance widths of the characters. Must be at least a large as the text. |
Return the advance widths for the characters in the string.
text | The text to measure. Cannot be null. |
---|---|
start | The index of the first char to to measure |
end | The end of the text slice to measure |
widths | array to receive the advance widths of the characters. Must be at least a large as (end - start). |
Return the advance widths for the characters in the string.
text | The text to measure. Cannot be null. |
---|---|
start | The index of the first char to to measure |
end | The end of the text slice to measure |
widths | array to receive the advance widths of the characters. Must be at least a large as the text. |
Return the advance widths for the characters in the string.
text | The text to measure. Cannot be null. |
---|---|
index | The index of the first char to to measure |
count | The number of chars starting with index to measure |
widths | array to receive the advance widths of the characters. Must be at least a large as count. |
Get the paint's typeface object.
The typeface object identifies which font to use when drawing or measuring text.Get the paint's xfermode object.
Helper for getFlags(), returning true if ANTI_ALIAS_FLAG bit is set AntiAliasing smooths out the edges of what is being drawn, but is has no impact on the interior of the shape. See setDither() and setFilterBitmap() to affect how colors are treated.
Helper for getFlags(), returning true if DITHER_FLAG bit is set Dithering affects how colors that are higher precision than the device are down-sampled. No dithering is generally faster, but higher precision colors are just truncated down (e.g. 8888 -> 565). Dithering tries to distribute the error inherent in this process, to reduce the visual artifacts.
Helper for getFlags(), returning true if FAKE_BOLD_TEXT_FLAG bit is set
Whether or not the bitmap filter is activated. Filtering affects the sampling of bitmaps when they are transformed. Filtering does not affect how the colors in the bitmap are converted into device pixels. That is dependent on dithering and xfermodes.
Helper for getFlags(), returning true if LINEAR_TEXT_FLAG bit is set
Helper for getFlags(), returning true if STRIKE_THRU_TEXT_FLAG bit is set
Helper for getFlags(), returning true if SUBPIXEL_TEXT_FLAG bit is set
Helper for getFlags(), returning true if UNDERLINE_TEXT_FLAG bit is set
Return the width of the text.
text | The text to measure. Cannot be null. |
---|
Return the width of the text.
text | The text to measure |
---|---|
start | The index of the first character to start measuring |
end | 1 beyond the index of the last character to measure |
Return the width of the text.
text | The text to measure. Cannot be null. |
---|---|
start | The index of the first character to start measuring |
end | 1 beyond the index of the last character to measure |
Return the width of the text.
text | The text to measure. Cannot be null. |
---|---|
index | The index of the first character to start measuring |
count | THe number of characters to measure, beginning with start |
Copy the fields from src into this paint. This is equivalent to calling get() on all of the src fields, and calling the corresponding set() methods on this.
Helper to setColor(), that takes a,r,g,b and constructs the color int
a | The new alpha component (0..255) of the paint's color. |
---|---|
r | The new red component (0..255) of the paint's color. |
g | The new green component (0..255) of the paint's color. |
b | The new blue component (0..255) of the paint's color. |
Helper to setColor(), that only assigns the color's alpha value, leaving its r,g,b values unchanged. Results are undefined if the alpha value is outside of the range [0..255]
a | set the alpha component [0..255] of the paint's color. |
---|
Helper for setFlags(), setting or clearing the ANTI_ALIAS_FLAG bit AntiAliasing smooths out the edges of what is being drawn, but is has no impact on the interior of the shape. See setDither() and setFilterBitmap() to affect how colors are treated.
aa | true to set the antialias bit in the flags, false to clear it |
---|
Set the paint's color. Note that the color is an int containing alpha as well as r,g,b. This 32bit value is not premultiplied, meaning that its alpha can be any value, regardless of the values of r,g,b. See the Color class for more details.
color | The new color (including alpha) to set in the paint. |
---|
Set or clear the paint's colorfilter, returning the parameter.
filter | May be null. The new filter to be installed in the paint |
---|
Helper for setFlags(), setting or clearing the DITHER_FLAG bit Dithering affects how colors that are higher precision than the device are down-sampled. No dithering is generally faster, but higher precision colors are just truncated down (e.g. 8888 -> 565). Dithering tries to distribute the error inherent in this process, to reduce the visual artifacts.
dither | true to set the dithering bit in flags, false to clear it |
---|
Helper for setFlags(), setting or clearing the FAKE_BOLD_TEXT_FLAG bit
fakeBoldText | true to set the fakeBoldText bit in the paint's flags, false to clear it. |
---|
Helper for setFlags(), setting or clearing the FILTER_BITMAP_FLAG bit. Filtering affects the sampling of bitmaps when they are transformed. Filtering does not affect how the colors in the bitmap are converted into device pixels. That is dependent on dithering and xfermodes.
filter | true to set the FILTER_BITMAP_FLAG bit in the paint's flags, false to clear it. |
---|
Set the paint's flags. Use the Flag enum to specific flag values.
flags | The new flag bits for the paint |
---|
Set the paint's hinting mode. May be either
HINTING_OFF
or HINTING_ON
.
Helper for setFlags(), setting or clearing the LINEAR_TEXT_FLAG bit
linearText | true to set the linearText bit in the paint's flags, false to clear it. |
---|
Set or clear the maskfilter object.
Pass null to clear any previous maskfilter. As a convenience, the parameter passed is also returned.maskfilter | May be null. The maskfilter to be installed in the paint |
---|
Set or clear the patheffect object.
Pass null to clear any previous patheffect. As a convenience, the parameter passed is also returned.effect | May be null. The patheffect to be installed in the paint |
---|
Set or clear the rasterizer object.
Pass null to clear any previous rasterizer. As a convenience, the parameter passed is also returned.rasterizer | May be null. The new rasterizer to be installed in the paint. |
---|
Set or clear the shader object.
Pass null to clear any previous shader. As a convenience, the parameter passed is also returned.shader | May be null. the new shader to be installed in the paint |
---|
This draws a shadow layer below the main layer, with the specified offset and color, and blur radius. If radius is 0, then the shadow layer is removed.
Helper for setFlags(), setting or clearing the STRIKE_THRU_TEXT_FLAG bit
strikeThruText | true to set the strikeThruText bit in the paint's flags, false to clear it. |
---|
Set the paint's Cap.
cap | set the paint's line cap style, used whenever the paint's style is Stroke or StrokeAndFill. |
---|
Set the paint's Join.
join | set the paint's Join, used whenever the paint's style is Stroke or StrokeAndFill. |
---|
Set the paint's stroke miter value. This is used to control the behavior of miter joins when the joins angle is sharp. This value must be >= 0.
miter | set the miter limit on the paint, used whenever the paint's style is Stroke or StrokeAndFill. |
---|
Set the width for stroking. Pass 0 to stroke in hairline mode. Hairlines always draws a single pixel independent of the canva's matrix.
width | set the paint's stroke width, used whenever the paint's style is Stroke or StrokeAndFill. |
---|
Set the paint's style, used for controlling how primitives' geometries are interpreted (except for drawBitmap, which always assumes Fill).
style | The new style to set in the paint |
---|
Helper for setFlags(), setting or clearing the SUBPIXEL_TEXT_FLAG bit
subpixelText | true to set the subpixelText bit in the paint's flags, false to clear it. |
---|
Set the paint's text alignment. This controls how the text is positioned relative to its origin. LEFT align means that all of the text will be drawn to the right of its origin (i.e. the origin specifieds the LEFT edge of the text) and so on.
align | set the paint's Align value for drawing text. |
---|
Set the text locale.
The text locale affects how the text is drawn for some languages.
For example, if the locale is CHINESE
or CHINA
,
then the text renderer will prefer to draw text using a Chinese font. Likewise,
if the locale is JAPANESE
or JAPAN
, then the text
renderer will prefer to draw text using a Japanese font.
This distinction is important because Chinese and Japanese text both use many
of the same Unicode code points but their appearance is subtly different for
each language.
By default, the text locale is initialized to the system locale (as returned
by getDefault()
). This assumes that the text to be rendered will
most likely be in the user's preferred language.
If the actual language of the text is known, then it can be provided to the
text renderer using this method. The text renderer may attempt to guess the
language script based on the contents of the text to be drawn independent of
the text locale here. Specifying the text locale just helps it do a better
job in certain ambiguous cases
locale | the paint's locale value for drawing text, must not be null. |
---|
Set the paint's horizontal scale factor for text. The default value is 1.0. Values > 1.0 will stretch the text wider. Values < 1.0 will stretch the text narrower.
scaleX | set the paint's scale in X for drawing/measuring text. |
---|
Set the paint's text size. This value must be > 0
textSize | set the paint's text size. |
---|
Set the paint's horizontal skew factor for text. The default value is 0. For approximating oblique text, use values around -0.25.
skewX | set the paint's skew factor in X for drawing text. |
---|
Set or clear the typeface object.
Pass null to clear any previous typeface. As a convenience, the parameter passed is also returned.typeface | May be null. The typeface to be installed in the paint |
---|
Helper for setFlags(), setting or clearing the UNDERLINE_TEXT_FLAG bit
underlineText | true to set the underlineText bit in the paint's flags, false to clear it. |
---|
Set or clear the xfermode object.
Pass null to clear any previous xfermode. As a convenience, the parameter passed is also returned.xfermode | May be null. The xfermode to be installed in the paint |
---|
Invoked when the garbage collector has detected that this instance is no longer reachable. The default implementation does nothing, but this method can be overridden to free resources.
Note that objects that override finalize
are significantly more expensive than
objects that don't. Finalizers may be run a long time after the object is no longer
reachable, depending on memory pressure, so it's a bad idea to rely on them for cleanup.
Note also that finalizers are run on a single VM-wide finalizer thread,
so doing blocking work in a finalizer is a bad idea. A finalizer is usually only necessary
for a class that has a native peer and needs to call a native method to destroy that peer.
Even then, it's better to provide an explicit close
method (and implement
Closeable
), and insist that callers manually dispose of instances. This
works well for something like files, but less well for something like a BigInteger
where typical calling code would have to deal with lots of temporaries. Unfortunately,
code that creates lots of temporaries is the worst kind of code from the point of view of
the single finalizer thread.
If you must use finalizers, consider at least providing your own
ReferenceQueue
and having your own thread process that queue.
Unlike constructors, finalizers are not automatically chained. You are responsible for
calling super.finalize()
yourself.
Uncaught exceptions thrown by finalizers are ignored and do not terminate the finalizer thread. See Effective Java Item 7, "Avoid finalizers" for more.
Throwable |
---|