简体   繁体   中英

How to draw onto Canvas leaving a trail? (Android)

I am programming a game and right now I am currently animating a sprite that is moving up and down the screen. After the sprite is drawn on screen, I am trying draw a permanent bitmap in place of where the animated sprite just was, sort of like a trail. However, I'm not sure how to permanently draw unto a Canvas or save the bitmap unto the canvas so it stays on screen when onDraw is updated. I want the character who is moving down the screen to leave a path behind him that is drawn from a bitmap. Is there any way to do this? I am programming using the Android SDK(Eclipse) with Java on Windows 7.

The idea is to have the character and its trail independently calculated:

Basic Solution

Path mCharacterPath=new Path();


public void onDraw(Canvas canvas) {

      canvas.drawBitmap (mCharacter, mPosX, mPosY, mCharacterPaint);
      canvas.drawPath (mCharacterPath, mPathPaint);

}

public void add_point_to_path(int x, int y) {
       mPath.lineTo(x,y);
}

So every some time, you just call

add_point_to_path (mPosX, mPosY);

and it will add the current sprite position to the path.

Changing the mPathPaint you can customize the kind of path, as you can guess. Also, you can add arcs, curves, etc...

BEtter solution

Another solution is to create a bitmap, transparent, below everything:

public void onDraw(Canvas canvas) {


     // we draw our auxiliary bitmap that contains all the trails
     canvas.drawBitmap (mTransparentBitmapForTrails, 0, 0, mTransparentBitmapPaint);
     .
     // then we draw the usual stuff, characters, etc...
     canvas.drawBitmap (mCharacterBimap, mPosX, mPosY, mCharacterPaint);
     .
     .
} 

// this creates a transparent background the size of the screen
// you will render trails here and they all will be painted at once in onDraw below the craracters

Bitmap mBitmapForTrails;
Canvas mCanvasForTrails;

public void initializeBitmapForTrails() {
      mBitmapForTrails=new Bitmap(); // checkout how to create bitmaps with specific w&h, can't remember at the moment. 
      mCanvasForTrails=new Canvas(mBitmapForTrails); // we create a canvas to draw in that bitmap
}

// this will paint the trail in our secondary bitmap. This secondary bitmap doesn't get
// erased, so the drawTrail()'s are accumulative

public void drawTrail (int x, int y) {
   mCanvasForTrails.drawBitmap(your_cool_trail_bitmap, x, y, mTrailPaint);
   invalidate(); // this works, but for performance you'll later do invalidate(rect), so Android only repaints the changed portion of the bitmap. That rect will be the rectangle occupied by the trail bitmap. ie. (x,y,x+trailw,y+trialh)
}

so you will paint all the trails to that bitmap. This has the advantage that the paths can be very complex without affecting performance at all.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM