Android Code Snippet Unfiltered ImageView SetImageViewUnfiltered()

SubName: SetImageViewUnfiltered()

Description:When you create an ImageView that has dimentions greater than the image it contains, Android will apply some filters to it to make the image look smooth. Sometimes, that is not what you want. I made this little sub that takes an imageView and a BitMap, and removes the filtering

B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Dim bmp As Bitmap

End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
    Dim imvFiltered As ImageView
    Dim imvUnfiltered As ImageView
    Dim lblFiltered As Label
    Dim lblUnfiltered As Label

End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("Layout1")
   
    'Create the label and draw a checkerboard on it
    bmp.InitializeMutable(32,32)
    Dim cnv As Canvas
    cnv.Initialize2(bmp)
    cnv.DrawColor(Colors.Black)
    Dim x, y As Int
    For x = 0 To 31
        For y = 0 To 31
            If Bit.And(x,1) = Bit.And(y,1) Then cnv.DrawPoint(x,y,Colors.White)
        Next
    Next
   
    'initialize the image views
    imvFiltered.Initialize("")
    imvUnfiltered.Initialize("")
   
    'set the bitmap to the Filtered imageview
    imvFiltered.SetBackgroundImage(bmp)
    imvFiltered.Gravity = Gravity.FILL
    'Set the bitmap to the unfiltered imageview
    SetImageViewUnfiltered(imvUnfiltered,bmp)
    imvUnfiltered.Gravity = Gravity.FILL
   
    'Place everything on the screen
    Activity.AddView(imvFiltered,0,0,50%x,50%x)
    Activity.AddView(imvUnfiltered,50%x,0,50%x,50%x)
   
    lblFiltered.Initialize("")
    lblFiltered.Text = "Filtered"
    Activity.AddView(lblFiltered,0,60%x,50%x,20%y)
   
    lblUnfiltered.Initialize("")
    lblUnfiltered.Text = "Unfiltered"
    Activity.AddView(lblUnfiltered,50%x,60%x,50%x,20%y)
   
   

End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

'This is where the magic happens.
Sub SetImageViewUnfiltered(iv As ImageView, bp As Bitmap)
    Dim bd As BitmapDrawable
    bd.Initialize(bp)
    Dim jobd As JavaObject  = bd
    jobd.RunMethod("setAntiAlias", Array As Object(False))
    jobd.RunMethod("setDither", Array As Object(False))
    jobd.RunMethod("setFilterBitmap", Array As Object(False))
   
    Dim joiv As JavaObject = iv
    joiv.RunMethod("setImageDrawable",Array As Object(bd))
   
End Sub

Dependencies: JavaObject library

Tags: ImageView, Unfiltered, SetAntiAlias, SetDither, SetFilterBitmap
 
Top