第七天android:弹球1

第十集:弹球

1.画小球,和上集的水波一样

    class GameView extends SurfaceView implements Callback, Runnable {



        private Paint paint = null;

        private Path path; // 画自定义图形

        private SurfaceHolder sh = null;



        public GameView(Context context) {

            super(context);

            paint = new Paint();

            paint.setColor(Color.RED);

            // 设置画笔类型

            paint.setStyle(Style.STROKE);

            path = new Path();

            sh = this.getHolder();

            // 注册一个监听,就是在View改变的时候执行是调用。

            sh.addCallback(this);

        }



        @Override

        public void surfaceDestroyed(SurfaceHolder holder) {

            // 销毁的时候执行内容

        }



        @Override

        public boolean onTouchEvent(MotionEvent event) {

            switch (event.getAction()) {

            case MotionEvent.ACTION_DOWN:

                // 按下的瞬间

                if (list.size() < 10) {

                    list.add(new Ball(event.getX(), event.getY(), 5));

                }

                break;

            case MotionEvent.ACTION_MOVE:

                // 移动

                break;

            default:

                break;

            }

            invalidate();

            // 不把事件传递给父窗体

            return true;

        }



        @Override

        public void surfaceCreated(SurfaceHolder holder) {

            // 创建时候执行

            new Thread(this).start();

        }



        class Ball {

            public Ball(float x, float y, int r) {

                this.x = x;

                this.y = y;

                this.r = r;

                paint = new Paint();

                paint.setColor(Color.RED);

                paint.setStyle(Style.FILL_AND_STROKE);// 填充和画边。

                paint.setStrokeWidth(5);

                speedx = new Random().nextInt(3);

                speedy = new Random().nextInt(3);

            }



            float x = 0;

            float y = 0;

            int r = 0;

            int speedx = 0;

            int speedy = 0;

            Paint paint;// 这个用来画球自己



            public void move() {

                x = x + speedx;

                y = y + speedy;

            }



            public void drawCicle(Canvas canvas) {

                canvas.drawCircle(x, y, r, paint);

            }



        }



        List<Ball> list = new ArrayList<Ball>();



        private void drawView() {

            Canvas canvas = sh.lockCanvas();

            // 刷背景,原来的图像就擦出了。

            canvas.drawColor(Color.BLACK);

            canvas.drawPath(path, paint);

            for (Ball b : list) {

                b.drawCicle(canvas);

            }

            sh.unlockCanvasAndPost(canvas);

        }



        @Override

        public void surfaceChanged(SurfaceHolder holder, int format, int width,

                int height) {

            // 修改的时候执行

        }



        @Override

        protected void onDraw(final Canvas canvas) {

            super.onDraw(canvas);

        }



        @Override

        public void run() {

            while (true) {

                drawView();

                for (Ball b : list) {

                    b.move();

                }

                try {

                    Thread.sleep(100);

                } catch (InterruptedException e) {

                    e.printStackTrace();

                }

            }

        }

    }

 

2.全屏

在AndroidMainifest.xml 中的<application><activity 中

android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen"

3.让小球无规律拖动

  public void move() {

                x = x + new Random().nextInt(6)-3;

                y = y + new Random().nextInt(6)-3;

   }

4.加入声音

在res下新建文件夹raw,将音频文件放入。放入a.mp3

private MediaPlayer mediaPlayer = null;

mediaPlayer = MediaPlayer.create(context, R.raw.a);

mediaPlayer.start();

//sleep();

mediaPlayer.stop();

 

第十集完

你可能感兴趣的:(android)