如何每x秒绘制一次比特图?

时间:2017-11-01 18:41:27

标签: java android multithreading canvas android-bitmap

我试图每隔x秒生成一个新的位图(3会起作用)。我已经尝试了THISTHISTHIS,但由于我需要访问我的canvas参数(您将在代码中看到),所以无济于事。现在,我可以在开始游戏时产生一个精灵,沿y轴向下移动,就是这样。我已经尝试了一个新的Thread,一个Runnable和一个Timer / TimerTask,但由于我的canvas需要访问,我无法实现任何这些。

如何每隔x秒执行渲染(Canvas画布)?

这是我的MainActivity,下面是GameView(SurfaceView):

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Point;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.SoundPool;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.Display;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import java.util.Random;

public class MainActivity extends Activity {
    //public static AssetManager assets;
    //public static final int GAME_WIDTH = 800;
    //public static final int GAME_HEIGHT = 450;

    GameView gameView;
    FrameLayout game;
    RelativeLayout widgets;
    ImageButton leftButton;
    ImageButton rightButton;
    ImageButton leftFireButton;
    ImageButton rightFireButton;
    ImageView ship;
    float shipX;
    MediaPlayer mp;
    static final int leftButtonID = 1;
    static final int rightButtonID = 2;
    static final int leftFireButtonID = 3;
    static final int rightFireButtonID = 4;
    //static final int laserId = 5;
    SoundPool soundPool;
    int soundID;
    AsteroidController asteroidController;
    BulletController bulletController;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ship = new ImageView(this);
        gameView = new GameView(this);
        game = new FrameLayout(this);
        widgets = new RelativeLayout(this);
        leftButton = new ImageButton(this);
        rightButton = new ImageButton(this);
        leftFireButton = new ImageButton(this);
        rightFireButton = new ImageButton(this);
        asteroidController = new AsteroidController(this);
        bulletController = new BulletController(this);


        leftButton.setId(leftButtonID);
        rightButton.setId(rightButtonID);
        leftFireButton.setId(leftFireButtonID);
        rightFireButton.setId(rightFireButtonID);
        //laser.setId(laserId);


        leftButton.setImageResource(R.drawable.left_arrow);
        rightButton.setImageResource(R.drawable.right_arrow);
        leftFireButton.setImageResource(R.drawable.red_button);
        rightFireButton.setImageResource(R.drawable.red_button);
        ship.setImageResource(R.drawable.spaceship_1_80x70);


        //add views to screen
        game.addView(gameView);
        game.addView(widgets);
        widgets.addView(leftButton);
        widgets.addView(rightButton);
        widgets.addView(leftFireButton);
        widgets.addView(rightFireButton);
        widgets.addView(ship);
        leftButton.setBackgroundColor(Color.TRANSPARENT);
        rightButton.setBackgroundColor(Color.TRANSPARENT);
        leftFireButton.setBackgroundColor(Color.TRANSPARENT);
        rightFireButton.setBackgroundColor(Color.TRANSPARENT);

        mp = MediaPlayer.create(this, R.raw.bass_loop);
        mp.setLooping(true);
        mp.start();
        loadSounds(this);




       leftButton.setOnTouchListener(new View.OnTouchListener() {
           private Handler handler;

           @Override
           public boolean onTouch(View view, MotionEvent motionEvent) {
               switch(motionEvent.getAction()) {
                   case MotionEvent.ACTION_DOWN:
                       if(handler != null) return true;
                       handler = new Handler();
                       handler.postDelayed(action,50);
                       break;
                       case MotionEvent.ACTION_UP:
                           if(handler == null) return true;
                           handler.removeCallbacks(action);
                           handler = null;
                           break;
               }
               return true;
           }
           Runnable action = new Runnable() {
               @Override public void run() {
                   Display display = getWindowManager().getDefaultDisplay();
                   Point size = new Point();
                   display.getSize(size);
                   //int width = size.x;
                   shipX = ship.getX() - 25;
                   //PREVENT SHIP FROM GOING OFF SCREEN
                   if(shipX <= 0) {
                       leftButton.setClickable(false);
                   } else {
                       leftButton.setClickable(true);
                       ship.setX(shipX);
                       handler.postDelayed(this, 50);
                   }
               }
           };
       });


