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"
/>