2014-04-01 2 views
0

나는 게임을 프로그래밍하고 있는데, 현재는 화면 위아래로 움직이는 스프라이트에 현재 애니메이션을 적용하고 있습니다. 스프라이트가 화면에 그려진 후 애니메이션 스프라이트가 있던 곳을 대신하여 영구 비트 맵을 그려 봅니다. 흔적과 같습니다. 그러나 캔버스에 영구적으로 그리거나 캔버스에 비트 맵을 저장하는 방법을 모르므로 onDraw가 업데이트 될 때 화면에 그대로 유지됩니다. 나는 비트 맵에서 그려진 경로를 남기고 화면 아래로 움직이는 캐릭터를 원한다. 이 일을 할 수있는 방법이 있습니까?캔버스에 흔적을 남기려면 어떻게해야합니까? (안드로이드)

기본 솔루션

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); 
} 

그래서 모든을 : 나는 7

+0

트레일은 다른 스프라이트가 될 것입니다 – rupps

+0

예 트레일은 또 다른 스프라이트가 될 것입니다. 현재 스프라이트가 비트 맵으로 변환되어 그려져 있지만 그 방법대로 작동하지 않습니다. – user3471603

+0

캐릭터처럼 페인트해야합니다. 무엇이 문제입니까? – rupps

답변

0

아이디어는 성격과 독립적으로 계산의 흔적을하는 것입니다 Windows에서 자바와 안드로이드 SDK (이클립스)를 사용하여 프로그래밍하고 잠시 후에

add_point_to_path (mPosX, mPosY); 

으로 전화를 걸면 현재 스프라이트 위치가 경로에 추가됩니다.

mPathPaint를 변경하면 추측 할 수있는대로 경로의 종류를 사용자 정의 할 수 있습니다.

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) 
} 

그래서 모든 페인트합니다 또한,

또 다른 해결책은 모든 것을 아래에, 투명, 비트 맵을 만드는 것입니다

더 나은 솔루션 등 ... 호, 곡선을 추가 할 수 있습니다 그 비트 맵의 ​​흔적. 이것은 성능에 전혀 영향을 미치지 않고 경로가 매우 복잡 할 수 있다는 장점이 있습니다.

+0

mCharacterPaint를 이미지 또는 비트 맵으로 사용하려면 어떻게해야합니까? 그게 가능하니? – user3471603

+0

mCharactedPaint는 알파, 스트로크 폭 등의 씬을 제어 할 수있는 페인트 객체입니다. 실제 비트 맵은 mCharacterBitmap입니다. – rupps

+0

그래서 mCharacterBitmap은 그려지는 객체입니까? 흔적은 폭, 뇌졸중, 알파가있는 임의의 선입니까? 비트 맵을 화면 아래에서 움직이는 스프라이트의 실제 흔적으로하고 싶습니다. 이것은 스프라이트의 뒤에 경로를 그리지 않고 뚜렷한 페인트 특성을 가진 선 객체로 나타 납니까? – user3471603

관련 문제