저가 빨간줄 쳐논부분을..좀 해결하고 싶은데요..
저가 canvas사용하려고 클래스를 vw로 하나 더 만들었어요 근데 버튼과 vw클래스를 겹치게 하는법을 잘 모르겠어요...
그리고 저가아직 익숙하지도 않고 많이 미숙해서 버튼을 아래쪽 빨간줄 친 분분과 연결해서
btn1을 누르면 캔버스에 까만색볼펜으로 그림을 그릴수 있고
btn2를 누르면 캔버스에 빨간색볼펜으로 그림을 그릴수 있게 하려고 하는데 이 버튼을 연결하는 법좀 알려주시면 안되나요....
혼자서 해보려고 계속하다가 ...안되서 이렇게 질문 올려요...부탁드립니다..
package exam.draw1;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
public class Draw1Activity extends Activity {
private MyView vw;
ArrayList<Vertex> arVertex;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
vw = new MyView(this);
Button btn1 = new Button(this);
btn1.setText("BLACK");
Button btn2 = new Button(this);
btn2.setText("RED");
setContentView(vw);
arVertex = new ArrayList<Vertex>();
}
// 정점 하나에 대한 정보를 가지는 클래스
public class Vertex {
Vertex(float ax, float ay, boolean ad) {
x = ax;
y = ay;
Draw = ad;
}
float x;
float y;
boolean Draw;
}
protected class MyView extends View {
public MyView(Context context) {
super(context);
// Paint 객체 미리 초기화
}
public void onDraw(Canvas canvas) {
canvas.drawColor(Color.WHITE);
Paint mPaint1 = new Paint();
mPaint1 = new Paint();
mPaint1.setColor(Color.BLACK);
mPaint1.setStrokeWidth(3);
mPaint1.setAntiAlias(true);
Paint mPaint2 = new Paint();
mPaint2 = new Paint();
mPaint2.setColor(Color.RED);
mPaint2.setStrokeWidth(3);
mPaint2.setAntiAlias(true);
// 정점을 순회하면서 선분으로 잇는다.
for (int i=0;i<arVertex.size();i++) {
if (arVertex.get(i).Draw) {
canvas.drawLine(arVertex.get(i-1).x, arVertex.get(i-1).y,
arVertex.get(i).x, arVertex.get(i).y, mPaint1);
}
else if (arVertex.get(i).Draw) {
canvas.drawLine(arVertex.get(i-1).x, arVertex.get(i-1).y,
arVertex.get(i).x, arVertex.get(i).y, mPaint2);
}
}
}
// 터치 이동시마다 정점들을 추가한다.
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
arVertex.add(new Vertex(event.getX(), event.getY(), false));
return true;
}
if (event.getAction() == MotionEvent.ACTION_MOVE) {
arVertex.add(new Vertex(event.getX(), event.getY(), true));
invalidate();
return true;
}
return false;
}
}
}
동그란 버튼 책에서 아직 못본거 같은데....
<RaidButton <이런식으로 사용하는건가요...?
그리구...
// 정점을 순회하면서 선분으로 잇는다. for (int i=0;i<arVertex.size();i++) { if (arVertex.get(i).Draw) { canvas.drawLine(arVertex.get(i-1).x, arVertex.get(i-1).y, arVertex.get(i).x, arVertex.get(i).y, mPaint1); } else if (arVertex.get(i).Draw) { canvas.drawLine(arVertex.get(i-1).x, arVertex.get(i-1).y, arVertex.get(i).x, arVertex.get(i).y, mPaint2); } } }
Button btn1 = (Button) findViewById(R.id.black);
btn1.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
[ ]
}
});
님이 말씀하신대로 버튼을 저렇게 지정해 놓고 [ ] 이부분을 어떡해 처리해야하나요??
클릭을 하면 위에 검은색으로해논 정보를 갖고와야하고 그러는데....
if(black == true)
{
canvas.drawLine(arVertex.get(i-1).x, arVertex.get(i-1).y,
arVertex.get(i).x, arVertex.get(i).y, mPaint1);
}
else
{
canvas.drawLine(arVertex.get(i-1).x, arVertex.get(i-1).y,
arVertex.get(i).x, arVertex.get(i).y, mPaint2);
}
이렇게 구문 짜야될듯한데요?
한번 테스트 해보세요
그리고 디버그 모드서 중단점 걸고 한단계씩 실행해서 조건문이 제대로 먹히는지 확인도 해보세용




라디오버튼 이용하시는게 좋을꺼 같기도 한데 무튼 버튼이 눌렸을때의 플레그값을 넘겨서
// 정점을 순회하면서 선분으로 잇는다.
for (int i=0;i<arVertex.size();i++) {
if (arVertex.get(i).Draw) {
canvas.drawLine(arVertex.get(i-1).x, arVertex.get(i-1).y,
arVertex.get(i).x, arVertex.get(i).y, mPaint1);
}
else if (arVertex.get(i).Draw) {
canvas.drawLine(arVertex.get(i-1).x, arVertex.get(i-1).y,
arVertex.get(i).x, arVertex.get(i).y, mPaint2);
}
}
}
요 부분에다가 if문 걸어서 플레그값에 따라서 그려질 paint속성을 결정해주면 될듯합니당~