본문 바로가기
Android/그림으로 쉽게 설명하는 안드로이드 프로그래밍 개정6판

[Android/그림으로 쉽게 설명하는 안드로이드 프로그래밍 개정6판][예제]PingPong 응용 - 볼을 비트맵으로 바꾸기(과제)

by starfish22 2022. 10. 18.
728x90

▶볼을 비트맵으로 바꾸기

비트맵을 이용하여 사진을 떠다니게 한다.

 

▶코드 작성

MainActivity.java

package kr.co.company.pingpong;

import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {
    MySurfaceView view;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        view = new MySurfaceView(this);
        setContentView(view);
        setTitle("Doge PingPong");
    }

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

    protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); }
}

MySurfaceView.java

package kr.co.company.pingpong;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;

class Ball extends View {
    int x, y, xInc = 1, yInc = 1;
    int width, height;
    static int WIDTH = 1080, HEIGHT = 1575;
    Resources resources;

    public Ball(Context context, int w, int h) {
        super(context);
        resources = context.getResources();
        width = w;
        height = h;

        x = (int) (Math.random() * (WIDTH - width) + 3);
        y = (int) (Math.random() * (HEIGHT - height) + 3);

        xInc = (int) (Math.random() * 5 + 5);
        yInc = (int) (Math.random() * 5 + 5);
    }

    public void paint(Canvas g) {
        Paint paint = new Paint();

        if (x < 0 || x > (WIDTH - width)) xInc = -xInc;
        if (y < 0 || y > (HEIGHT - height)) yInc = -yInc;

        x += xInc;
        y += yInc;

        Bitmap doge = BitmapFactory.decodeResource(resources, R.drawable.doge);
        Bitmap sDoge = Bitmap.createScaledBitmap(doge, width, height, false);
        g.drawBitmap(sDoge, x, y, paint);
    }
}

public class MySurfaceView extends SurfaceView implements SurfaceHolder.Callback {
    public Ball basket[] = new Ball[10];
    private MyThread thread;

    public MySurfaceView(Context context) {
        super(context);

        SurfaceHolder holder = getHolder();
        holder.addCallback(this);

        thread = new MyThread(holder);

        for (int i = 0; i < 10; i++) basket[i] = new Ball(context, 300, 300);
    }

    public MyThread getThread() {
        return thread;
    }

    public void surfaceCreated(SurfaceHolder holder) {
        thread.setRunning(true);
        thread.start();
    }

    public void surfaceDestroyed(SurfaceHolder holder) {
        boolean retry = true;

        thread.setRunning(false);
        while (retry) {
            try {
                thread.join();
                retry = false;
            } catch (InterruptedException e) {
            }
        }
    }

    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
        //Ball.WIDTH=width;
        //Ball.HEIGHT=height;
    }

    public class MyThread extends Thread {
        private boolean mRun = false;
        private SurfaceHolder mSurfaceHolder;

        public MyThread(SurfaceHolder surfaceHolder) {
            mSurfaceHolder = surfaceHolder;
        }

        @Override
        public void run() {
            while (mRun) {
                Canvas c = null;
                try {
                    c = mSurfaceHolder.lockCanvas(null);
                    c.drawColor(Color.DKGRAY);
                    synchronized (mSurfaceHolder) {
                        for (Ball b : basket) {
                            b.paint(c);
                        }
                    }
                } finally {
                    if (c != null) {
                        mSurfaceHolder.unlockCanvasAndPost(c);
                    }
                }
            }
        }

        public void setRunning(Boolean b) {
            mRun = b;
        }
    }
}

 

▶실행 영상

 

 

728x90

댓글