[英]How to dim the area around a view in android?
I am trying to make an activity that needs to show square area as focused. 我正在尝试制作一个需要将方形区域显示为焦点的活动。 So far I have done this.
到目前为止,我已经这样做了。
Now what I want is to dim the area outside the square. 现在我想要的是将广场外的区域调暗。 Using framelayout dims the whole view.
使用framelayout使整个视图变暗。 I only want to dim the region outside the square.
我只想把广场外的区域调暗。
You can paint a black semitranslucent rectangle and then draw an inner rectangle with paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_OUT));
您可以绘制一个黑色的半透明矩形,然后使用
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_OUT));
绘制一个内部矩形paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_OUT));
As I know nothing about your layout, I'm giving a full example with a CustomOverlay
class: 由于我对你的布局一无所知,我给出了一个
CustomOverlay
类的完整示例:
my_activity.xml 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>
(I'm using a map as base view instead of a camera) (我使用地图作为基本视图而不是相机)
CustomOverlay.java 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;
}
}
The result looks like this: 结果如下:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.