2

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);
}
Carpetfizz
  • 8,707
  • 22
  • 85
  • 146
  • Are you using the FULL_SCREEN Layout attribute from WindowManager? – Opiatefuchs May 01 '17 at 17:42
  • Yes, I believe so, I've added my `MainActivity.java` to the original post – Carpetfizz May 01 '17 at 17:43
  • I don´t know if this is the problem. But I detected the problem with the FULL_SCREEN feature a few days ago.I am drawing some animated bitmaps on a SurfaceView and as soon as I put this feature to my activity, the animated bitmap is disapearing on certain areas and apearing again if they passed this area. I don´t know if it´s a bug, but just remove the features and try if the issue is related to it. – Opiatefuchs May 01 '17 at 17:54
  • I'm not dynamically changing the position or the paint object. The above code just doesn't work on app startup. – Carpetfizz May 01 '17 at 17:58
  • I know that, I just want to explain what the cause is. If you set it to 100,100 it is not visible, so (if it´s the same problem) it could be in that area where the view is not visible. – Opiatefuchs May 01 '17 at 18:00
  • @Opiatefuchs strange, I removed that snippet of code and it works fine – Carpetfizz May 01 '17 at 18:00
  • I really guess it is a bug in the system, I have no fix for it.... :( – Opiatefuchs May 01 '17 at 18:01

2 Answers2

0

Try adding this to your constructor:

setWillNotDraw(false);

This could be the same problem as in Custom onDraw() method not called

Community
  • 1
  • 1
Doron Yakovlev Golani
  • 5,188
  • 9
  • 36
  • 60
0

Call to Canvas.drawRect(100, 100, 50, 50, paint) will not draw anything because the passed arguments are wrong in a way that the points by which should be the desired rectangle drawn represent, lets call it a reversed rectangle.

If you look at description (JavaDoc) of Canvas.drawRect(float, float, float, float, Paint) method you will see that the first argument is left, second one is top, third one is right and the fourth one is bottom, that is where yours issue lies, the right (50) point is before left (100) point on X axis, and also the bottom (50) point is before top (100) point on Y axis.

So if I assume that by calling Canvas.drawRect(100, 100, 50, 50, paint) you would like to draw a rectangle that is 50 px wide and 50 px tall at X position of 100 px and Y position of 100 px you should be able to achieve that by calling Canvas.drawRect(100, 100, 150, 150, paint).