我正在尝试进行一项活动,以显示方形区域为重点.
到目前为止,我已经做到了.
现在,我要使广场外的区域变暗.
使用框架布局会使整个视图变暗.我只想使广场外的区域变暗.
解决方法:
您可以绘制黑色的半透明矩形,然后使用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;
}
}
结果看起来像这样:
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。