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

[Android/그림으로 쉽게 설명하는 안드로이드 프로그래밍 개정6판]그림으로 쉽게 설명하는 안드로이드 프로그래밍(개정6판) 연습문제 6장

by starfish22 2022. 10. 10.
728x90

정답이 아닐 수 있습니다. 오류가 있으면 알려주세요ㅎ

 

1. Canvas, Paint

 

2. void onDraw(Canvas canvas)

 

3. <kr.co.company.myview.MyView

(패키지가 kr.co.company.myview 이고, 클래스명이 MyView 일 때)

 

4.

MainActivity.java

package kr.co.company.ch6;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.os.Bundle;
import android.view.View;

import androidx.appcompat.app.AppCompatActivity;

class MyView extends View {
    private Paint paint;
    private RectF rectF;
    private float start, sweep;
    private static final float SWEEP_INC = 2;
    private static final float START_INC = 15;

    public MyView(Context context) {
        super(context);
        paint = new Paint();
        paint.setAntiAlias(true);
        paint.setStyle(Paint.Style.FILL);
        paint.setColor(Color.GREEN);
        rectF = new RectF(10, 10, 1070, 1070);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        canvas.drawArc(rectF, start, sweep, true, paint);
        sweep += SWEEP_INC;
        if (sweep > 360) {
            sweep -= 360;
            start += START_INC;
            if (start >= 360) start -= 360;
        }
        invalidate();
    }
}

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(new MyView(this));
    }
}

결과

 

 

5.

MainActivity.java

package kr.co.company.ch6;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;

import androidx.appcompat.app.AppCompatActivity;

import java.util.ArrayList;
import java.util.Arrays;

class MyView extends View {
    private ArrayList<Integer> list, temp;

    public MyView(Context context) {
        super(context);
        list = new ArrayList<>(Arrays.asList(R.drawable.a1, R.drawable.a2, R.drawable.a3, R.drawable.a4,
                R.drawable.a5, R.drawable.a6, R.drawable.a7, R.drawable.a8,
                R.drawable.a9, R.drawable.a10, R.drawable.a11, R.drawable.a12));
        temp = new ArrayList<>(5);

        int r;
        for (int i = 0; i < 5; i++) {
            r = (int) (Math.random() * list.size());
            temp.add(list.remove(r));
        }
    }

    @Override
    protected void onDraw(Canvas canvas) {
        Bitmap b, sb;
        int x, y, index = 0;
        for (int i = 0; i < 5; i++) {
            x = (int) (Math.random() * 900);
            y = (int) (Math.random() * 1300);
            b = BitmapFactory.decodeResource(getResources(), temp.get(index));
            sb = Bitmap.createScaledBitmap(b, 300, 300, false);
            canvas.drawBitmap(sb, x, y, null);
            list.add(temp.remove(index));
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            int r;
            for (int i = 0; i < 5; i++) {
                r = (int) (Math.random() * list.size());
                temp.add(list.remove(r));
            }
            invalidate();
        }
        return true;
    }
}

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(new MyView(this));
    }
}

결과

 

 

6.

MainActivity.java

package kr.co.company.ch6;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    public class MyView extends View {
        private int[] dice = {R.drawable.dice1, R.drawable.dice2, R.drawable.dice3,
                R.drawable.dice4, R.drawable.dice5, R.drawable.dice6};
        private int r;
        private Bitmap b, sb;

        public MyView(Context context) {
            super(context);
        }

        @Override
        protected void onDraw(Canvas canvas) {
            r = (int) (Math.random() * 6);
            b = BitmapFactory.decodeResource(getResources(), dice[r]);
            sb = Bitmap.createScaledBitmap(b, 430, 500, false);
            canvas.drawBitmap(sb, 320, 500, null);
        }

        @Override
        public boolean onTouchEvent(MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                invalidate();
            }
            return true;
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        MyView myView = new MyView(this);
        setContentView(myView);
    }
}

결과

 

 

7.

MainActivity.java

package kr.co.company.ch6;

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.ch6;

import android.content.Context;
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;

class Ball {
    public float x, y, yInc = 1;
    private int diameter;//눈의 크기
    public static int WIDTH = 1200, HEIGHT = 1650;

    public Ball(int d) {
        this.diameter = d;

        x = (int) (Math.random() * (WIDTH - d) + 3);
        y = 1;
        yInc = ((int) (Math.random() * 3 + 1)) * 0.6f;
    }

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

        paint.setColor(Color.WHITE);
        g.drawCircle(x, y, diameter, paint);
    }
}

public class MySurfaceView extends SurfaceView implements SurfaceHolder.Callback {
    public int max = 20;//눈 개수
    public int index = 0;//현재 존재하는 눈 개수
    public Ball basket[] = new Ball[max];
    private MyThread thread;
    private Bitmap b;

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

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

        b = BitmapFactory.decodeResource(getResources(), R.drawable.pic3);
        thread = new MyThread(holder, b);
    }

    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;
        private Bitmap b;
        int r;

        public MyThread(SurfaceHolder surfaceHolder, Bitmap b) {
            mSurfaceHolder = surfaceHolder;
            this.b = b;
        }

        @Override
        public void run() {
            while (mRun) {
                Canvas c = null;
                try {
                    c = mSurfaceHolder.lockCanvas(null);
                    c.drawBitmap(b, 0, 0, null);
                    r = (int) (Math.random() * 100);
                    if (r == 0 && max > index) {
                        r = (int) (Math.random() * 10 + 3);
                        basket[index++] = new Ball(r);
                    }
                    synchronized (mSurfaceHolder) {
                        for (int i = 0; i < index; i++) {
                            basket[i].paint(c);
                            if (basket[i].y > basket[i].HEIGHT) {
                                basket[i].y = 1;
                            }
                        }
                    }
                } finally {
                    if (c != null) {
                        mSurfaceHolder.unlockCanvasAndPost(c);
                    }
                }
            }
        }

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

결과

 

 

728x90

댓글