android:用framelayout设置遮罩层

一,代码:

xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:id="@+id/main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".activity.FrameActivity">

    <!-- frame区域 -->
    <FrameLayout
        android:id="@+id/imageContainer"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        >
        <!-- 最下层内容 -->
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            >
            <!-- 标题 -->
            <TextView
                android:id="@+id/title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:text="欢迎使用小工具"
                android:textSize="18sp"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                android:layout_marginTop="100dp"
                android:padding="16dp"/>

            <ImageView
                android:id="@+id/image"
                android:layout_width="match_parent"
                android:layout_height="200dp"
                android:src="@drawable/b" />

            <Button
                android:id="@+id/buttonImage"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginTop="40dp"
                android:onClick="onButtonImageClick"
                android:text="图片信息"/>
        </LinearLayout>

        <!-- mask层 -->
        <View
            android:id="@+id/mask"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:clickable="true"
            android:focusable="true"
            android:onClick="onMaskClick"
            android:alpha="0.7"
            android:background="#000000"
            />

        <!-- 最外层的button -->
        <Button
            android:id="@+id/switchMask"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:onClick="onSwitchMaskClick"
            android:text="显示关闭遮罩层"/>

    </FrameLayout>


</androidx.constraintlayout.widget.ConstraintLayout>

java

package com.example.okdemo1.activity;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.os.Bundle;
import android.view.View;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import android.widget.Toast;

import com.example.okdemo1.R;

public class FrameActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_frame);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });
    }

    //显示view
    public void displayView(View view) {
        // 淡入效果,首先设置透明度为0
        view.setAlpha(0f);
        view.setVisibility(View.VISIBLE);
        view.animate().alpha(0.7f).setDuration(500).setListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                // 动画结束后的操作
            }
        });
    }

    //隐藏view
    public void hideView(View view) {
        // 淡入效果
        view.animate().alpha(0f).setDuration(500).setListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                view.setVisibility(View.INVISIBLE); // 动画结束时设置不可见
            }
        });
    }

    //切换遮罩层按钮被点击
    public void onSwitchMaskClick(View v) {
        View mask=findViewById(R.id.mask);
        if (mask.getVisibility() == View.INVISIBLE) {
            displayView(mask);
        } else {
            hideView(mask);
        }
    }

    //遮罩层被点击
    public void onMaskClick(View v) {
        View mask=findViewById(R.id.mask);
        hideView(mask);
    }

    //图片信息按钮被点击
    public void onButtonImageClick(View v) {

        Toast.makeText(FrameActivity.this, "图片内容是外国人", Toast.LENGTH_LONG).show();
    }
}

二,测试效果:

posted @ 2025-05-10 09:01  刘宏缔的架构森林  阅读(37)  评论(0)    收藏  举报