We use the class WatermarkPosition
to control the position of a watermark.
WatermarkPosition watermarkPosition = new WatermarkPosition(double position_x, double position_y, double rotation);
WatermarkPosition watermarkPosition = new WatermarkPosition(double position_x, double position_y);
We can set the abscissa and ordinate of the watermark in the constructor, or you can optionally add the rotation angle. The coordinate system starts from the upper left corner of the background image, and the upper left corner is the origin.
If you need to change the align origin point from the upper left corner to another position, you can call the setOrigin(new WatermarkPosition(0.5, 0.5))
method for WatermarkText
or WatermarkImage
later,
Here x, y are both floating point numbers from 0 to 1. The default is 0, which means align to the upper left corner; 0.5, 0.5 means aligns to the absolute center.
The WatermarkPosition
also supports change the position dynamically, androidwm offers several methods for you to modify the positions.
watermarkPosition
.setPositionX(x)
.setPositionY(y)
.setRotation(rotation);
In full coverage mode (Tile mode), the positional parameters of the watermark image will be invalid.
x = y = 0, rotation = 15 | x = y = 0.5, rotation = -15 |
Both the abscissa and the ordinate are a number from 0 to 1, representing a fixed proportion of the background image.
You can set the text color and background color in WatermarkText
:
WatermarkText watermarkText = new WatermarkText(editText)
.setPositionX(0.5)
.setPositionY(0.5)
.setOrigin(new WatermarkPosition(0.5, 0.5))
.setTextSize(30)
.setTextAlpha(200)
.setTextColor(Color.GREEN)
.setBackgroundColor(Color.WHITE); // if not, the background color will be transparent
color = green, background color = white | color = green, background color = default |
You can set the text font by loading a local resource, besides, you can also set the shadow by setTextShadow
.
WatermarkText watermarkText = new WatermarkText(editText)
.setPositionX(0.5)
.setPositionY(0.5)
.setTextSize(40)
.setTextAlpha(200)
.setTextColor(Color.GREEN)
.setTextFont(R.font.champagne)
.setTextShadow(0.1f, 5, 5, Color.BLUE);
font = champagne | shadow = (0.1f, 5, 5, BLUE) |
the four parameters of text shadow is: (blur radius, x offset, y offset, color)
.
The text font size unit and the image size unit are different:
- Text size is as the same unit as the android layout. (will auto adjust with the screen density and background pixels)
- The image size is from 0 to 1, which is the width of the background image percentage.
image size = 0.3 | text size = 40 |
Here is a table of attributes in WatermarkText
and WatermarkImage
that you can custom with:
Method | Description | Default value |
---|---|---|
setPosition | WatermarkPosition for the watermark |
null |
setPositionX | the x-axis coordinates of the watermark | 0 |
setPositionY | the y-axis coordinates of the watermark | 0 |
setRotation | the rotation of the watermark | 0 |
setOrigin | the align origin of the watermark | null |
setOriginX | the abscissa align position of the watermark, between 0 and 1 | 0 |
setOriginY | the ordinate align position of the watermark, between 0 and 1 | 0 |
setTextColor (WatermarkText ) |
the text color of the WatermarkText |
Color.BLACK |
setTextStyle (WatermarkText ) |
the text style of the WatermarkText |
Paint.Style.FILL |
setBackgroundColor (WatermarkText ) |
the background color of the WatermarkText |
null |
setTextAlpha (WatermarkText ) |
the text alpha of the WatermarkText , from 0 to 255 |
50 |
setImageAlpha (WatermarkImage ) |
the text alpha of the WatermarkImage , from 0 to 255 |
50 |
setTextSize (WatermarkText ) |
the text size of the WatermarkText , consistent with the size unit used by the layout |
20 |
setSize (WatermarkImage ) |
the image size of the WatermarkImage , from 0 to 1 (the proportion of background size) |
0.2 |
setTextFont (WatermarkText ) |
typeface of the WatermarkText |
default |
setTextShadow (WatermarkText ) |
shadow of the WatermarkText |
(0, 0, 0) |
setImageDrawable (WatermarkImage ) |
image drawable of the WatermarkImage |
null |
The basic methods of WatermarkImage
are the same as WatermarkText
.