B4A Library [Lib] Accelerated surface

This library provides a dedicated drawing surface embedded inside a view, which benefits from the hardware acceleration. With it, you get the speed of OpenGL for 2D without the complexity.
It includes many Canvas methods (with anti-aliasing, matrix and camera) and a few useful methods for Bitmaps and Drawables (AlterColors, Crop, LoadNinePatch, ReduceColors, SetDensity, etc.). You can import the Matrix, Camera, Paint and Path objects from another library (if they are not wrapped).

surface.png

imageviews.jpg


You can use it to make games:

princess_tiles.png

space_enemies.png


It includes a tool (TextFactory) to do nice titles (that you can export to a bitmap):

textfactory.jpg


It supports Porter-Duff modes, color filters and texture blending (the processing time is very fast):

pdmodes.jpg


The archive includes four benchmarks:

Perf.png


Because of my lack of (free) time, don't expect answers from me about this library if you're not one of my donors.

Download the latest version (1.12)
To convert a project from v0.9x to v1.x, read this.

Hints & Tips

This library does not work with Android versions < 2 (Eclair and Froyo may exhibit performance problems, so I recommend only Gingerbread or a newer version for animations with a high FPS).
The hardware acceleration is not enabled with Android versions < 3.
 

Attachments

  • Java source - AcceleratedSurface.zip
    21.3 KB · Views: 653
Last edited:

padvou

Active Member
Licensed User
Longtime User
Sorry to bother again.
I try to understand how to draw lines, but I 'm missing something.
I added the following code:
B4X:
ObjPath.Initialize(4dip, 0)
ObjPath.
ObjPath.LineTo(0, -4dip).LineTo(8dip, -4dip).LineTo(12dip, 0).LineTo(8dip, 4dip).LineTo(0, 4dip)

AcSf.Initialize("AcSf", True)
    AcSf.Color = Colors.black
    Activity.AddView(AcSf, 0, 0, 100%x, 100%y)
But nothing is drawn, why??
 

padvou

Active Member
Licensed User
Longtime User
Accelerated Surface rules!
I finally got around with my code adjsustments to it and started to figure out how to use it for my application.
I'm very excited, thank you Frederique!
 

socialnetis

Active Member
Licensed User
Longtime User
Hi, very nice lib, the examples run very good and smooth in my oldy motorola Defy with ICS.
By reading the examples I couldn't tell the exact difference between the draw and update events. What should be done in each event, and how often are they?
 

Informatix

Expert
Licensed User
Longtime User
Hi, very nice lib, the examples run very good and smooth in my oldy motorola Defy with ICS.
By reading the examples I couldn't tell the exact difference between the draw and update events. What should be done in each event, and how often are they?
The Draw event is used to draw things and the Update event to update their properties (mainly their position). The Update event is raised just before the Draw event.
 

Medel

Member
Licensed User
Longtime User
Hi, my name is Luis, I've followed your advice in : http://www.b4x.com/android/forum/threads/opencv-library.12689/page-2#post-208103 post #23, however when I try to run the example program AcceleratedSurface in my smartphone it says:

Parsing code. 0.07
Compiling code. 0.24
Compiling layouts code. 0.01
Generating R file. Error
AndroidManifest.xml:17: error: No resource identifier found for attribute 'hardwareAccelerated' in package 'android'

My cellphone is Android 4.1 so I don't think the OS is the problem, maybe I'm missing something.

a) how can I run the example?
b) how much can someone donate so we can have the libraries?
c) Is it possible to use the get pixel function and the video camera in real time?, my goal is to do camera-color tracking, i've done it in Labview, matlab, CMUCAM4 but I need to do it in a smartphone so I'm looking for a library that allows me to access the pixels in the image to create a 3 dimentional matrix RGB.
 

padvou

Active Member
Licensed User
Longtime User
Let's suppose I 'd like to load a foto taken from the camera as background to the AcSf.
After having loaded it, I would like the user to touch on specific points on the foto and the application should respond by adding small circles with dots in their center, centers being where the up action has been. Is it also possible to calculate the distance from two given points touched on the screen?
Thank you
 

johndb

Active Member
Licensed User
Longtime User
Is it possible to create a custom linestyle using "DashPathEffect" using this library?
Thanks.
 

Informatix

Expert
Licensed User
Longtime User
Hi, my name is Luis, I've followed your advice in : http://www.b4x.com/android/forum/threads/opencv-library.12689/page-2#post-208103 post #23, however when I try to run the example program AcceleratedSurface in my smartphone it says:

Parsing code. 0.07
Compiling code. 0.24
Compiling layouts code. 0.01
Generating R file. Error
AndroidManifest.xml:17: error: No resource identifier found for attribute 'hardwareAccelerated' in package 'android'

My cellphone is Android 4.1 so I don't think the OS is the problem, maybe I'm missing something.

