Android Question FILTER_BITMAP_FLAG not working on Android 11

roumei

Active Member
Licensed User
I'm trying to use the FILTER_BITMAP_FLAG while drawing bitmaps on a canvas. When the image is scaled, bilinear sampling should be used to blur the whole image. This doesn't work for me on two Android 11 devices. On Android 7, the image is drawn correctly with blurred pixels (see attached images).

The documentation states the following but I'm not sure what to do with this information:

"On devices running Build.VERSION_CODES#O and below, hardware accelerated drawing always uses bilinear sampling on scaled bitmaps, regardless of this flag. On devices running Build.VERSION_CODES#Q and above, this flag defaults to being set on a new Paint. It can be cleared with setFlags(int) or setFilterBitmap(boolean)."

This is the code (B4XPages example attached):
B4X:
Sub Class_Globals
    Private Root As B4XView
    Private xui As XUI
    Private Panel1 As Panel
    Private Canvas1 As Canvas
    Private refl As Reflector
End Sub

Public Sub Initialize
'    B4XPages.GetManager.LogEvents = True
End Sub

'This event will be called once, before the page becomes visible.
Private Sub B4XPage_Created (Root1 As B4XView)
    
    Root = Root1
    Root.LoadLayout("MainPage")
    
    Canvas1.Initialize(Panel1)
    SetAntiAlias(Canvas1, False)
    Dim bmp1 As Bitmap = LoadBitmap(File.DirAssets, "TestImage.png")
    Dim rect1 As Rect
    rect1.Initialize(0dip, 0dip, Panel1.Width, Panel1.Width)
    Canvas1.DrawBitmap(bmp1, Null, rect1)

End Sub

Sub SetAntiAlias (c3 As Canvas, bOn As Boolean)
    c3.AntiAlias = bOn
    Dim iSetBits As Int = 3 ' ANTI_ALIAS_FLAG | FILTER_BITMAP_FLAG
    Dim iClearBits As Int = 3
    If bOn = True Then
        iClearBits = 0
    Else
        iSetBits = 0
    End If
    Dim NativeCanvas As Object
    refl.Target = c3
    NativeCanvas = refl.GetField("canvas")
    Dim PaintFlagsDrawFilter As Object
    PaintFlagsDrawFilter = refl.CreateObject2("android.graphics.PaintFlagsDrawFilter", Array As Object(iClearBits, iSetBits), Array As String("java.lang.int", "java.lang.int"))
    refl.Target = NativeCanvas
    refl.RunMethod4("setDrawFilter", Array As Object(PaintFlagsDrawFilter), Array As String("android.graphics.DrawFilter"))
End Sub

Does anyone have an idea about how to fix this and get the bilinear sampling?
 

Attachments

  • Android7.png
    Android7.png
    65.6 KB · Views: 119
  • Android11.png
    Android11.png
    27.1 KB · Views: 126
  • TestAntiAliasing.zip
    14.6 KB · Views: 115
Solution
It seems that for Android 10+ you can't use bilinear sampling globally. You need to use DrawBitmap together with a Paint object. The ABExtDrawing library provides such a DrawBitmap function. This is the working example:
B4X:
Sub Class_Globals
    Private Root As B4XView
    Private xui As XUI
    Private Panel1 As Panel
    Private Canvas1 As Canvas
    Private ExtDraw As ABExtDrawing
End Sub

Public Sub Initialize
'    B4XPages.GetManager.LogEvents = True
End Sub

'This event will be called once, before the page becomes visible.
Private Sub B4XPage_Created (Root1 As B4XView)
    
    Root = Root1
    Root.LoadLayout("MainPage")
    
    Canvas1.Initialize(Panel1)

    Dim paint As ABPaint
    paint.Initialize()...

roumei

Active Member
Licensed User
It seems that for Android 10+ you can't use bilinear sampling globally. You need to use DrawBitmap together with a Paint object. The ABExtDrawing library provides such a DrawBitmap function. This is the working example:
B4X:
Sub Class_Globals
    Private Root As B4XView
    Private xui As XUI
    Private Panel1 As Panel
    Private Canvas1 As Canvas
    Private ExtDraw As ABExtDrawing
End Sub

Public Sub Initialize
'    B4XPages.GetManager.LogEvents = True
End Sub

'This event will be called once, before the page becomes visible.
Private Sub B4XPage_Created (Root1 As B4XView)
    
    Root = Root1
    Root.LoadLayout("MainPage")
    
    Canvas1.Initialize(Panel1)

    Dim paint As ABPaint
    paint.Initialize()
    paint.setFilterBitmap(True)
    paint.SetAntiAlias(True)
    
    Dim bmp1 As Bitmap = LoadBitmap(File.DirAssets, "TestImage.png")
    Dim rect1 As Rect
    rect1.Initialize(0dip, 0dip, Panel1.Width, Panel1.Width)
    ExtDraw.DrawBitmap(Canvas1, bmp1, Null, rect1, paint)

End Sub
 
Upvote 0
Solution
Top