마스터Q&A 안드로이드는 안드로이드 개발자들의 질문과 답변을 위한 지식 커뮤니티 사이트입니다. 안드로이드펍에서 운영하고 있습니다. [사용법, 운영진]

안드로이드 그림판 질문드려요~~

0 추천
메뉴에서 선색 하고 도형을 선택해야 그림그릴수있는 그림판을 만들었는데요..
 
하나그린다음 다른걸 선택하면 지워져서그런데.. 어떻게해야 계속 중복되게 그릴수 있을까요 ..? ㅠㅜ
 
 
package com.cookandroid.project9_1;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.SubMenu;
import android.view.View;
public class Project9_1Activity extends Activity {
final static int LINE = 1, CIRCLE = 2, FIVECIRCLE = 4, RECTANGLE = 3;
static int curShape = LINE;
static int curColor;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new MyGraphicView(this));
setTitle("간단 그림판");
}
 
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
super.onCreateOptionsMenu(menu);
menu.add(0, 1, 0, "선 그리기");
menu.add(0, 2, 0, "원 그리기");
menu.add(0, 3, 0, "사각형 그리기");
menu.add(0, 4, 0, "오륜기 그리기");
SubMenu Smenu = menu.addSubMenu("색상변경");
Smenu.add(0, 5, 0, "빨강");
Smenu.add(0, 6, 0, "파랑");
Smenu.add(0, 7, 0, "초록");

return true;
}  
 
 
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId()) {
case 1:
curShape = LINE; // 선
return true;
case 2:
curShape = CIRCLE; // 원
return true;
case 3:
curShape = RECTANGLE; // 사각형
return true;
case 4:
curShape = FIVECIRCLE; // 오륜기
return true;
case 5:
curColor = Color.RED;
return true;
case 6:
curColor = Color.BLUE;
return true;
case 7:
curColor = Color.GREEN;
return true;
}
return super.onOptionsItemSelected(item);
}
 
 
private static class MyGraphicView extends View {
int startX = -1, startY = -1, stopX = -1, stopY = -1;
public MyGraphicView(Context context) {
super(context);
}
 
 
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
// 화면을 누르기 시작했을 때할일
startX = (int) event.getX();
startY = (int) event.getY();
break;
case MotionEvent.ACTION_MOVE:
// 움질일 때 할일
case MotionEvent.ACTION_UP:
// 뗄때 할일
stopX = (int) event.getX();
stopY = (int) event.getY();
this.invalidate();
break;
case MotionEvent.ACTION_CANCEL:
//터치가 취소될 때 할일
break;
}
return true;
}
 
 
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setStrokeWidth(5);
paint.setStyle(Paint.Style.STROKE);
paint.setColor(curColor);
switch (curShape) {
case LINE:
canvas.drawLine(startX, startY, stopX, stopY, paint);
break;
case CIRCLE:
int radius = (int) Math.sqrt(Math.pow(stopX - startX, 2)
+ Math.pow(stopY - startY, 2));
canvas.drawCircle(startX, startY, radius, paint);

break;
case RECTANGLE:
canvas.drawRect(new Rect(startX, startY, stopX, stopY), paint);
break;
case FIVECIRCLE:
int radius2 = (int) Math.sqrt(Math.pow(stopX - startX, 2)
+ Math.pow(stopY - startY, 2));
canvas.drawCircle(startX, startY, radius2, paint);
canvas.drawCircle(startX+150, startY, radius2, paint);
canvas.drawCircle(startX+300, startY, radius2, paint);
canvas.drawCircle(startX+70, startY+100, radius2, paint);
canvas.drawCircle(startX+230, startY+100, radius2, paint);
break;
}
}
}
}
 
 
 

뭐가 잘못된건지를 모르겠습니다 ㅠㅠ

 

 

익명사용자 님이 2013년 12월 6일 질문

1개의 답변

0 추천
switch(curShape){

case 1:

case 2:

case 3:

default :

}

문법으로 그릴유형을 걸러내고 있네요.

변수에 그린 내용을 저장시켜놓고 타입구분없이 모두 그리도록 변경하시면 됩니다.
dev_아마 (9,750 포인트) 님이 2013년 12월 6일 답변
...