高級UI<第五十一篇>:Android水印位置探討

水印,是一種防止非法盜圖、非法截取重要信息的安全措施之一,在Android中的水印一般運用于:
(1)圖片上傳前必須加水印;
(2)重要的涉密界面需要加水??;
(3)office文件需要加水??;
水印必須要放在所有可是內(nèi)容之上,所以水印應(yīng)該為半透明。

[一] 圖片加水印

圖片上加入水印比較簡單,在網(wǎng)上代碼也比較容易找到

首先,將圖片生成bitmap

Bitmap bitmap = Bitmap.createBitmap(240, 240, Bitmap.Config.ARGB_8888);

將bitmap傳入Canvas

Canvas canvas = new Canvas(bitmap);

最后利用canvase對象繪制水印

canvas.drawXXX

[二] 在Android某界面上加水印

<androidx.constraintlayout.widget.ConstraintLayout
    android:id="@+id/rootview"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="#3F51B5"
        app:layout_constraintTop_toTopOf="parent"
        android:text="放一個按鈕測試"
        android:textSize="20sp"
        android:textColor="#ffffff"
        app:layout_constraintBottom_toBottomOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>

就以上面的xml布局為例。

[措施一] 直接在根布局上添加背景

    rootview.setBackground(XXX);
圖片.png

如圖所示,水印被按鈕擋住,所以,這不是一個合格的水印,那么,只能想辦法將水印放在按鈕上層。

[措施二] 直接在根布局上添加前景

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        rootview.setForeground(new WaterMarkBg(MainActivity.this,listString, 30, 20));
    }
圖片.png

這樣的效果比較顯著,完全符合水印在頂端的特性,點擊按鈕也能觸發(fā)點擊事件,對組件的點擊事件沒有影響。但是,view添加前景在Android 6.0才推出,這樣Android 6.0以下的設(shè)備就無法實現(xiàn)水印效果。

[措施三] bringToFront將view直接移到頂端(可行)

先看一下這個布局

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <LinearLayout
        android:id="@+id/rl_match"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:background="@color/colorAccent"/>


    <View
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_centerInParent="true"
        android:background="#3F51B5" />


</RelativeLayout>

View做為按鈕,LinearLayout設(shè)置水印背景,設(shè)置背景的代碼如下:

    rl_match.setBackground(waterMarkBg);

效果如下:

圖片.png

如果加入這句話:

    rl_match.bringToFront();

效果如下:

圖片.png

點擊view,可以觸發(fā)點擊事件。

[措施四] elevation屬性的使用(可行)

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <LinearLayout
        android:elevation="1dp"
        android:id="@+id/rl_match"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:background="@color/colorAccent"/>


    <View
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_centerInParent="true"
        android:background="#3F51B5" />

</RelativeLayout>

在LinearLayout中添加屬性

        android:elevation="1dp"

將LinearLayout的Z軸升高,這樣LinearLayout布局將在View上層,且不影響View組件的點擊事件。

效果和[措施三]的代碼一致。

[需要注意的是]

在高SDK版本中,Button為了添加陰影效果,elevation屬性有一個默認值,且初始值和按下按鈕的值是不一樣的,elevation屬性增加按鈕的Z軸高度,高度增加了就會有陰影(影子),所以elevation屬性其實是增加陰影效果來用的,當(dāng)點擊按鈕時,按鈕的高度增加,陰影效果增強。所以,使用[措施三]或者[措施四]時,要特別注意,如果遇到Button,要想實現(xiàn)水印就需要結(jié)合[措施三][措施四]實現(xiàn)。

[本章完...]

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容