I recently played with something similar and adapted it for you. All magic happens in onDraw:
public class FocusView extends View { private Paint mTransparentPaint; private Paint mSemiBlackPaint; private Path mPath = new Path(); public FocusView(Context context) { super(context); initPaints(); } public FocusView(Context context, AttributeSet attrs) { super(context, attrs); initPaints(); } public FocusView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); initPaints(); } private void initPaints() { mTransparentPaint = new Paint(); mTransparentPaint.setColor(Color.TRANSPARENT); mTransparentPaint.setStrokeWidth(10); mSemiBlackPaint = new Paint(); mSemiBlackPaint.setColor(Color.TRANSPARENT); mSemiBlackPaint.setStrokeWidth(10); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); mPath.reset(); mPath.addCircle(canvas.getWidth() / 2, canvas.getHeight() / 2, 550, Path.Direction.CW); mPath.setFillType(Path.FillType.INVERSE_EVEN_ODD); canvas.drawCircle(canvas.getWidth() / 2, canvas.getHeight() / 2, 550, mTransparentPaint); canvas.drawPath(mPath, mSemiBlackPaint); canvas.clipPath(mPath); canvas.drawColor(Color.parseColor("#A6000000")); } }
The trick here is to create a Path (transparent circle) so that we can set the method of drawing the path "out of the path" instead of "inside the path". Finally, we can just put the canvas on this path and fill in the black color.
For you, you just need to change Color.BLACK to your color, as well as change the desired radius.
EDIT: Oh and just add it programmatically: FocusView view = new FocusView(context) your_layout.addView(view)
Or via XML:
<package_path_to_.FocusView android:layout_width="match_parent" android:layout_height="match_parent" />
EDIT2: I just saw that you wanted this to host your application. You might consider looking at https://github.com/iammert/MaterialIntroView , then
NSimon
source share