Android Question Help with accelerometer and imageview

Douglas Farias

Expert
Licensed User
Longtime User
Hi all.
i dont know how to explain exact this

i have a big imageview on my activity
B4X:
imgmao.SetLeftAndRight(-50%x,150%x)
imgmao.SetTopAndBottom(-50%y,150%y)

i need to make the user tilt the device and move to left,right,top etc.
for example the activity size its (0%x to 100%x) if i tilt my device to right i got (10%x to 110%x) etc...

this example by @Erel
https://www.b4x.com/android/forum/threads/orientation-and-accelerometer.6647/#content
already show the x based on accelerometer.
but how to move one imageview or activity based on accelerometer x and y?




i m tryed


B4X:
Sub Sensor_SensorChanged (Values() As Float)

    Dim ps As PhoneSensors
    Dim sd As SensorData
    ps = Sender
    sd = SensorsMap.Get(ps)

    If sd.ThreeValues Then
        imgmao.Left = PerXToCurrent(NumberFormat(Values(0), 0, 3)*100)
    Else
        imgmao.Left = PerXToCurrent(NumberFormat(Values(0), 0, 3)*100)
    End If
   
   
End Sub

This is moving the image to left and right with tilt phone but its very fast and make a bug on background like this



how can i correct move the imageview with tilt, based on %x and %y?
and how to stop this bug when i move the imageview?

thx
 
Last edited:

eurojam

Well-Known Member
Licensed User
Longtime User
Hi Douglas,
I assume you want to move the image while moving the phone. I did something similar before using the a canvas and 2 rectangles to draw a part of the image. See code below and attached project. the project has a panorama, which you can move with the phone...not perfect, but it goes in that direction I think...
B4X:
Sub Process_Globals
    Dim Accelerometer As PhoneAccelerometer

End Sub

Sub Globals
    Dim can As Canvas
    Dim srect As Rect
    Dim drect As Rect

    Dim bm As Bitmap
End Sub

Sub Activity_Create(FirstTime As Boolean)

    can.Initialize(Activity)
    bm.Initialize(File.DirAssets,"PANO.jpg")
   
    Dim l As Int = (bm.Width/2)-Activity.Width/2 'left
    Dim r As Int = (bm.Width/2)+Activity.Width/2  'right

    Dim t As Int = (bm.height/2)-Activity.height/2 'top
    Dim b As Int = (bm.height/2)+Activity.height/2  'bottom
   
    'source middle of Panorama
    srect.Initialize(l,t,r,b)
   
    drect.Initialize(0,0, Activity.width, Activity.Height)
   
    can.DrawBitmap( bm, srect,drect)

End Sub

Sub Activity_Resume
    Accelerometer.StartListening("Accelerometer")
End Sub

Sub Activity_Pause (UserClosed As Boolean)
    Accelerometer.StopListening
End Sub

Sub Accelerometer_AccelerometerChanged (X As Float, Y As Float, Z As Float)
    Log (x & " " & Y & " " & Z)

    srect.Left = srect.Left - (y*100)
   
    'avoid negative values of left
    If srect.Left < 0 Then
        srect.Left = 0
        srect.right = Activity.width
        Return
    End If
    srect.right = srect.right -(y*100)
   
    'avoid values > imagewidth for right
    If srect.right > bm.width Then
        srect.Left = bm.width-Activity.Width
        srect.right = bm.width
        Return
    End If
       
    can.DrawBitmap( bm, srect,drect)
    Activity.Invalidate
   
End Sub
 

Attachments

  • test.zip
    360.3 KB · Views: 354
Upvote 0

Douglas Farias

Expert
Licensed User
Longtime User
many thx @eurojam i will try make changes on your code, this is based on pic size right?
i need based on imageview and with top and botton event.

i will try make changes and use your example, many thx
 
Upvote 0

eurojam

Well-Known Member
Licensed User
Longtime User
yes, it is based on the pic size. at least you will need the aspect (imge.height/image.width) to displa the image correctly. The clue is the two rectangles, the source rec gives you the part of the image you will show in image dimensions, the destination rec the part of the canvas where you draw this part on.

best
stefan
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…