안드로이드 개발 질문/답변
(글 수 45,052)
public class One extends View{
private static final float RADIUS = 30;
private Paint backgroundPaint;
private Paint myPaint1;
private Paint myPaint2;
private Paint myPaint7;
private Paint myPaint8;
private Paint myPaint9;
public One(Context context, AttributeSet attrs){
super(context, attrs);
backgroundPaint = new Paint();
backgroundPaint.setColor(Color.YELLOW);
myPaint1 = new Paint();
myPaint2 = new Paint();
myPaint7 = new Paint();
myPaint8 = new Paint();
myPaint9 = new Paint();
private Paint backgroundPaint;
private Paint myPaint1;
private Paint myPaint2;
private Paint myPaint7;
private Paint myPaint8;
private Paint myPaint9;
public One(Context context, AttributeSet attrs){
super(context, attrs);
backgroundPaint = new Paint();
backgroundPaint.setColor(Color.YELLOW);
myPaint1 = new Paint();
myPaint2 = new Paint();
myPaint7 = new Paint();
myPaint8 = new Paint();
myPaint9 = new Paint();
myPaint1.setColor(Color.BLUE);
myPaint2.setColor(Color.BLUE);
myPaint7.setColor(Color.BLUE);
myPaint8.setColor(Color.BLUE);
myPaint9.setColor(Color.BLUE);
}
@Override
public boolean onTouchEvent(MotionEvent event){
int action = event.getAction();
switch(action){
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_MOVE:
public boolean onTouchEvent(MotionEvent event){
int action = event.getAction();
switch(action){
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_MOVE:
final float a = event.getX();
final float b = event.getY();
if(Math.sqrt(Math.pow(a - 160, 2) + Math.pow(b - 100, 2)) <= RADIUS){
myPaint1.setColor(Color.RED);
}
else if(Math.sqrt(Math.pow(a - 160, 2) + Math.pow(b - 140, 2)) <= RADIUS){
myPaint2.setColor(Color.RED);
}
final float b = event.getY();
if(Math.sqrt(Math.pow(a - 160, 2) + Math.pow(b - 100, 2)) <= RADIUS){
myPaint1.setColor(Color.RED);
}
else if(Math.sqrt(Math.pow(a - 160, 2) + Math.pow(b - 140, 2)) <= RADIUS){
myPaint2.setColor(Color.RED);
}
if(Math.sqrt(Math.pow(a - 100, 2) + Math.pow(b - 140, 2)) <= RADIUS){
myPaint7.setColor(Color.RED);
}
else if(Math.sqrt(Math.pow(a - 120, 2) + Math.pow(b - 160, 2)) <= RADIUS){
myPaint8.setColor(Color.RED);
}
else if(Math.sqrt(Math.pow(a - 140, 2) + Math.pow(b - 180, 2)) <= RADIUS){
myPaint9.setColor(Color.RED);
}
myPaint7.setColor(Color.RED);
}
else if(Math.sqrt(Math.pow(a - 120, 2) + Math.pow(b - 160, 2)) <= RADIUS){
myPaint8.setColor(Color.RED);
}
else if(Math.sqrt(Math.pow(a - 140, 2) + Math.pow(b - 180, 2)) <= RADIUS){
myPaint9.setColor(Color.RED);
}
return true;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL:
break;
}
return(true);
}
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL:
break;
}
return(true);
}
@Override
public void onDraw(Canvas canvas){
int width = canvas.getWidth();
int height = canvas.getHeight();
switch(){
Case Eng.A
canvas.drawRect(0, 0, width, height, backgroundPaint);
int[] start1 ={160,160};
int[] end1 = {100,120};
Paint[] paint1 = {backgroundPaint, myPaint1, myPaint2};
for (int i=0; i<start1.length; i++) {
canvas.drawCircle(start1[i],end1[i],RADIUS,paint1[i]);
}
break;
Case Eng.B
canvas.drawRect(0, 0, width, height, backgroundPaint);
Case Eng.A
canvas.drawRect(0, 0, width, height, backgroundPaint);
int[] start1 ={160,160};
int[] end1 = {100,120};
Paint[] paint1 = {backgroundPaint, myPaint1, myPaint2};
for (int i=0; i<start1.length; i++) {
canvas.drawCircle(start1[i],end1[i],RADIUS,paint1[i]);
}
break;
Case Eng.B
canvas.drawRect(0, 0, width, height, backgroundPaint);
int[] start2 = {100,120,140,};
int[] end2 = {140,160,180};
Paint[] paint2 = {backgroundPaint, myPaint7, myPaint8, myPaint9};
for (int a=0; a<start2.length; a++) {
canvas.drawCircle(start2[i],end2[i],RADIUS,paint2[i]);
}
break;
}
invalidate();
}
}
}
int[] end2 = {140,160,180};
Paint[] paint2 = {backgroundPaint, myPaint7, myPaint8, myPaint9};
for (int a=0; a<start2.length; a++) {
canvas.drawCircle(start2[i],end2[i],RADIUS,paint2[i]);
}
break;
}
invalidate();
}
}
}
myPaint로 원을 여러만들었고요, 터치이벤트를 줘서 원위로 드래그 하면서 지나가면 원의 색깔이 바뀌는 코드입니다.
여기서 myPaint1,2 와 7,8,9가 각각 그룹으로 묶여져서 화면에 나타나게 하려고 하고 싶은데요.
'public void onDraw(Canvas canvas)'부분에서 switch 문을 넣어서 해보려고 해요.
첫번째 터치하면 원 1,2가 나오고요. 원 1,2 위를 드래그 해서 색상이 모두 변하면 원 7,8,9가 나오도록 하고 싶어요.
switch문과 터치이벤트를 같이 써야할거 같은데 어떻게 해야할지 방향이 잡히지 않아 질문 드려요~