Sub Activity_Create(FirstTime As Boolean)
'Do not forget to load the layout file created with the visual designer. For example:
'Activity.LoadLayout("Layout1")
Dim W0 As Int = Activity.Width/3 ' Original dimensions for a square bitmap
'***************************************************************
' Will draw something. Can also load a picture
'***************************************************************
Dim B0 As Bitmap
B0.InitializeMutable(W0,W0)
Dim c As Canvas
c.Initialize2(B0)
Dim mRect As Rect
mRect.Initialize(0,0,B0.Width,B0.Height)
c.DrawRect(mRect, Colors.Blue,True, 2dip)
c.DrawCircle(W0/2,W0/2,W0/2,Colors.Yellow,True,4)
Dim IV As ImageView
IV.Initialize("")
Activity.AddView(IV,Activity.Width/2-W0/2, Activity.Height/4-W0/2,W0,W0)
IV.Bitmap = B0
'***************************************************************
' Source points are the boundaries of the original picture
'***************************************************************
Dim srcPoints() As Float = Array As Float( _
0 , 0 , _
W0-1, 0 , _
W0-1, W0-1, _
0 , W0-1 _
)
' Here we define the 4 distorted edges (pairs of coordinates, that are the edges in clockwise order). For reference, keep the first at origin.
' We are defining it to be 2*W0 width, the same height on first vertical side, 2*W0/3 on the second,
' and skewed. Can be changed at will.
Dim dstPoints() As Float = Array As Float( _
0 , 0 , _
2*W0, -W0/3 , _
2*W0, W0/3, _
0 , W0 _
)
'***************************************************************
' Create the distorted version, according to defined points
'***************************************************************
Dim J As JavaObject
J.InitializeContext
Dim B1 As Bitmap = J.RunMethod("distortBitmap", Array(B0,srcPoints, dstPoints))
#if 0
' Tried with JavaObject but get a Runtime Error regarding createBitmap, it does not accept JM (Null) works)
Dim JM As JavaObject = J.InitializeNewInstance("android.graphics.Matrix",Null)
Dim res As Boolean = JM.RunMethod("setPolyToPoly", Array( srcPoints, 0, dstPoints, 0, 4))
Dim JB As JavaObject = J.InitializeStatic("android.graphics.Bitmap")
B1 = JB.RunMethod("createBitmap",Array(B0,0,0,W0,W0,JM,True))
#end if
'***************************************************************
' Now we get an ImageView as large as B1 bounds.
' Will draw it centered on the second half of our portrait display
'***************************************************************
Dim W1 As Int = B1.Width
Dim H1 As Int = B1.Height
Dim IV2 As ImageView
IV2.Initialize("")
Activity.AddView(IV2,Activity.Width/2-W1/2, 3*Activity.Height/4-H1/2,W1,H1)
IV2.Bitmap = B1
End Sub
Sub Activity_Resume
End Sub
Sub Activity_Pause (UserClosed As Boolean)
End Sub
#if JAVA
import android.graphics.Bitmap;
import android.graphics.Matrix;
public Bitmap distortBitmap(Bitmap B0, float[] src,float[] dst) {
Matrix M = new Matrix();
M.setPolyToPoly(src,0,dst,0,4);
Bitmap B1 = Bitmap.createBitmap(B0,0,0,B0.getWidth(),B0.getHeight(), M, true);
return B1;
}
#End If