Customizing the View watermark layout WaterMark foreground color
Directory
Directory
Use cases in the project
Watermark Layout Markframelayout
Custom properties
Use cases in the project
The project requires that watermarks be added to all pages, in which case the watermark layout can be set as the root layout in baseactivity
Foreground color style:
Background color style:
Layout:
<com.bqt.lock.MarkFrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/mark_layout" android:layout_width="match_parent" android:layout_height="match_parent" app:mark_is_foreground="false" app:mark_show_value="包青天" app:mark_textcolor="#fff"> <TextView android:id="@+id/tv" android:layout_width="match_parent" android:layout_height="100dp" android:background="#f00" android:gravity="center"/> <ImageView android:layout_width="match_parent" android:layout_height="100dp" android:layout_marginTop="200dp" android:scaleType="centerCrop" android:src="@drawable/icon"/></com.bqt.lock.MarkFrameLayout>
Watermark Layout Markframelayout
When you draw a watermark, you can choose to draw the foreground color on ondrawforeground
(above all views), or you can choose to draw a background color on onDraw
(it will be obscured by all view backgrounds).
If you need to use a watermark layout that inherits from other layout
, simply change the inherited class to relativelayout
or linearlayout
. Nothing else needs to be changed.
public class Markframelayout extends Framelayout {private static final int default_degreses = -15;//watermark Tilt Angle Private static final int default_mark_paint_color = Color.parsecolor ("#FFCCCCCC");//Watermark color private static final int Default_alpha = (int) (0.5 * 255);//watermark Transparency private static final String Default_mark_show_value = "[watermark]";//Watermark Content Private Boolean sho Wmark = true; private float mmarktextsize; private int mmarktextcolor; Private Boolean mmarklayerisforeground; Whether the watermark is drawn on the background of the control or the private float mdegrees on the foreground color; private int mverticalspacing; private int mhorizontalspacing; private int mmarkpainalpha; Private String Mmarkvalue; Private Textpaint Mmarkpaint; Private Bitmap Mmarkbitmap; Public Markframelayout (@NonNull context context) {This (context, NULL); } public Markframelayout (@NonNull context context, @Nullable AttributeSet attrs) {Super (context, attrs); if (showmark) {int defaultmarktextsize = (int) TypedvaLue.applydimension (TYPEDVALUE.COMPLEX_UNIT_SP, N, Getresources (). Getdisplaymetrics ()); int defaultspacing = (int) typedvalue.applydimension (Typedvalue.complex_unit_dip, Getresources (). Getdisplaymetrics ()); TypedArray a = Context.obtainstyledattributes (Attrs, r.styleable.markframelayout); Mdegrees = A.getinteger (r.styleable.markframelayout_mark_rotate_degrees, default_degreses); Mmarktextcolor = A.getcolor (R.styleable.markframelayout_mark_textcolor, Default_mark_paint_color); Mmarktextsize = A.getdimension (r.styleable.markframelayout_mark_textsize, defaultmarktextsize); Mmarkpainalpha = A.getint (R.styleable.markframelayout_mark_alpha, Default_alpha); Mmarklayerisforeground = A.getboolean (R.styleable.markframelayout_mark_is_foreground, true);//The default is drawn on the foreground color MHorizo ntalspacing = (int) a.getdimension (r.styleable.markframelayout_mark_hor_spacing, defaultspacing); mverticalspacing = (int) a.geTdimension (r.styleable.markframelayout_mark_ver_spacing, defaultspacing); Mmarkvalue = a.getstring (R.styleable.markframelayout_mark_show_value); Mmarkvalue = Textutils.isempty (mmarkvalue)? Default_mark_show_value:mmarkvalue; A.recycle (); Initwaterpaint (); Setforeground (New colordrawable (color.transparent)); Reset foreground color Transparent}} @Override public void Ondrawforeground (canvas canvas) {Super.ondrawforeground (canvas ); if (Showmark && mmarklayerisforeground) {drawmark (canvas);//Draw foreground color}} @Override Prote CTED void OnDraw (canvas canvas) {super.ondraw (canvas); if (Showmark &&!mmarklayerisforeground) {drawmark (canvas);//Draw by Color}} private void Init Waterpaint () {//Initialize Mark's Paint mmarkpaint = new Textpaint (paint.anti_alias_flag);//mmarkpaint.setantialias ( true) Mmarkpaint.setcolor (Mmarktextcolor); MmarkpaiNt.setalpha (Mmarkpainalpha); Mmarkpaint.settextsize (mmarktextsize); Initialize markbitmap paint.fontmetrics fontmetrics = Mmarkpaint.getfontmetrics (); int textHeight = (int) (fontmetrics.bottom-fontmetrics.top); int textLength = (int) mmarkpaint.measuretext (mmarkvalue); Mmarkbitmap = Bitmap.createbitmap (textLength + 2 * mhorizontalspacing, TextHeight + mverticalspacing * 2, B Itmap. config.argb_8888); Canvas canvas = new canvas (MMARKBITMAP); Canvas.drawtext (Mmarkvalue, mhorizontalspacing, mverticalspacing, Mmarkpaint); } private void Drawmark (canvas canvas) {int maxSize = Math.max (Getmeasuredwidth (), Getmeasuredheight ()); Mmarkpaint.setshader (New Bitmapshader (Mmarkbitmap, Shader.TileMode.REPEAT, Shader.TileMode.REPEAT)); Canvas.save (); Canvas.translate (-(Maxsize-getmeasuredwidth ())/2, 0); Canvas.rotate (Mdegrees, MAXSIZE/2, MAXSIZE/2); Canvas.drawrect (New RECTF (0, 0, MaxSize, MaxSize), mmarkpaint); Canvas.restore (); The public void Setshowmark (Boolean showmark) {This.showmark = Showmark; Invalidate (); }}
Custom properties
<?xml version="1.0" encoding="utf-8"?><resources> <declare-styleable name="basicres_MarkFrameLayout"> <attr name="basicres_mark_rotate_degrees" format="integer" /> <attr name="basicres_mark_textcolor" format="color|reference" /> <attr name="basicres_mark_textsize" format="dimension" /> <attr name="basicres_mark_alpha" format="integer" /> <attr name="basicres_mark_is_foreground" format="boolean" /> <attr name="basicres_mark_hor_spacing" format="dimension" /> <attr name="basicres_mark_ver_spacing" format="dimension" /> <attr name="basicres_mark_show_value" format="string" /> </declare-styleable></resources>
2018-10-13 11:59:36 Saturday
Customizing the View watermark layout WaterMark foreground color