Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/181.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在android中如何使视图周围的区域变暗?_Android_Layout_View_Camera_Overlay - Fatal编程技术网

在android中如何使视图周围的区域变暗?

在android中如何使视图周围的区域变暗?,android,layout,view,camera,overlay,Android,Layout,View,Camera,Overlay,我正在尝试做一个活动,需要显示广场面积为重点。 到目前为止,我已经做到了这一点 现在我想把广场外的区域调暗。 使用framelayout可使整个视图变暗。我只想把广场外的区域调暗 您可以绘制一个黑色半透明矩形,然后使用paint.setXfermode(新的PorterDuffXfermode(PorterDuff.Mode.SRC_OUT))绘制一个内部矩形 由于我对您的布局一无所知,我将给出一个CustomOverlay类的完整示例: my_activity.xml <Relativ

我正在尝试做一个活动,需要显示广场面积为重点。 到目前为止,我已经做到了这一点

现在我想把广场外的区域调暗。
使用framelayout可使整个视图变暗。我只想把广场外的区域调暗

您可以绘制一个黑色半透明矩形,然后使用
paint.setXfermode(新的PorterDuffXfermode(PorterDuff.Mode.SRC_OUT))绘制一个内部矩形

由于我对您的布局一无所知,我将给出一个
CustomOverlay
类的完整示例:

my_activity.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <fragment
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/map"
        android:name="com.google.android.gms.maps.SupportMapFragment" />

    <mypackage.CustomOverlay
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_centerInParent="true" />

</RelativeLayout>
结果如下所示:


您可以将背景色设置为半透明黑色,以形成阴影。可能会发布整个布局xml,以便清楚地将其放置在何处@pryankvexHow你会怎么做,这样矩形视图就可以调整大小,而暗视图就可以紧跟其后,这样它就完全流畅了?
public class CustomOverlay extends LinearLayout {
    private Bitmap windowFrame;

    public CustomOverlay(Context context) {
        super(context);
    }

    public CustomOverlay(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public CustomOverlay(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public CustomOverlay(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    @Override
    protected void dispatchDraw(Canvas canvas) {
        super.dispatchDraw(canvas);

        if (windowFrame == null) {
            createWindowFrame();
        }
        canvas.drawBitmap(windowFrame, 0, 0, null);
    }

    @Override
    public boolean isEnabled() {
        return false;
    }

    @Override
    public boolean isClickable() {
        return false;
    }

    protected void createWindowFrame() {
        windowFrame = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888);
        Canvas osCanvas = new Canvas(windowFrame);

        RectF outerRectangle = new RectF(0, 0, getWidth(), getHeight());

        Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setColor(Color.argb(150, 0, 0, 0));
        osCanvas.drawRect(outerRectangle, paint);

        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_OUT));
        RectF innerRectangle = new RectF(100, 200, getWidth() - 100, getHeight() - 200);
        osCanvas.drawRect(innerRectangle, paint);

        paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setStrokeWidth(5);
        paint.setColor(Color.WHITE);
        paint.setStyle(Paint.Style.STROKE);
        osCanvas.drawRect(innerRectangle, paint);
    }

    @Override
    public boolean isInEditMode() {
        return true;
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
        windowFrame = null;
    }
}