a) how can I run the example?
b) how much can someone donate so we can have the libraries?
c) Is it possible to use the get pixel function and the video camera in real time?, my goal is to do camera-color tracking, i've done it in Labview, matlab, CMUCAM4 but I need to do it in a smartphone so I'm looking for a library that allows me to access the pixels in the image to create a 3 dimentional matrix RGB.
a) In the B4A IDE, you have to select a more recent Android.Jar in Tools/Configure Paths (take the 19 which is the latest).
b) This library is free so I don't understand the question.
c) In real-time, I don't think so.
 

Informatix

Expert
Licensed User
Longtime User
Let's suppose I 'd like to load a foto taken from the camera as background to the AcSf.
After having loaded it, I would like the user to touch on specific points on the foto and the application should respond by adding small circles with dots in their center, centers being where the up action has been. Is it also possible to calculate the distance from two given points touched on the screen?
Thank you
What's exactly the question? The AcSf has a touch event, so you just have to store in a variable the touch coordinates to draw your circles. And the distance is computed with the Pythagorean theorem : d = sqrt((x2 - x1)² + (y2 - y1)²)
 

padvou

Active Member
Licensed User
Longtime User
What's exactly the question? The AcSf has a touch event, so you just have to store in a variable the touch coordinates to draw your circles. And the distance is computed with the Pythagorean theorem : d = sqrt((x2 - x1)² + (y2 - y1)²)

Hi, and best wishes for a happier and better new year!

How exactly do i do this? Load the photo and then add the circle where it is touched and add the coordinates?
Thank you
 

Informatix

Expert
Licensed User
Longtime User
Hi, and best wishes for a happier and better new year!

How exactly do i do this? Load the photo and then add the circle where it is touched and add the coordinates?
Thank you
Happy new year !

All the drawings are done in the Draw event, so to draw a circle after touching the screen, you need to store in a global variable the coordinates of the touch point in your Touch event and use these coordinates in your Draw event to draw the circle. The Draw event is raised regularly if you call StartRegularDraw, so the sequence is:
- a touch event is raised -> I store the coordinates
- the draw event is raised -> if I have new coordinates, I draw a circle with these coordinates
 

johnaaronrose

Active Member
Licensed User
Longtime User
I have a panel (larger than the screen) on which I've placed an Accelerated Surface with same left-top-width-height. I've loaded a bitmap using SetBackgroundImage. I want to be able to scroll horizontally & vertically on the displayed bitmap. Can I do this using the Accelerated Surface?
PS I currently also have a ScrollView2D view also with the same position & size. This should allow scrolling but it doesn't. I suspect this is due to interference between events for the Accelerated Surface & the Scroll2View. Is this the method that you would recommend if the Accelerated Surface won't allow scrolling?
 

Informatix

Expert
Licensed User
Longtime User
I have a panel (larger than the screen) on which I've placed an Accelerated Surface with same left-top-width-height. I've loaded a bitmap using SetBackgroundImage. I want to be able to scroll horizontally & vertically on the displayed bitmap. Can I do this using the Accelerated Surface?

No. You have to place the AS in a ScrollView2D panel.

PS I currently also have a ScrollView2D view also with the same position & size. This should allow scrolling but it doesn't. I suspect this is due to interference between events for the Accelerated Surface & the Scroll2View. Is this the method that you would recommend if the Accelerated Surface won't allow scrolling?

Yes. And it's supposed to work without trouble out of the box.
 

ernschd

Active Member
Licensed User
Longtime User
Hi, after reading the last postings i'm asking myself if it's possible to use Accelerated surface for drawing on a canvas (with fingers)?

You wrote that the position have to be saved in the AC_Touch-Event and the drawing has to be made in AC_Draw-Event, but in my test-app i don't see the path.
 

Informatix

Expert
Licensed User
Longtime User
Hi, after reading the last postings i'm asking myself if it's possible to use Accelerated surface for drawing on a canvas (with fingers)?

You wrote that the position have to be saved in the AC_Touch-Event and the drawing has to be made in AC_Draw-Event, but in my test-app i don't see the path.
I hope that you're kidding. In My Playground, the drawing activity is built upon an Accelerated Surface. I wouldn't be able to do that with another library (except libGDX).
Post your app and I'll show you where's the problem.
 

Informatix

Expert
Licensed User
Longtime User
Ok, here's my app.

Thank you.
I downloaded your file but I cannot run it because it is generated with the latest version and produces an error when I compile (I'm still with v3.0). Anyway, there are two problems: 1) move StartRegularDraw to Activity_Resume; 2) your code draws the last move then clears the drawing (do you really need StartRegularDraw?). You should look at the Paint example of my Gesture Detector library to see how to handle properly the touch events in a drawing program. With the AS library, you have to store all points of the path then draw the lines connecting these points in the Draw event.
 
Top