android實(shí)現(xiàn)文字水印效果 支持多行水印
特點(diǎn)
支持多行水印,支持自定義角度,支持自定義文字大小。
原理:
使用一個(gè)TextView 占據(jù)整個(gè)頁面。在TextView基礎(chǔ)上面打水印。
用法:
具體的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
/**
* 初始化構(gòu)造
* @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("不可擴(kuò)散");
bg_tv.setBackgroundDrawable(new WaterMarkBg(MainActivity.this,labels,-30,13));
}
}
效果圖

github源碼地址
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android 詳解沉浸式狀態(tài)欄的實(shí)現(xiàn)流程
沉浸式就是要給用戶提供完全沉浸的體驗(yàn),使用戶有一種置身于虛擬世界之中的感覺。沉浸式模式就是整個(gè)屏幕中顯示都是應(yīng)用的內(nèi)容,沒有狀態(tài)欄也沒有導(dǎo)航欄,用戶不會(huì)被一些系統(tǒng)的界面元素所打擾,讓我們來實(shí)現(xiàn)下網(wǎng)上傳的沸沸揚(yáng)揚(yáng)的安卓沉浸式狀態(tài)欄2021-11-11
Android開發(fā)之獲取網(wǎng)絡(luò)鏈接狀態(tài)
這篇文章主要介紹了Android獲取網(wǎng)絡(luò)鏈接狀態(tài)的方法,主要是通過ConnectivityManager類來完成的,需要的朋友可以參考下2014-08-08
android實(shí)現(xiàn)點(diǎn)擊按鈕切換不同的fragment布局
這篇文章主要為大家詳細(xì)介紹了android實(shí)現(xiàn)點(diǎn)擊按鈕切換不同的fragment布局,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-12-12
Android使用ViewPager快速切換Fragment時(shí)卡頓的優(yōu)化方案
今天小編就為大家分享一篇關(guān)于Android使用ViewPager快速切換Fragment時(shí)卡頓的優(yōu)化方案,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2018-12-12
Android自定義ViewGroup實(shí)現(xiàn)標(biāo)簽流容器FlowLayout
這篇文章主要介紹了Android自定義ViewGroup實(shí)現(xiàn)FlowLayout標(biāo)簽流容器,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-09-09
android的activity跳轉(zhuǎn)到另一個(gè)activity
這篇文章主要介紹了android實(shí)現(xiàn)從一個(gè)activity跳轉(zhuǎn)到另一個(gè)activity中去2013-11-11