        rightButton.setOnTouchListener(new View.OnTouchListener() {
            private Handler handler;

            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                switch(motionEvent.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        if(handler != null) return true;
                        handler = new Handler();
                        handler.postDelayed(action,50);
                        break;
                    case MotionEvent.ACTION_UP:
                        if(handler == null) return true;
                        handler.removeCallbacks(action);
                        handler = null;
                        break;
                }
                return true;
            }
            Runnable action = new Runnable() {
                @Override public void run() {
                    Display display = getWindowManager().getDefaultDisplay();
                    Point size = new Point();
                    display.getSize(size);
                    int width = size.x;
                    shipX = ship.getX() + 25;
                    //PREVENT SHIP FROM GOING OFF SCREEN
                    if(shipX >= width - 200) {
                        rightButton.setClickable(false);
                    } else {
                        rightButton.setClickable(true);
                        ship.setX(shipX);
                        handler.postDelayed(this, 50);
                    }
                }
            };
        });



        leftFireButton.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                switch(motionEvent.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        //TODO FIRE BULLET....
                        leftFireButton.setClickable(true);
                        bulletController.addBullet(new Bullet((ship.getX() + ship.getWidth() / 2) - 15, ship.getY() + ship.getHeight() / 2, getApplicationContext()));
                        leftFireButton.setBackgroundResource(R.drawable.red_button_pressed);
                        soundPool.play(soundID, 1.0f, 0.5f, 1, 0, 1.0f);

                        return true;
                    case MotionEvent.ACTION_UP:
                        leftFireButton.setBackgroundResource(R.drawable.red_button);
                        //widgets.removeView(laser);
                        return true;
                }

                return false;
            }
        });
        rightFireButton.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                switch(motionEvent.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        //TODO FIRE BULLET                           
                        bulletController.addBullet(new Bullet((ship.getX() + ship.getWidth() / 2) - 15, ship.getY() + ship.getHeight() / 2, getApplicationContext()));
                        rightFireButton.setBackgroundResource(R.drawable.red_button_pressed);
                        soundPool.play(soundID, 1.0f, 0.5f, 1, 0, 1.0f);

                        return true;
                    case MotionEvent.ACTION_UP:
                        rightFireButton.setBackgroundResource(R.drawable.red_button);
                        //widgets.removeView(laser);

                        return true;
                }

                return false;
            }
        });


        //Setup ship
        RelativeLayout.LayoutParams shipParams = new RelativeLayout.LayoutParams(250, 250);

        //Setup a 200 x 200 ImageView for Left Button
        RelativeLayout.LayoutParams leftBtn = new RelativeLayout.LayoutParams(200,200);

        //Setup a 200 x 200 ImageView for Right Button
        RelativeLayout.LayoutParams rightBtn = new RelativeLayout.LayoutParams(200,200);

        //Setup a 200 x 200 ImageView for Left Fire Button
        RelativeLayout.LayoutParams leftFireBtn = new RelativeLayout.LayoutParams(200,200);

        //Setup a 200 x 200 ImageView for Right Fire Button
        RelativeLayout.LayoutParams rightFireBtn = new RelativeLayout.LayoutParams(200,200);

        //Add rules to align the left button programmatically
        leftBtn.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
        leftBtn.addRule(RelativeLayout.ALIGN_PARENT_LEFT);

        //Add rules to align the right button programmatically
        rightBtn.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
        rightBtn.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);

        //Add rules to align left and right fire buttons
        leftFireBtn.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
        rightFireBtn.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
        leftFireBtn.addRule(RelativeLayout.ABOVE,leftButton.getId());
        rightFireBtn.addRule(RelativeLayout.ABOVE, rightButton.getId());
        leftFireBtn.topMargin = 850;
        rightFireBtn.topMargin = 850;

        shipParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
        shipParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
        shipParams.bottomMargin = 100;

        //Now set the params
        leftButton.setLayoutParams(leftBtn);
        rightButton.setLayoutParams(rightBtn);
        leftFireButton.setLayoutParams(leftFireBtn);
        rightFireButton.setLayoutParams(rightFireBtn);
        ship.setLayoutParams(shipParams);

        //Set the content view of the game
        //this.setContentView(new GameView(this));
        this.setContentView(game);
    }

    public void loadSounds(Context context) {
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            soundPool = new SoundPool.Builder().setMaxStreams(10).build();
        }
        else {
            soundPool = new SoundPool(10, AudioManager.STREAM_MUSIC, 1);
        }
        soundID = soundPool.load(context,R.raw.laser,1);
    }


    @Override
    protected void onPause() {
        super.onPause();
        mp.pause();
        gameView.pause();
    }
    @Override
    protected void onResume() {
        super.onResume();
        mp.start();
        gameView.resume();
    }

    public class GameView extends SurfaceView implements Runnable {
        Thread gameViewThread = null;
        SurfaceHolder surfaceHolder;
        boolean okToRun = true;
        //boolean isAlive = true;
        Bitmap star;
        Bitmap three_pixel_star;
        Bitmap stars;
        Asteroid asteroid0;

        public GameView(Context context) {
            super(context);
            //initialize holder
            surfaceHolder = this.getHolder();
            bulletController = new BulletController(context);
            asteroidController = new AsteroidController(context);
            //asteroid_0 = BitmapFactory.decodeResource(getResources(), R.drawable.asteroid_0);
            //asteroid_1 = BitmapFactory.decodeResource(getResources(), R.drawable.asteroid_1);
            //asteroid_2 = BitmapFactory.decodeResource(getResources(),R.drawable.asteroid_2);
            //asteroid0 = new Asteroid(500, -25, context);
            //asteroid1 = new Asteroid(700, -50, context);
            //asteroid2 = new Asteroid(250, -75, context);

        }

        @Override
        public void run() {
            while(okToRun) {
                if(!surfaceHolder.getSurface().isValid()) {
                    continue;
                }
                Canvas gameCanvas = surfaceHolder.lockCanvas();
                customOnDraw(gameCanvas);
                bulletController.tick();
                asteroidController.tick();
                surfaceHolder.unlockCanvasAndPost(gameCanvas);

            }
        }
        protected void customOnDraw(Canvas canvas) {
            Random random = new Random();
            Random random1 = new Random();
            canvas.drawColor(Color.BLACK);
            stars = BitmapFactory.decodeResource(getResources(), R.drawable.stars);
            star = BitmapFactory.decodeResource(getResources(), R.drawable.single_pixel_star);
            three_pixel_star = BitmapFactory.decodeResource(getResources(),R.drawable.three_pixel_star);
            canvas.drawBitmap(star, random1.nextInt(canvas.getWidth()-star.getWidth()),random1.nextInt(canvas.getHeight()-star.getHeight()), null);
            canvas.drawBitmap(three_pixel_star, random.nextInt(canvas.getWidth()-three_pixel_star.getWidth()),random.nextInt(canvas.getHeight()-three_pixel_star.getHeight()), null);
            bulletController.render(canvas);
            asteroid0.render(canvas);
        }
        public void pause() {
            okToRun = false;
            while(true) {
                try {
                    gameViewThread.join();
                } catch(InterruptedException e) {
                    Log.v("ERROR", e.getMessage());
                }
                break;
            }
            gameViewThread = null;
        }
        public void resume() {
            okToRun = true;
            gameViewThread = new Thread(this);
            gameViewThread.start();
        }
    }
}

