使用dialog来实现蒙层露出某个控件的效果

2020-04-03  本文已影响0人  歳月的童話
<?xml version="1.0" encoding="utf-8"?>
<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"
    android:focusable="false"
    tools:context=".MainActivity">

    <ImageView
        android:id="@+id/Image"
        android:layout_width="99dp"
        android:layout_centerVertical="true"
        android:layout_height="99dp"
        android:layout_centerInParent="true"
        android:background="#ff00"
        android:focusable="true" />

</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/shade_linear"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#88000000">

    <RelativeLayout
        android:id="@+id/content"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#ffffff">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="30dp"
            android:gravity="center"
            android:text="这是蒙层"
            android:textColor="#ff000000" />
    </RelativeLayout>
</FrameLayout>

import android.graphics.Rect;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final ImageView imageView = findViewById(R.id.Image);
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Rect rect = new Rect();
                imageView.getGlobalVisibleRect(rect);
                rect.top = rect.top - getStatusBarHeight();
                rect.bottom = rect.bottom - getStatusBarHeight();
                new ShadeDialog(MainActivity.this, rect).show();
            }
        });
    }
    //获取状态栏高度
    protected int getStatusBarHeight() {
        Class<?> c = null;
        Object obj = null;
        Field field = null;
        int x = 0, sbar = 38;//默认为38,貌似大部分是这样的
        try {
            c = Class.forName("com.android.internal.R$dimen");
            obj = c.newInstance();
            field = c.getField("status_bar_height");
            x = Integer.parseInt(field.get(obj).toString());
            sbar = getResources().getDimensionPixelSize(x);
        } catch (Exception e1) {
            e1.printStackTrace();
        }
        return sbar;
    }
}
import android.app.Dialog;
import android.content.Context;
import android.graphics.Rect;
import android.os.Bundle;
import android.view.ViewTreeObserver;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;

public class ShadeDialog extends Dialog {

    private Context context;

    private Rect viewRect;

    public ShadeDialog(Context context, Rect viewRect) {
        super(context, R.style.shapeDialog);
        this.context = context;
        this.viewRect = viewRect;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.shade_dialog);

        //使Dialog显示整个屏幕大小
        Window window = getWindow();
        WindowManager.LayoutParams layoutParams = window.getAttributes();
        layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
        layoutParams.height = WindowManager.LayoutParams.MATCH_PARENT;
        window.setAttributes(layoutParams);

        FrameLayout shade_linear = findViewById(R.id.shade_linear);
        ShadeView shadeView = new ShadeView(context, viewRect);
        shade_linear.addView(shadeView);

        final RelativeLayout content = findViewById(R.id.content);
        //布局的状态发生变化或者可见性发生变化才会调用
        content.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(content.getWidth(), content.getHeight());
                params.setMargins((int) viewRect.right + 20, (int) viewRect.top, (int) viewRect.right + 20 + content.getWidth(), (int) viewRect.top + content.getHeight());
                content.setLayoutParams(params);
            }
        });

    }
}

*自定义view代码

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

public class ShadeView extends View {
    private Rect rect;
    private Paint paint;
    private PorterDuffXfermode porterDuffXfermode;

    public ShadeView(Context context, Rect rect) {
        super(context);
        this.rect = new Rect();
        this.rect = rect;
        paint = new Paint();
    }

    public ShadeView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public ShadeView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //透明效果
        porterDuffXfermode = new PorterDuffXfermode(PorterDuff.Mode.CLEAR);  //SRC_OUT或者CLEAR都可以
        paint.setXfermode(porterDuffXfermode);
        paint.setAntiAlias(true);
        canvas.drawRect(rect, paint);
    }
}

上一篇 下一篇

猜你喜欢

热点阅读