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

안드로이드 그림판 만드는데 질문점

0 추천
package com.example.chap9_6;
 
import java.util.ArrayList;
import java.util.List;
 
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 MainActivity extends Activity {
 
final static int LINE = 1, CIRCLE = 2, RECTANGLE = 3;
static int curShape = LINE;
static int curColor = Color.DKGRAY;
static List<MyShape> myshape = new ArrayList<MyShape>();
static List<Paint> mypaint = new ArrayList<Paint>();
 
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new MyGraphicView(this));
setTitle("연습문제 9-6");
}
 
@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, "사각형 그리기");
SubMenu sMenu = menu.addSubMenu("색상 변경 >>");
sMenu.add(0, 4, 0, "빨강");
sMenu.add(0, 5, 0, "초록");
sMenu.add(0, 6, 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:
curColor = Color.RED;
return true;
case 5:
curColor = Color.GREEN;
return true;
case 6:
curColor = Color.BLUE;
return true;
}
return super.onOptionsItemSelected(item);
}
 
private static class MyShape {
int shapeType; // 도형 종류
int startX, startY, stopX, stopY; // 도형의 2점
int color; // 도형 색상
}
 
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:
break;
case MotionEvent.ACTION_UP:
stopX = (int) event.getX();
stopY = (int) event.getY();
this.invalidate();
break;
}
 
return true;
}
 
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
 
MyShape ms = new MyShape();
 
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setStrokeWidth(5);
paint.setStyle(Paint.Style.STROKE);
paint.setColor(curColor);
 
ms.startX = startX;
ms.startY = startY;
ms.stopX = stopX;
ms.stopY = stopY;
ms.shapeType = curShape;
ms.color = curColor;
 
mypaint.add(paint);
 
myshape.add(ms);
 
for(int i=0; i<myshape.size(); i++) {
switch (myshape.get(i).shapeType) {
case LINE:
canvas.drawLine(myshape.get(i).startX, myshape.get(i).startY, myshape.get(i).stopX, myshape.get(i).stopY, mypaint.get(i));
break;
case CIRCLE:
int radius = (int) Math.sqrt(Math.pow(myshape.get(i).stopX - myshape.get(i).startX, 2)
+ Math.pow(myshape.get(i).stopY - myshape.get(i).startY, 2));
canvas.drawCircle(myshape.get(i).startX, myshape.get(i).startY, radius, mypaint.get(i));
break;
case RECTANGLE:
Rect rect = new Rect(myshape.get(i).startX, myshape.get(i).startY, myshape.get(i).stopX, myshape.get(i).stopY);
canvas.drawRect(rect, mypaint.get(i));
break;
}
}
}
}
}
 
 
이런 코드로 짜보았는데 드래그 할때마다 흔적이 보이게 하려고 합니다.
 
그래서 case MotionEvent.ACTION_MOVE: 의 break를 지우고 하면
 
이상하게 그림이 출력되어버립니다.
 
원래 마우스를 땠을 떄만 그림이 그려져야는데 움직이는 동안에도 그림이 그려져 버리네요
 
어떻게 해결할까요??
익명사용자 님이 2015년 5월 16일 질문

답변 달기

· 글에 소스 코드 보기 좋게 넣는 법
· 질문에 대해 추가적인 질문이나 의견이 있으면 답변이 아니라 댓글로 달아주시기 바랍니다.
표시할 이름 (옵션):
개인정보: 당신의 이메일은 이 알림을 보내는데만 사용됩니다.
스팸 차단 검사:
스팸 검사를 다시 받지 않으려면 로그인하거나 혹은 가입 하세요.
...