본문 바로가기

자바 웹 개발자가 될거야/JAVA

[Android] 선그리기, 원그리기

- 오른쪽 상단 메뉴바를 이용해서 선그리기, 원그리기 메뉴를 선택한다.

- 선택된 메뉴에 따라 switch case 문을 이용해서 받아들인다.

- X, Y 좌표값을 통해서 그림

 

 

① menu.xml

 

<?xml version="1.0" encoding="utf-8" ?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:title="선 그리기"
        android:id="@+id/line_draw"/>
    <item android:title="원 그리기"
        android:id="@+id/circle_draw"/>
</menu>

 

 

② MainActivity.java

 

package com.example.graphicex2;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;

public class MainActivity extends AppCompatActivity {
    public final static int LINE = 1;
    public final static int CIRCLE = 2;
    public static int curShape = LINE;

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

    private static class MyGraphicView extends View {
        float startX, startY,stopX,stopY;

        MyGraphicView(Context c){
            super(c);
        }

        @Override
        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);
            Paint p = new Paint();
            p.setAntiAlias(true);
            p.setColor(Color.RED);
            p.setStrokeWidth(5);
            p.setStyle(Paint.Style.STROKE);

            switch (curShape){
                case LINE:
                    canvas.drawLine(startX,startY,stopX,stopY,p);
                    break;
                case CIRCLE:
                    int radius = (int)Math.sqrt(Math.pow(stopX-stopY,2)+
                            Math.pow(stopY-startY,2));
                    canvas.drawCircle(startX,startY,radius,p);
                    break;
            }
        }

        @Override
        public boolean onTouchEvent(MotionEvent event) {
            super.onTouchEvent(event);
            switch(event.getAction()){
                case MotionEvent.ACTION_DOWN:
                    startX = event.getX();
                    startY = event.getY();
                    break;
                case MotionEvent.ACTION_MOVE:
                case MotionEvent.ACTION_UP:
                    stopX = event.getX();
                    stopY = event.getY();
                    break;
            }

            return true;
        }
    }



    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu1,menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        super.onOptionsItemSelected(item);
        switch (item.getItemId()){
            case R.id.line_draw:
                curShape = LINE;
                break;
            case R.id.circle_draw:
                curShape = CIRCLE;
                break;
        }
        return true;
    }
}

 

- ACTION_MOVE에 break문이 없는 이유는 마우스가 움직일 때마다 선 그리는 걸 보여주기 위함