这是我的小行星课程:

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;

public class Asteroid  {

    private float x, y;
    private Bitmap asteroid0;

    public Asteroid(float x, float y, Context context) {
        this.x = x;
        this.y = y;
        asteroid0 = BitmapFactory.decodeResource(context.getResources(),R.drawable.asteroid_0);
        //asteroid1 = BitmapFactory.decodeResource(context.getResources(),R.drawable.asteroid_1);
        //asteroid2 = BitmapFactory.decodeResource(context.getResources(),R.drawable.asteroid_2);
    }

    public void tick() {
        //MOVE DOWN Y AXIS
        y += 15;

    }
    public void render(Canvas canvas) { //<-- PARAMETER THAT NEEDS ACCESSED

        canvas.drawBitmap(asteroid0, x, y, null); //<-- BECAUSE OF THIS
        this.tick();

    }

    public float getY() {
        return y;
    }
    public float getX() {
        return x;
    }

}

最后,这是我的AsteroidController类:

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Canvas;
import java.util.LinkedList;


public class AsteroidController {

   private LinkedList<Asteroid> a = new LinkedList<>();
    Asteroid tempAsteroid;
   Context context;
   float x;
   float y;

   public AsteroidController(Context context) {
       this.context = context;
       this.x = x;
       this.y = y;
       addAsteroid(new Asteroid(x, y, context));
   }
   public void tick() {
       for (int i = 0; i < a.size(); i++) {
           tempAsteroid = a.get(i);

           if (tempAsteroid.getY() > getScreenHeight()) {
               removeAsteroid(tempAsteroid);
           }
           tempAsteroid.tick();
       }
   }

   public static int getScreenHeight() {
       return Resources.getSystem().getDisplayMetrics().heightPixels;
   }
   public static float getScreenWidth() {
       //System.out.println(Resources.getSystem().getDisplayMetrics().widthPixels);
       return Resources.getSystem().getDisplayMetrics().widthPixels;
   }
   //NOT BEING USED AT THE MOMENT
   public void render(Canvas canvas) {
       for(int i = 0; i < a.size(); i++) {
           tempAsteroid = a.get(i);
           //tempAsteroid.render(canvas);
       }
   }
   public void addAsteroid(Asteroid asteroid) {

       a.add(asteroid);
   }
   public void removeAsteroid(Asteroid asteroid) {

       a.remove(asteroid);
   }
}

我有其他课程,但这些是与我的问题相关的唯一课程。我已经尝试了上面发布的所有链接,但它们不适用于我的实现(除非我错了)。我怎样才能每隔x秒填充一个(或多个)小行星?感谢。

0 个答案:

没有答案