網站首頁 編程語言 正文
本文實例為大家分享了Android設置重復文字水印背景的具體代碼,供大家參考,具體內容如下
效果如圖:
自定義Drawable :
/**
?* Created by Owen Chan
?* On 2017-07-05.
?*/
public class WaterMarkBg extends Drawable {
? ? private Paint paint = new Paint();
? ? private String logo = "SoYoung";
? ? public WaterMarkBg(String logo) {
? ? ? ? this.logo = logo;
? ? }
? ? @Override
? ? public void draw(@NonNull Canvas canvas) {
? ? ? ? int width = getBounds().right;
? ? ? ? int height = getBounds().bottom;
? ? ? ? canvas.drawColor(Color.parseColor("#F3F5F9"));
? ? ? ? paint.setColor(Color.parseColor("#AEAEAE"));
? ? ? ? paint.setAntiAlias(true);
? ? ? ? paint.setTextSize(30);
? ? ? ? canvas.save();
? ? ? ? canvas.rotate(-30);
? ? ? ? float textWidth = paint.measureText(logo);
? ? ? ? int index = 0;
? ? ? ? for (int positionY = height / 10; positionY <= height; positionY += height / 10) {
? ? ? ? ? ? float fromX = -width + (index++ % 2) * textWidth;
? ? ? ? ? ? for (float positionX = fromX; positionX < width; positionX += textWidth * 2) {
? ? ? ? ? ? ? ? canvas.drawText(logo, positionX, positionY, paint);
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? 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 class MainActivity extends AppCompatActivity {
? ? @Override
? ? protected void onCreate(Bundle savedInstanceState) {
? ? ? ? super.onCreate(savedInstanceState);
? ? ? ? setContentView(R.layout.activity_main);
? ? ? ? findViewById(R.id.text).setBackground(new WaterMarkBg("Owen Chan"));
? ? }
}
原文鏈接:https://blog.csdn.net/owenchan1987/article/details/74422747
相關推薦
- 2022-05-18 解決iOS驗證碼顯示在左邊問題_IOS
- 2022-10-14 Linux 安裝指定版本GCC方法
- 2022-08-16 Hive常用日期格式轉換語法_數據庫其它
- 2023-02-09 C++存儲持續性生命周期原理解析_C 語言
- 2022-05-09 Python數據結構與算法中的棧詳解(2)_python
- 2022-04-21 Android自定義控件實現簡單滑動開關效果_Android
- 2022-12-24 C++中析構函數為何是虛函數_C 語言
- 2023-12-08 float 引起的塌陷
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支