如果在上述对话框中用作背景,则此视图可用于在屏幕上绘制透明矩形。
public class WindowView extends View {
private static final int LOW_DPI_STATUS_BAR_HEIGHT = 19;
private static final int MEDIUM_DPI_STATUS_BAR_HEIGHT = 25;
private static final int HIGH_DPI_STATUS_BAR_HEIGHT = 38;
Context mContext;
int mHeight, mWidth, mTop, mRight, mLeft, mBottom;
Rect mLeftRect, mRightRect, mTopRect, mBottomRect;
Rect mRect;
Paint mPaint;
View mAnchorView;
Paint mTempPaint1;
public WindowView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public WindowView(Context context) {
this(context, null);
}
public WindowView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
mContext = context;
initVariables();
}
boolean mReset = false;
GestureDetector mGestureDetector;
GestureDetector.SimpleOnGestureListener mSimpleOnGestureListener;
private View getView() {
return this;
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (mSimpleOnGestureListener == null) {
mSimpleOnGestureListener = new SimpleOnGestureListener() {
@Override
public boolean onSingleTapConfirmed(MotionEvent e) {
if (mListener != null && e.getX() >= mRect.left
&& e.getX() <= mRect.right && e.getY() >= mRect.top
&& e.getY() <= mRect.bottom)
mListener.onClick(getView());
return true;
}
};
}
if (mGestureDetector == null) {
mGestureDetector = new GestureDetector(mContext,
mSimpleOnGestureListener);
}
mGestureDetector.onTouchEvent(event);
return true;
}
private void initVariables() {
mPaint = new Paint();
mPaint.setStyle(Style.FILL);
mPaint.setColor(mContext.getResources().getColor(
R.color.tut_transcluscent_bg));
mRect = new Rect();
mTempPaint1 = new Paint(Paint.ANTI_ALIAS_FLAG);
mTempPaint1.setAlpha(64);
mTempPaint1.setColorFilter(new PorterDuffColorFilter(Color.WHITE,
Mode.SRC_IN));
}
OnClickListener mListener;
public void setTransparentBackGroundColor(int color) {
mPaint.setColor(color);
invalidate();
}
@Override
public void setOnClickListener(OnClickListener l) {
mListener = l;
}
public View getAnchorView() {
return mAnchorView;
}
float[] mPaddings;
Rect mAnchorRect;;
public void setAnchorView(View mView, Rect rect, float[] paddings) {
mAnchorRect = rect;
if (mAnchorRect == null)
mAnchorView = mView;
else
mAnchorView = null;
mPaddings = paddings;
requestLayout();
invalidate();
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
if (mAnchorView != null || mAnchorRect != null)
initView();
mReset = true;
}
boolean mFlagInvalid = false;
Handler mAnrHandler;
public void setHandler(Handler handler) {
mAnrHandler = handler;
}
private int getStatusBarHeight() {
DisplayMetrics displayMetrics = new DisplayMetrics();
((WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE))
.getDefaultDisplay().getMetrics(displayMetrics);
int statusBarHeight;
switch (displayMetrics.densityDpi) {
case DisplayMetrics.DENSITY_HIGH:
statusBarHeight = HIGH_DPI_STATUS_BAR_HEIGHT;
break;
case DisplayMetrics.DENSITY_MEDIUM:
statusBarHeight = MEDIUM_DPI_STATUS_BAR_HEIGHT;
break;
case DisplayMetrics.DENSITY_LOW:
statusBarHeight = LOW_DPI_STATUS_BAR_HEIGHT;
break;
default:
statusBarHeight = MEDIUM_DPI_STATUS_BAR_HEIGHT;
}
return statusBarHeight;
}
public void initView() {
mFlagInvalid = false;
int top = 0, left = 0;
if (mAnchorView != null) {
top = mAnchorView.getTop();
left = mAnchorView.getLeft();
View temp = mAnchorView;
int cnt = 0;
try {
while (((View) temp.getParent()).getId() != android.R.id.content) {
temp = (View) temp.getParent();
int scrolledHeight = 0;
if (temp instanceof ScrollView) {
scrolledHeight = ((ScrollView) temp).getScrollY();
}
top = top + temp.getTop() - scrolledHeight;
left = left + temp.getLeft();
cnt++;
if (cnt > 100) {
if (mAnrHandler != null)
mAnrHandler.sendEmptyMessage(9);
mFlagInvalid = true;
break;
}
}
} catch (Exception e) {
mFlagInvalid = true;
if (mAnrHandler != null)
mAnrHandler.sendEmptyMessage(8);
e.printStackTrace();
}
TypedValue tv = new TypedValue();
if (getContext()
.getTheme()
.resolveAttribute(
VERSION.SDK_INT >= VERSION_CODES.HONEYCOMB ? android.R.attr.actionBarSize
: R.attr.actionBarSize, tv, true)) {
int actionBarHeight = TypedValue.complexToDimensionPixelSize(
tv.data, getResources().getDisplayMetrics());
top = top + actionBarHeight;
}
} else if (mAnchorRect != null) {
}
if (mFlagInvalid)
init(20, 20, 50, 50);
else {
if (mAnchorRect != null) {
init(mAnchorRect.bottom, mAnchorRect.right, mAnchorRect.left,
mAnchorRect.top);
} else
init(mAnchorView.getHeight(), mAnchorView.getWidth(), left, top);
}
}
public void init(int height, int width, int left, int top) {
mWidth = width;
mHeight = height;
mTop = top;
mBottom = top + height;
mLeft = left;
mRight = left + width;
mLeft = (int) (mLeft + mPaddings[0] * mWidth);
mTop = (int) (mTop + mPaddings[1] * mHeight);
mRight = (int) (mRight - mPaddings[2] * mWidth);
mBottom = (int) (mBottom - mPaddings[3] * mHeight);
mRect = new Rect(mLeft, mTop, mRight, mBottom);
invalidate();
}
public Rect getAnchorRect() {
return mRect;
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
initOnceOnDraw();
canvas.drawRect(mLeftRect, mPaint);
canvas.drawRect(mTopRect, mPaint);
canvas.drawRect(mRightRect, mPaint);
canvas.drawRect(mBottomRect, mPaint);
canvas.drawRect(mRect, mTempPaint1);
}
private void initOnceOnDraw() {
if (mReset) {
mReset = false;
mTopRect = new Rect(0, 0, getWidth(), mRect.top);
mLeftRect = new Rect(0, mRect.top, mRect.left, mRect.bottom);
mRightRect = new Rect(mRect.right, mRect.top, getWidth(),
mRect.bottom);
mBottomRect = new Rect(0, mRect.bottom, getWidth(), getHeight());
}
}}
setAnchorView() 方法给出了必须在其上绘制叠加层的视图。
mWindowView = (WindowView) mTutorialView
.findViewById(R.id.windowview_tutorial);
mWindowView.setTransparentBackGroundColor(backgroundColor);
mWindowView.setAnchorView(view, null, padding);
希望这可以帮助