I have an image background in surface viewenter image description here

I want to move something according to black surface.But i can not do this using height of this image as it return same height.and one more thing bitmap.getPixel is also not working.and my background is moving.So what is the way to achieve this feat


2 Answers 2


To answer the question directly - you need to extract the gradient of the surface from the image. The image pixels need to be analysed using an algorithm such as marching squares. However there are two problems with this approach:

  1. Image analysis uses up valuable processing resources
  2. The result will only be an approximation of the surface

A better solution would be to not use an image in the first place, but to rather keep the path as a vector and use that to render the curve at runtime.


Consider using a pre-existing physics engine, see here for some references: https://stackoverflow.com/questions/1034253/are-there-any-decent-physics-engines-for-android

  • \$\begingroup\$ can you suggest some example for last approach mention by you. \$\endgroup\$
    – Tofeeq
    Commented Dec 21, 2011 at 7:43
  • \$\begingroup\$ You could also pre-calculate the curve and hard code it into your game, if you can't use the vector path.. I don't recommend hard cording though! \$\endgroup\$
    – Zolomon
    Commented Dec 21, 2011 at 11:26
  • \$\begingroup\$ @Zolomon:i want to use vector path as it sound nice so can you suggest me some tutorial for vector path to meet my problem \$\endgroup\$
    – Tofeeq
    Commented Dec 21, 2011 at 11:37
  • \$\begingroup\$ @lukevanin:Library mention by you in link is for java.And i think it will not work for android. \$\endgroup\$
    – Tofeeq
    Commented Dec 21, 2011 at 11:51
  • \$\begingroup\$ @TofeeqAhmad - I'm not sure what you are trying to say, as unless you are writing with NDK, you are using Java on Android. Also, don't add a useless tag to bump this, if that's what you are trying to do... \$\endgroup\$
    – DMan
    Commented Dec 27, 2011 at 2:34

If I understood right, you need a "raster" background image to display and a gemetric shape that describes that background's silhouette for physical simulations issues.

Looking at your example image I can see a bezier profile so you may store the parameters you used to generate it, along with your image; is up to you to store the rasterized version or use the bezier parameters to generate the displaying image (cpu vs storage trade-off).

The interesting case comes if your background shape does not come from a bezier curve or from an equivalent parametrization: and hand-made artwork for example.

I such cases you may find this paper useful. Here is shown how to get a bezier parametrization from an hand-drawn sketch.

They basically illustrate the process with the following steps:

1 - apply a logarithmic transformation to make it easier to extract the edge.

2 - erosion - dilatation is used to smoth the image

3 - laplacian is applied to find out the edges

once they get the edges, as set of points laying on the shape edge, they start to construct a set of (bezier) curves each of which produces a better edge approximation.

4 - the set of edge points are grouped and for each group a regression line segment is computed

5 - using the intersection of the line segments in the step 4 a set of bezier 2nd grade curves are computed

6 - a method to generate 5th degree Bezier Curve is given: the curve is derived from that obtained in the previous step by letting the derivate and curvature discontinuities be smoothed.

All these steps are obviously computationally relevant and are not intended to be done at runtime.

This should not be a problem since you do not do dynamic backgrounds, if you do so you should have some parametrization and the problem solved in (moved to) an earlier phase of the project.


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .