Android中的动画

时间:2012-02-28 19:24:17

标签: android animation android-canvas

我是Android的新手,我想做一些动画。我正在尝试让我的精灵表自动移动。但是屏幕渲染存在问题。它在移动时会留下痕迹。Click here to see the screen shot

这是我的代码:

public class SampleAnimationActivity extends Activity {
    /** Called when the activity is first created. */

    Screen screen;
    MapAnimation mapAnimation;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        screen = new Screen(this);
        setContentView(screen);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }


    @Override
    protected void onPause() {
        super.onPause();
    }
    @Override
    protected void onResume() {
        super.onResume();
    }

    public class Screen extends SurfaceView implements Callback{

        private SurfaceHolder holder;
        private MySurfaceViewThread mySurfaceViewThread;
        private boolean isSurfaceCreated;
        private Bitmap character, tiles;

        public Screen(Context context) {
            super(context);
            initialize();
        }

        public void initialize(){
            //Create a new SurfaceHolder and assign this class as its callback...
            holder = getHolder();
            holder.addCallback(this);
            isSurfaceCreated = false;
            character = BitmapFactory.decodeResource(getResources(),R.drawable.penguin_sprite);
            tiles = BitmapFactory.decodeResource(getResources(), R.drawable.tile_sprites);
            resume();
        }

        public void resume(){
            //Create and start the graphics update thread.
            if(mySurfaceViewThread == null){
                mySurfaceViewThread = new MySurfaceViewThread();
                if(isSurfaceCreated == true){
                    mySurfaceViewThread.start();
                }
            }
        }

        public void pause(){
            //Kill the graphics update thread
            if(mySurfaceViewThread != null){
                mySurfaceViewThread.pause();
                mySurfaceViewThread = null;
            }
        }

        @Override
        public void surfaceChanged(SurfaceHolder holder, int format, int width,
                int height) {
        }

        @Override
        public void surfaceCreated(SurfaceHolder holder) {
            isSurfaceCreated = true;
            if(mySurfaceViewThread != null){
                mySurfaceViewThread.start();
            }
        }

        @Override
        public void surfaceDestroyed(SurfaceHolder holder) {
            isSurfaceCreated = false;
            pause();
        }

        public class MySurfaceViewThread extends Thread{

            private boolean isPaused;
            private boolean characterLoaded, characterDrawn;
            private SurfaceHolder surfaceHolder;

            public MySurfaceViewThread(){
                super();
                isPaused = false;
                characterLoaded = false;
                surfaceHolder = holder;
                characterDrawn = false;
            }

            public void run(){
                //Repeat the drawing loop until the thread is stopped
                while(!isPaused){
                    if(!surfaceHolder.getSurface().isValid()){
                        continue;
                    }
                    if(characterLoaded == false){
                        mapAnimation = new MapAnimation(screen, character);
                        characterLoaded = true;
                    }
                    Canvas canvas = surfaceHolder.lockCanvas();
                    mapAnimation.onDraw(canvas);
                    surfaceHolder.unlockCanvasAndPost(canvas);
                }
            }

            public void pause(){
            }
            public void onDraw(){
            }
        }
    }   
}


public class MapAnimation {

    private Screen screen;
    private Bitmap character;
    private int width, height, xPosition, yPosition, xSpeed, ySpeed;

    public MapAnimation(Screen screen, Bitmap character) {
        this.screen = screen;
        this.character = character;
        this.width = character.getWidth();
        this.height = character.getHeight();
        xPosition = 0;
        yPosition = 0;
        xSpeed = 5;
        ySpeed = 5;
    }

    public void updateCharacter(){
        if(xPosition > screen.getWidth() - width - xSpeed){
            xSpeed = 0;
            ySpeed = 5;
        }
        if(yPosition > screen.getHeight() - height - ySpeed){
            xSpeed = -5;
            ySpeed = 0;
        }
        if(xPosition + xSpeed < 0){
            xPosition=0;
            xSpeed = 0;
            ySpeed = -5;
        }
        if(yPosition+ySpeed < 0){
            yPosition = 0;
            xSpeed = 5;
            ySpeed = 0;
        }
        xPosition += xSpeed;
        yPosition += ySpeed;
    }

    public void onDraw(Canvas canvas){
        updateCharacter();
        Rect src = new Rect(0, 0,135,225);
        Rect dst = new Rect(xPosition, yPosition, xPosition+width, yPosition+height);
        canvas.drawBitmap(character, src, dst, null);
    }
}

我们将非常感谢您的帮助:)

1 个答案:

答案 0 :(得分:1)

我已经解决了我的问题,我只需要添加“drawColor(color.BLACk);”在调用mapAnimation.onDraw()方法之前。