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

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

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

my_activity.xml

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical">

android:layout_width="match_parent"

android:layout_height="match_parent"

android:id="@+id/map"

android:name="com.google.android.gms.maps.SupportMapFragment" />

android:layout_width="match_parent"

android:layout_height="match_parent"

android:layout_centerInParent="true" />

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

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;

}

}

结果看起来像这样:

c41490771e9ae96dd9c1fa09cc70f552.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值