Android Question Strange behavior of Canvas

Status
Not open for further replies.
I need to make a background for the views so that only part of the image is visible.
For example, the top half.

Standard code. imvForeground is already built into the Activity.
B4X:
Dim cvs As Canvas
Dim img As Bitmap
Dim Rect1 As Rect
Dim Rect2 As Rect

cvs.Initialize(imvForeground)
img.Initialize(File.DirAssets, "image.png")
Rect1.Initialize(0, 0, img.Width, img.Height/2)
Rect2.Initialize(0, 0, img.Width, img.Height/2)
cvs.DrawBitmap(img, Rect2, Rect1)
imvForeground.Invalidate2(Rect1)

I'm testing on multiple devices.

On the smartphone "Xiaomi Redmi Note 11 Pro", Anroid 13,
On a smartphone "ZTE Blade A0622", Android 7,
on the tablet "BQ-1045G", Anroid 8.

On both smartphones, the code works fine.
The tablet displays the top left quarter of the image across the entire imvForeground.

How can this be?
 
This is the original image.

Image1.jpg


This is how the code should work. And this is how it works on smartphones.

Image2.jpg


And this is how it works on the tablet.

Image3.png
 
Upvote 0
I posted the simplest code that works correctly on smartphones and incorrectly on a tablet. The entire project in compiled form - 8 Mb.
In Designer - section "Script-variant" I don't have any code.

The question is: "Why does the same fairly simple code work on one type of device and stop working on another type of device?".
This is something strange.
 
Upvote 0

Andrew (Digitwell)

Well-Known Member
Licensed User
Longtime User
it is important to understand how the imvForeground is anchored in thelayout which is why there was a request to provide a sample project. Incorrect anchoring along with different screen resoutions, may produce the effects that you see here.
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
You define Rect1 and Rect2 with the same dimensions !
The dimensions of the bitmap are the same in all cases, but the dimensions of imvForeground are probably different.
As already said without knowing what exactly you have done and how, it is impossible to give any concrete advice.
 
Upvote 0
I created a new project with nothing but the following code:
B4X:
#Region  Project Attributes
    #ApplicationLabel: B4A Example
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

Sub Process_Globals

End Sub

Sub Globals
    Private imvForeground As ImageView
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Dim cvs As Canvas
    Dim img As Bitmap
    Dim Rect1 As Rect
    Dim Rect2 As Rect

    Activity.LoadLayout("Main")

    cvs.Initialize(imvForeground)
    img.Initialize(File.DirAssets, "image.png")
    Rect1.Initialize(0, 0, img.Width, img.Height/2)
    Rect2.Initialize(0, 0, img.Width, img.Height/2)
    cvs.DrawBitmap(img, Rect2, Rect1)
    imvForeground.Invalidate2(Rect1)
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

This is what Designer looks like for the Phone and Tablet variants:

designer1.png


and

designer2.png


There is nothing else in the project. The result is the same.
 
Upvote 0
Status
Not open for further replies.
Top