views:

202

answers:

2

Hi, I'm currently writing an android side-scrolling game and am having trouble filling a path with a repeating bitmap image. I'm creating a path from a number of coordinates to make up the "ground" area. I have a character who is fixed in the middle of the canvas and screen and am moving the path to represent the movement of the character. I've been able to fill the path with a repeating image using a BitmapShader. Also I can move the path shape from side to side on the screen. However, the Bitmapshader seems to be using a default origin of 0,0 which means the shader is always drawing the ground repeating image in the same place. This means that even though the path is moving the ground repeated image never appears to move. Does anyone have any idea how to change the origin of the shader or know of a better way to fill the path with a repeating image?

Alternatively, can anyone suggest a better solution for filling a drawable shape with an image?

Thanks Andy

A: 

Have you looked at the Snake example in the android sdk? Also Replica Island is another example of how to do a tile engine in android.

schwiz
A: 

Thanks, had a look at those...Replica Island seems to use OpenGL quite a lot which is a bit beyond me at present and Snake didn't quite do what I was looking for...eventually got there..

    //Shape pathShape = this.getPathShape();
    Bitmap groundImage = ImageHandler.getmGroundImage();
    int offset = groundImage.getWidth()-(xPosition%groundImage.getWidth());
    Path path = new Path();
    path.moveTo(coordinates.get(0).getmX(), coordinates.get(0).getmY());

    for ( ShapeCoordinate coordinate : coordinates ) {
        path.lineTo(coordinate.getmX(), coordinate.getmY());
    }

    path.lineTo(coordinates.get(coordinates.size()-1).getmX(), mYBase);
    path.lineTo(coordinates.get(0).getmX(), mYBase);
    path.lineTo(coordinates.get(0).getmX(), coordinates.get(0).getmY());
    path.close();

    PathShape shape = new PathShape(path,canvas.getWidth(),canvas.getHeight());

    BitmapShader bs = new BitmapShader(groundImage, Shader.TileMode.REPEAT,Shader.TileMode.REPEAT);

    Matrix matrix = new Matrix();
    matrix.reset();
    matrix.setScale(1,1);
    matrix.preTranslate(offset, 0);
    bs.setLocalMatrix(matrix);

    ShapeDrawable sd = new ShapeDrawable(shape);
    sd.setColorFilter(Color.argb(255, 50*(mLevel+1), 50*(mLevel+1), 50*(mLevel+1)), Mode.LIGHTEN);
    sd.getPaint().setShader(bs);
    sd.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
    sd.draw(canvas);   
Fletch