'Android Canvas shifts drawings after drawBitmap

I try to draw some rectangles on a bitmap image with canvas and surfaceview. When bitmap image is drawn, the rectangle is shifted. If I draw the rectangle to the center point of the surfaceview-coordinates are x = 492 and y = 301-rectangle is shifted to x = 588 and y = 388. But if I don't draw background, it is drawn to correct point.

Codes are not exactly belongs to the project but it reflect main progress.

public class Submit {

    public Submit(Context context) {
        this.surfaceHolder = surfaceHolder;
        this.context = context;
    }

    public void draw(){

        canvas = surfaceHolder.lockCanvas()
        surfaceHolder.setFixedSize(bgImage.getWidth(),bgImage.getHeight());
        canvas.drawBitmap(bgImage,0,0,null)
        canvas.drawRect(492,301,492+100,301+100,paint);
        surfaceHolder.unlockCanvasAndPost(canvas)
        


}
}

Here the xml file

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <SurfaceView
        android:id="@+id/cylindersImage"
        android:layout_width="358dp"
        android:layout_height="219dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.49"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.05">

    </SurfaceView>


</androidx.constraintlayout.widget.ConstraintLayout>



Solution 1:[1]

The error is trying to open a file with the name of test_XXX.txt. Your code is creating a file with the name of test_xxxx.txt.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 123 456 789 0