I have a custom view on top of another view in which I want to draw stuff. The canvas has width 1080
and height 1920
(fixed portrait orientation). I have confirmed that this view is indeed sitting on top of another view (camera preview). However, the onDraw
function isn't drawing anything visible. When I try canvas.drawRect(0, 0, 10, 10)
I see a blue rectangle, but when I change the position of the top left corner to something like (100, 100)
there's nothing on the screen. (100, 100)
is certainly within the dimensions of my screen size but why doesn't the rectangle show up?
public class OverlayView extends View {
private Paint paint;
public BarcodeOverlayView(Context context, AttributeSet attributeSet) {
super(context, attributeSet);
paint = new Paint();
paint.setColor(Color.BLUE);
paint.setAntiAlias(true);
paint.setStrokeWidth(5);
paint.setStyle(Paint.Style.STROKE);
}
@Override
protected void onDraw(Canvas canvas) {
canvas.drawRect(100, 100, 50, 50, paint);
}
}
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getWindow().getDecorView().setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION // hide nav bar
| View.SYSTEM_UI_FLAG_FULLSCREEN // hide status bar
| View.SYSTEM_UI_FLAG_IMMERSIVE);
}