一、自定义VIew,添加VIew的带文字斜体的背景:MarkBgView
package com.zdmtech.backgroundtextdemo; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.ColorFilter; import android.graphics.Paint; import android.graphics.PixelFormat; import android.graphics.drawable.Drawable; import androidx.annotation.IntRange; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import java.util.List; public class MarkBgView extends Drawable { private Paint paint = new Paint(); private List<String> labels; private Context context; private int degress; // 角度 private int fontSize; // 字体大小 单位sp public MarkBgView(Context context, List<String> labels, int degress, int fontSize) { this.labels = labels; this.context = context; this.degress = degress; this.fontSize = fontSize; } @Override public void draw(@NonNull Canvas canvas) { int width = getBounds().right; int height = getBounds().bottom; // 画布背景色 canvas.drawColor(Color.parseColor("#40F3F5F9")); // 水印文字的颜色 paint.setColor(Color.parseColor("#50AEAEAE")); paint.setAntiAlias(true); paint.setTextSize(sp2px(context, fontSize)); canvas.save(); canvas.rotate(degress); float textWidth = paint.measureText(labels.get(0)); int index = 0; for (int positionY = height / 10; positionY <= height; positionY += height / 10 + 80) { float fromX = -width + (index++ % 2) * textWidth; for (float positionX = fromX; positionX < width; positionX += textWidth * 2) { int spacing = 0;//间距 for (String label : labels) { canvas.drawText(label, positionX, positionY + spacing, paint); spacing = spacing + 50; } } } canvas.restore(); } @Override public void setAlpha(@IntRange(from = 0, to = 255) int alpha) { } @Override public void setColorFilter(@Nullable ColorFilter colorFilter) { } @Override public int getOpacity() { return PixelFormat.UNKNOWN; } public static int sp2px(Context context, float spValue) { final float fontScale = context.getResources().getDisplayMetrics().scaledDensity; return (int) (spValue * fontScale + 0.5f); } } 二、Activity
1.activity代码:MarkViewActivity
package com.zdmtech.backgroundtextdemo; import android.os.Bundle; import android.view.WindowManager; import android.widget.RelativeLayout; import androidx.annotation.Nullable; import androidx.appcompat.app.AppCompatActivity; import java.util.ArrayList; import java.util.List; /** * 设置防截屏、添加水印 * */ public class MarkViewActivity extends AppCompatActivity { @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); //设置防截屏 getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE); setContentView(R.layout.activity_main); initView(); } public void initView() { RelativeLayout reView = findViewById(R.id.reView); // 添加水印 List<String> labels = new ArrayList<>(); labels.add("你"); labels.add("我"); labels.add("他"); reView.setBackground(new MarkBgView(MarkViewActivity.this, labels, -30, 14)); } }
2.布局xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/reView" android:orientation="vertical"> <TextView android:id="@+id/tcContent" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:text="防截屏" /> </RelativeLayout>