How to draw a Bitmap as a trapezoid?

NeoTechni

Well-Known Member
Licensed User
Longtime User
Without using opengl, I want to draw a couple square bitmaps as a trapezoid.

ie: /_\

Now I can do it by manually shrinking each line, but I was hoping there was faster API to do it.

I've read drawbitmapmesh should do it, but our canvas object doesnt support it.
ABExtDrawing apparently adds that, so I'll look in on it.
 
Last edited:

NeoTechni

Well-Known Member
Licensed User
Longtime User
B4X:
Sub DrawTrapezoid(myBG As Canvas, BMP As Bitmap , X As Int, Y As Int, TopWidth As Int, BottomWidth As Int, Height As Int)
   Dim BottomLeft As Int
   If BottomWidth> TopWidth Then X = X + (BottomWidth-TopWidth)/2
   BottomLeft = X+ TopWidth/2 - BottomWidth/2
   mMatrix.setPolyToPoly( Array As Float(0,0,    BMP.Width,0,   BMP.Width, BMP.Height,   0,BMP.Height), 0,  Array As Float( X,Y, X+TopWidth,Y, BottomLeft+BottomWidth, Y+Height, BottomLeft, Y+Height   ), 0,4)
   ExDraw.drawBitmap4(myBG, BMP,  mMatrix, mPaint)
End Sub

Gees, even the documentation on Google's site sucked.
I had to guess at how to do it.
 
Last edited:
Upvote 0

NeoTechni

Well-Known Member
Licensed User
Longtime User
I improved it with:

B4X:
Sub DrawTrapezoid(BG As Canvas, BMP As Bitmap ,SRC As Rect , X As Int, Y As Int, TopWidth As Int, BottomWidth As Int, Height As Int, Alpha As Int)
    Dim BottomLeft As Int
    ExDraw.save2(BG, LCAR.ExDraw.MATRIX_SAVE_FLAG)
    If TopWidth=0 Then TopWidth=BottomWidth
    If BottomWidth=0 Then BottomWidth=TopWidth
    If BottomWidth> TopWidth Then X = X + (BottomWidth-TopWidth)/2
    BottomLeft = X+ TopWidth/2 - BottomWidth/2
    If SRC=Null Then SRC.Initialize(0,0, BMP.Width, BMP.Height)
    mPaint.Initialize
    mPaint.SetAlpha(Alpha)
    mMatrix.setPolyToPoly( Array As Float(SRC.Left,SRC.Top,  SRC.Right,SRC.Top,  SRC.Right, SRC.Bottom,        SRC.Left,SRC.Bottom), 0,  Array As Float( X,Y, X+TopWidth,Y, BottomLeft+BottomWidth, Y+Height, BottomLeft, Y+Height  ), 0,4)
    ExDraw.drawBitmap4(BG, BMP,  mMatrix, mPaint)
    ExDraw.restore(BG) 'before Activity.Invalidate
End Sub

But I'm having some trouble, in that it's not just drawing what's inside the SRC rect, but the entire bitmap around it too. How do I turn that off?
 
Upvote 0

Informatix

Expert
Licensed User
Longtime User
Without using opengl, I want to draw a couple square bitmaps as a trapezoid.

ie: /_\

Now I can do it by manually shrinking each line, but I was hoping there was faster API to do it.

I've read drawbitmapmesh should do it, but our canvas object doesnt support it.
ABExtDrawing apparently adds that, so I'll look in on it.
My Accelerated Surface library allows to do that (with DrawBitmapMesh).
 
Upvote 0

NeoTechni

Well-Known Member
Licensed User
Longtime User
I'd prefer to use ABExtDrawing as I already use the library for something else and I don't want to bloat my app (it's already up to 2 megabytes!) and my code is mostly done as it is. I'm sure it's like one line of code setting a flag or something
 
Upvote 0
Top