Android水印字体,Android实现界面布局文字水印效果

0818b9ca8b590ca3270a3433284dd417.png

0818b9ca8b590ca3270a3433284dd417.png

asd

package watermark;

import android.content.Context;

import android.content.res.TypedArray;

import android.graphics.Canvas;

import android.util.AttributeSet;

import android.view.Gravity;

import android.widget.TextView;

import com.song.androidwatermark.R;

/**

* 水印文字

* Created by Song on 2017/6/29.

*/

public class WaterMarkText extends TextView {

private int mDegree; // 旋转角度

public WaterMarkText(Context context) {

this(context, null);

}

public WaterMarkText(Context context, AttributeSet attrs) {

this(context, attrs, 0);

}

public WaterMarkText(Context context, AttributeSet attrs, int defStyleAttr) {

super(context, attrs, defStyleAttr);

this.setGravity(Gravity.CENTER);

// 获取自定义属性

TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.WaterMarkText,defStyleAttr,0);

for (int i = 0; i < ta.getIndexCount(); i++) {

int index = ta.getIndex(i);

switch (index) {

case R.styleable.WaterMarkText_degree:

mDegree = ta.getInt(index,0);

break;

}

}

ta.recycle();

}

@Override

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

super.onMeasure(widthMeasureSpec, heightMeasureSpec);

setMeasuredDimension(getMeasuredWidth(), getMeasuredHeight()+80);

}

@Override

protected void onDraw(Canvas canvas) {

canvas.save();

// 位移,保持文字居中

canvas.translate(getCompoundPaddingLeft(), getExtendedPaddingTop());

// 以文字中心轴旋转

canvas.rotate(mDegree, this.getWidth() / 2f, this.getHeight() / 2f);

super.onDraw(canvas);

canvas.restore();

}

/**

* 设置旋转角度

* @param degree

*/

public void setDegree(int degree) {

this.mDegree = degree;

}

}

阿萨大神多阿萨德

package com.song.androidwatermark;

import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;

public class MainActivity extends AppCompatActivity {

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

}

@Override

protected void onStart() {

addWaterMarkView();

super.onStart();

}

/**

* 添加水印

*/

private void addWaterMarkView() {

View waterMarkView = LayoutInflater.from(this)

.inflate(R.layout.layout_watermark,null);

getRootView().addView(waterMarkView,0);

}

/**

* 获取根布局DecorView

* @return

*/

private ViewGroup getRootView() {

ViewGroup rootView = (ViewGroup)findViewById(android.R.id.content);

return rootView;

}

}asdadasd

xmlns:app="http://schemas.android.com/apk/res-auto"

xmlns:tools="http://schemas.android.com/tools"

android:id="@+id/activity_main"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:background="#00000000"

tools:context="com.song.androidwatermark.MainActivity">

android:orientation="vertical"

android:layout_width="match_parent"

android:layout_height="match_parent">

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Songlcy"

android:textColor="@android:color/darker_gray"

android:textSize="25sp"

app:degree="350"

/>

android:layout_marginTop="50dp"

android:layout_marginLeft="150dp"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Songlcy"

android:textColor="@android:color/darker_gray"

android:textSize="25sp"

app:degree="350"

/>

android:layout_marginTop="120dp"

android:layout_marginLeft="100dp"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Songlcy"

android:textColor="@android:color/darker_gray"

android:textSize="25sp"

app:degree="350"

/>

android:layout_marginTop="30dp"

android:layout_marginLeft="250dp"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Songlcy"

android:textColor="@android:color/darker_gray"

android:textSize="25sp"

app:degree="350"

/>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值