Android Question PDF render page, zoom and swipe

Perdita

New Member
Licensed User
Longtime User
Hey there,
I have a PDF file in an ImageView, along with buttons to go back and fwd to pages.
I would like to be able to add Swipe and Zoom.
I think the Gestures Library is the way to go but am not sure how to implement it.
Any help would be most appreciated ;-)

Here is my code:
Dim pdfr As PdfRenderer
Dim pdfPages As Int
Private lblpage As Label
Private activepage As Int
Private btnNext As Button
Private btnPrev As Button
Private img As ImageView
Private Panel1 As Panel

Dim GD As GestureDetector
Dim pnlTwoFingers As Panel
Dim C As Canvas
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("Main")
GD.SetOnGestureListener(Activity, "Gesture")

End Sub

Sub Activity_Resume
pdfr.Initialize("pdf",File.DirRootExternal,"pjc1.pdf", File.DirRootExternal&"/PDFs")
pdfPages = pdfr.PageCount
activepage = 0
pdfr.renderPage(activepage)

End Sub
Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub pdf_pagerendered (page As Int, pagecount As Int, image As Object)
activepage = page
If activepage = pagecount-1 Then

btnNext.Enabled = False
Else
btnNext.Enabled = True
End If
If activepage = 0 Then
btnPrev.Enabled = False
Else
btnPrev.Enabled = True
End If
lblpage.Text = (page+1)&"/"&pagecount
Log("b4a pagerendered ("&page&","&pagecount&")")
img.Bitmap = image
End Sub
Sub btnPrev_Click
If activepage > 0 Then
pdfr.renderPage(activepage-1)
End If
End Sub
Sub btnNext_Click
If activepage < pdfPages Then
pdfr.renderPage(activepage+1)
End If
End Sub


Sub ResizePanel(MotionEvent As Object, Color As Int)
Dim Left, Top, Width, Height As Int
If GD.getX(MotionEvent, 0) > GD.getX(MotionEvent, 1) Then
Left = GD.getX(MotionEvent, 1)
Else
Left = GD.getX(MotionEvent, 0)
End If
If GD.getY(MotionEvent, 0) > GD.getY(MotionEvent, 1) Then
Top = GD.getY(MotionEvent, 1)
Else
Top = GD.getY(MotionEvent, 0)
End If
Width = Abs(GD.getX(MotionEvent, 0) - GD.getX(MotionEvent, 1))
Height = Abs(GD.getY(MotionEvent, 0) - GD.getY(MotionEvent, 1))
Panel1.SetLayout(Left, Top, Width, Height)
pnlTwoFingers.Color = Color

End Sub

Sub Gesture_onTouch(Action As Int, X As Float, Y As Float, MotionEvent As Object) As Boolean
Log("onTouch action=" & Action & ", x=" & X & ", y=" & Y & ", ev=" & MotionEvent)
Return True 'True = Handle this touch event, False = Ignore it
End Sub
Sub Gesture_onDown(X As Float, Y As Float, MotionEvent As Object)
Log(" onDown x = " & X & ", y = " & Y & ", ev = " & MotionEvent)
Log(" pressure = " & GD.getPressure(MotionEvent, 0))
Log(" size = " & GD.getSize(MotionEvent, 0))
End Sub
Sub Gesture_onPointerDown(ptrIndex As Int, PID As Int, MotionEvent As Object)
Log("onPointerDown ptrindex=" & ptrIndex & ", pid=" & PID & ", ev=" & MotionEvent)
ResizePanel(MotionEvent, Colors.White)
Panel1.Visible = True
End Sub
Sub Gesture_onPointerUp(ptrIndex As Int, PID As Int, MotionEvent As Object)
Log("onPointerUp ptrindex=" & ptrIndex & ", pid=" & PID & ", ev=" & MotionEvent)
Panel1.Visible = False
End Sub
Sub Gesture_onPinchOpen(NewDistance As Float, PreviousDistance As Float, MotionEvent As Object)
Log(" onPinchOpen dist=" & NewDistance & ", prevdist=" & PreviousDistance & ", ev=" & MotionEvent)
ResizePanel(MotionEvent, Colors.Green)
End Sub
Sub Gesture_onPinchClose(NewDistance As Float, PreviousDistance As Float, MotionEvent As Object)
Log(" onPinchClose dist=" & NewDistance & ", prevdist=" & PreviousDistance & ", ev=" & MotionEvent)
ResizePanel(MotionEvent, Colors.Red)
End Sub
Sub Gesture_onRotation(Degrees As Double, MotionEvent As Object)
Log(" onRotation degrees=" & Degrees & ", ev=" & MotionEvent)
If Panel1.Width > 0 And Panel1.Height > 0 Then
C.Initialize(Panel1)
C.DrawTextRotated("Angle=" & Round(Degrees) & "°", Panel1.Width / 2, Panel1.Height / 2, Typeface.DEFAULT_BOLD, 16, Colors.Black, "CENTER", Degrees)
End If
End Sub
Sub Gesture_onSingleTapUp(X As Float, Y As Float, MotionEvent As Object)
Log(" onSingleTapUp x = " & X & ", y = " & Y & ", ev = " & MotionEvent)
End Sub
Sub Gesture_onSingleTapConfirmed(X As Float, Y As Float, MotionEvent As Object)
Log(" onSingleTapConfirmed x = " & X & ", y = " & Y & ", ev = " & MotionEvent)
'ToastMessageShow("Single-Tap", False)
End Sub
Sub Gesture_onDoubleTap(X As Float, Y As Float, MotionEvent As Object)
Log(" onDoubleTap x = " & X & ", y = " & Y & ", ev = " & MotionEvent)
'ToastMessageShow("Double-Tap", False)
End Sub
Sub Gesture_onShowPress(X As Float, Y As Float, MotionEvent As Object)
Log(" onShowPress x=" & X & ", y=" & Y & ", ev=" & MotionEvent)
End Sub
Sub Gesture_onLongPress(X As Float, Y As Float, MotionEvent As Object)
Log(" onLongPress x=" & X & ", y=" & Y & ", ev=" & MotionEvent)
'ToastMessageShow("Long Press", False)
End Sub
Sub Gesture_onScroll(distanceX As Float, distanceY As Float, MotionEvent1 As Object, MotionEvent2 As Object)
Log(" onScroll distanceX = " & distanceX & ", distanceY = " & distanceY & ", ev1 = " & MotionEvent1 & ", ev2=" & MotionEvent2)
Log(" X1, Y1 = " & GD.getX(MotionEvent1, 0) & ", " & GD.getY(MotionEvent1, 0))
Log(" X2, Y2 = " & GD.getX(MotionEvent2, 0) & ", " & GD.getY(MotionEvent2, 0))
End Sub
Sub Gesture_onFling(velocityX As Float, velocityY As Float, MotionEvent1 As Object, MotionEvent2 As Object)
Log(" onFling velocityX = " & velocityX & ", velocityY = " & velocityY & ", ev1 = " & MotionEvent1 & ", ev2 = " & MotionEvent2)
'ToastMessageShow("Fling", False)
End Sub
 
Top