여기에서 그리기 까지는 잘 됩니다.
문제는 검은색 선 그린 후 메뉴로 색 선택해서 빨간색으로 고른 다음 터치 후 검은색 선이 빨간색 선으로 바뀝니다. 또 노란색도 화면에 터치하면 빨간색이 노란색 선으로 변하면서 그려집니다 ㅠㅠ
어떻게하면 따로따로 할 수 있나요??
public class FreeLine extends Activity {
MyPaint sv;
static int curColor = Color.DKGRAY;
class Point {
float x;
float y;
boolean isDraw;
public Point(float x, float y, boolean isDraw) {
this.x = x;
this.y = y;
this.isDraw = isDraw;
}
}
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.save_reply, menu);
SubMenu sMenu = menu.addSubMenu("색 선택>>");
sMenu.add(0, 1, 0, "검정색");
sMenu.add(0, 2, 0, "빨간색");
sMenu.add(0, 3, 0, "파란색");
sMenu.add(0, 4, 0, "노란색");
sMenu.add(0, 5, 0, "초록색");
menu.add(0, 100, 0, "지우개");
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
Paint p = new Paint();
if (id == R.id.action_reply) {
exit();
}
if (id == R.id.action_save) {
record();
}
if (id == 1) {
curColor = Color.BLACK;
return true;
}
if (id == 2) {
curColor = Color.RED;
return true;
}
if (id == 3) {
curColor = Color.BLUE;
return true;
}
if (id == 4) {
curColor = Color.YELLOW;
return true;
}
if (id == 5) {
curColor = Color.GREEN;
return true;
}
if (id == 100) {
curColor = Color.WHITE;
return true;
}
return super.onOptionsItemSelected(item);
}
class MyPaint extends View {
private Canvas canvas;
public MyPaint(Context context) {
super(context);
setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
this.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch( event.getAction() ) {
case MotionEvent.ACTION_MOVE:
points.add(new Point(event.getX(), event.getY(), true));
invalidate();
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_DOWN:
points.add(new Point(event.getX(), event.getY(), false));
}
return true;
}
});
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
this.canvas = canvas;
Paint p = null;
p = new Paint();
p.setAntiAlias(true);
p.setStrokeWidth(3);
p.setStyle(Paint.Style.STROKE);
p.setColor(curColor);
for(int i=1; i<points.size(); i++) {
if(!points.get(i).isDraw) continue;
canvas.drawLine(points.get(i-1).x, points.get(i-1).y, points.get(i).x, points.get(i).y, p);
}
}
}
ArrayList<Point> points = new ArrayList<Point>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActionBar actionBar = getActionBar();
actionBar.setBackgroundDrawable(new ColorDrawable(0xFF62a1ff));
//setContentView(R.layout.freeline);
sv = new MyPaint(this);
setContentView(sv);
}