Android界面布局文字水印

一、需求分析
需求:在每个布局中加上水印效果,如下图:
在这里插入图片描述
从图中,我们可以看到在页面中很明显有文字水印效果,那么如何实现这种效果呢:
(1)水印文字有旋转角度
(2)水印效果处于内容布局之上,且内容布局具有焦点效果
(3)水印文字位置不固定可根据需求自动改变
分析以上三步,可以利用Android自定义View的方式来实现。
二 、效果实现

1.自定义水印文字View

从需求图中可以看到,水印文字有旋转角度,颜色,字体大小等属性自定义实现。颜色,字体的属性系统TextView已帮我们实现,所以我们可以通过继承TextView来实现旋转的角度功能。
1.1.res/values/attr.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="RotateTextView">
        <attr name="degree" format="dimension" />
    </declare-styleable>
</resources>

1.2.自定义RotateTextView

@SuppressLint("AppCompatCustomView")
public class RotateTextView extends TextView {
    private static final int DEFAULT_DEGREES = 0;
    private int mDegrees;

    public RotateTextView(Context context) {
        super(context, null);
    }

    public RotateTextView(Context context, AttributeSet attrs) {
        super(context, attrs, android.R.attr.textViewStyle);
        this.setGravity(Gravity.CENTER);
        TypedArray a = context.obtainStyledAttributes(attrs,
                R.styleable.RotateTextView);
        mDegrees = a.getDimensionPixelSize(R.styleable.RotateTextView_degree,
                DEFAULT_DEGREES);
        a.recycle();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        setMeasuredDimension(getMeasuredWidth(), getMeasuredWidth());
    }

    @Override
    protected void onDraw(Canvas canvas) {
        canvas.save();
        canvas.translate(getCompoundPaddingLeft(), getExtendedPaddingTop());
        canvas.rotate(mDegrees, this.getWidth() / 2f, this.getHeight() / 2f);
        super.onDraw(canvas);
        canvas.restore();
    }

    public void setDegrees(int degrees) {
        mDegrees = degrees;
    }

}

2.自定义水印布局

水印布局中,水印文字随机展示在某个位置,我们可以获取【x,y】的坐标范围,然后在范围中产生随机数,展示水印文字即可。此处为了方便,我直接定义位置
2.1.res/layout/fragment_layout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_marginTop="55dp"
        android:layout_weight="1"
        android:orientation="horizontal">

        <RelativeLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1">

            <com.admin.waterdemo.RotateTextView
                android:id="@+id/fragment_tag11"
                style="@style/waterPrint"
                app:degree="350dp" />
        </RelativeLayout>

        <RelativeLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1">

            <com.admin.waterdemo.RotateTextView
                android:id="@+id/fragment_tag12"
                style="@style/waterPrint"
                app:degree="350dp" />

        </RelativeLayout>

        <RelativeLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1">

            <com.admin.waterdemo.RotateTextView
                android:id="@+id/fragment_tag13"
                style="@style/waterPrint"
                app:degree="350dp" />

        </RelativeLayout>

        <RelativeLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1">

            <com.admin.waterdemo.RotateTextView
                android:id="@+id/fragment_tag14"
                style="@style/waterPrint"
                app:degree="350dp" />
        </RelativeLayout>
    </LinearLayout>


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_marginTop="15dp"
        android:layout_weight="1"
        android:orientation="horizontal">

        <RelativeLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1">

            <com.admin.waterdemo.RotateTextView
                android:id="@+id/fragment_tag21"
                style="@style/waterPrint"
                app:degree="350dp" />
        </RelativeLayout>

        <RelativeLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1">

            <com.admin.waterdemo.RotateTextView
                android:id="@+id/fragment_tag22"
                style="@style/waterPrint"
                app:degree="350dp" />

        </RelativeLayout>

        <RelativeLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1">

            <com.admin.waterdemo.RotateTextView
                android:id="@+id/fragment_tag23"
                style="@style/waterPrint"
                app:degree="350dp" />

        </RelativeLayout>

        <RelativeLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1">

            <com.admin.waterdemo.RotateTextView
                android:id="@+id/fragment_tag24"
                style="@style/waterPrint"
                app:degree="350dp" />
        </RelativeLayout>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_marginTop="15dp"
        android:layout_weight="1"
        android:orientation="horizontal">

        <RelativeLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1">

            <com.admin.waterdemo.RotateTextView
                android:id="@+id/fragment_tag31"
                style="@style/waterPrint"
                app:degree="350dp" />
        </RelativeLayout>

        <RelativeLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1">

            <com.admin.waterdemo.RotateTextView
                android:id="@+id/fragment_tag32"
                style="@style/waterPrint"
                app:degree="350dp" />

        </RelativeLayout>

        <RelativeLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1">

            <com.admin.waterdemo.RotateTextView
                android:id="@+id/fragment_tag33"
                style="@style/waterPrint"
                app:degree="350dp" />

        </RelativeLayout>

        <RelativeLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1">

            <com.admin.waterdemo.RotateTextView
                android:id="@+id/fragment_tag34"
                style="@style/waterPrint"
                app:degree="350dp" />
        </RelativeLayout>
    </LinearLayout>

</LinearLayout>

2.2.ContentFragment

public class ContentFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_layout, null);
        TextView tv11 =  view.findViewById(R.id.fragment_tag11);
        TextView tv12 =  view.findViewById(R.id.fragment_tag12);
        TextView tv13 =  view.findViewById(R.id.fragment_tag13);
        TextView tv14 =  view.findViewById(R.id.fragment_tag14);
        TextView tv21 =  view.findViewById(R.id.fragment_tag21);
        TextView tv22 =  view.findViewById(R.id.fragment_tag22);
        TextView tv23 =  view.findViewById(R.id.fragment_tag23);
        TextView tv24 =  view.findViewById(R.id.fragment_tag24);
        TextView tv31 =  view.findViewById(R.id.fragment_tag31);
        TextView tv32 =  view.findViewById(R.id.fragment_tag32);
        TextView tv33 =  view.findViewById(R.id.fragment_tag33);
        TextView tv34 =  view.findViewById(R.id.fragment_tag34);
        String tag = this.getArguments().getString("key");
        tv11.setText(tag);
        tv12.setText(tag);
        tv13.setText(tag);
        tv14.setText(tag);
        tv21.setText(tag);
        tv22.setText(tag);
        tv23.setText(tag);
        tv24.setText(tag);
        tv31.setText(tag);
        tv32.setText(tag);
        tv33.setText(tag);
        tv34.setText(tag);
        return view;
    }

}

2.1.MainActivity

public class MainActivity extends AppCompatActivity {
    private Button btnClick;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btnClick = findViewById(R.id.btn_main_click);
        btnClick.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(MainActivity.this, "水印", Toast.LENGTH_SHORT).show();
            }
        });
        String userName = "水印";
        Fragment fragment = new ContentFragment();
        Bundle args = new Bundle();
        args.putString("key", userName);
        fragment.setArguments(args);
        FragmentManager fragmentManager = getSupportFragmentManager();
        fragmentManager.beginTransaction().replace(R.id.container, fragment)
                .commit();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值