如何在Android中的视图周围变暗?

如何在Android中的视图周围变暗?,第1张

概述我正在尝试进行一项活动,以显示方形区域为重点.到目前为止,我已经做到了.现在,我要使广场外的区域变暗.使用框架布局会使整个视图变暗.我只想使广场外的区域变暗.解决方法:您可以绘制黑色的半透明矩形,然后使用paint.setXfermode(newPorterDuffXfermode(PorterDuff.Mode.SRC_OU

我正在尝试进行一项活动,以显示方形区域为重点.
到目前为止,我已经做到了.

现在,我要使广场外的区域变暗.
使用框架布局会使整个视图变暗.我只想使广场外的区域变暗.

解决方法:

您可以绘制黑色的半透明矩形,然后使用paint.setXfermode(new 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>

(我将地图用作基本视图,而不是相机)

CustomOverlay.java

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;    }}

结果看起来像这样:

总结

以上是内存溢出为你收集整理的如何在Android中的视图周围变暗?全部内容,希望文章能够帮你解决如何在Android中的视图周围变暗?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1090686.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-27
下一篇2022-05-27

发表评论

登录后才能评论

评论列表(0条)

    保存