这篇文章主要介绍“Android怎么设置重复文字水印背景”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“Android怎么设置重复文字水印背景”文章能帮助大家解决问题。
效果如图:
自定义Drawable :
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")); }}
关于“Android怎么设置重复文字水印背景”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识,可以关注编程网行业资讯频道,小编每天都会为大家更新不同的知识点。