亲宝软件园·资讯

展开

android实现文字水印 android实现文字水印效果 支持多行水印

fulushan的技术专栏 人气:1

特点

支持多行水印,支持自定义角度,支持自定义文字大小。

原理:

使用一个TextView 占据整个页面。在TextView基础上面打水印。

用法:

具体的view在

package cn.fulushan.watermark.view;

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 android.support.annotation.IntRange;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;


import java.util.List;

/**
 * Created by fulushan on 18/4/5.
 */

public class WaterMarkBg extends Drawable {

  private Paint paint = new Paint();
  private List<String> labels;
  private Context context;
  private int degress;//角度
  private int fontSize;//字体大小 单位sp

  /**
   * 初始化构造
   * @param context 上下文
   * @param labels 水印文字列表 多行显示支持
   * @param degress 水印角度
   * @param fontSize 水印文字大小
   */
  public WaterMarkBg(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);
  }
}

具体的用法

public class MainActivity extends AppCompatActivity {
  private TextView bg_tv;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    bg_tv = findViewById(R.id.bg_tv);

    SimpleDateFormat createTimeSdf1 = new SimpleDateFormat("yyyy-MM-dd");

    List<String> labels = new ArrayList<>();
    labels.add("用户名:张三");
    labels.add("日期:"+ createTimeSdf1.format(new Date()));
    labels.add("不可扩散");
    bg_tv.setBackgroundDrawable(new WaterMarkBg(MainActivity.this,labels,-30,13));


  }
}

效果图

github源码地址

加载全部内容

相关教程
猜你喜欢
用户评论