Анимации в Android - PullRequest
       13

Анимации в Android

0 голосов
/ 28 февраля 2012

Я новичок в Android и хочу сделать анимацию. Я пытаюсь заставить мой лист спрайта двигаться автоматически. Но есть проблема с рендерингом экрана. Во время движения он оставляет след. Нажмите здесь, чтобы увидеть скриншот

Это мой код:

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 Ответ

1 голос
/ 01 марта 2012

Я уже решил свою проблему, мне просто нужно добавить "drawColor (color.BLACk);" перед вызовом метода mapAnimation.onDraw ().

...