public class ImageTester extends Activity {
 private MyView vw;
 ArrayList<Vertex> arVertex;
 LinearLayout linear,linear1;
 private View d;
 public void onCreate(Bundle savedInstanceState) {                  
  super.onCreate(savedInstanceState);
  linear = new LinearLayout(this);
  linear.setOrientation(LinearLayout.VERTICAL                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       );
  linear1 = new LinearLayout(this);
  linear1.setOrientation(LinearLayout.HORIZONTAL);
  linear.addView(linear1);
  vw = new MyView(this);
  linear.addView(vw);
  setContentView(linear);
  arVertex = new ArrayList<Vertex>();
 }
 // 정점 하나에 대한 정보를 가지는 클래스
 public class Vertex {
  float x;
  float y;
  boolean Draw;
  int color;
  Vertex(float ax, float ay, boolean ad, int color) {
   System.out.println("x값"+ax+"y값"+ay+"드로우"+ad);
   x = ax;
   y = ay;
   Draw = ad;
   this.color=color;
  }
 }
 protected class MyView extends View  implements OnClickListener {
  Paint mPaint;
  boolean clear;
  int co;
  public MyView(Context context) {
   super(context);
   // Paint 객체 미리 초기화
   mPaint = new Paint();
   mPaint.setStrokeWidth(3); //두께 설정
   mPaint.setAntiAlias(true); //부드러운 표현
   clear = false;
   co=Color.BLACK;
  }
  public void onDraw(Canvas canvas) {
   canvas.drawColor(0xffe0e0e0);//배경 하얀색으로 덮어(도화지 배경을 하얗게 칠함)
   // 정점을 순회하면서 선분으로 잇는다.
   for (int i=0;i<arVertex.size();i++) {
    if (arVertex.get(i).Draw) {
     mPaint.setColor(arVertex.get(i).color);
     //
     canvas.drawLine(arVertex.get(i-1).x, arVertex.get(i-1).y, 
       arVertex.get(i).x, arVertex.get(i).y, mPaint);
    }
   }
  }
  // 터치 이동시마다 정점들을 추가한다.
  public boolean onTouchEvent(MotionEvent event) {
   if (event.getAction() == MotionEvent.ACTION_DOWN) {
    arVertex.add(new Vertex(event.getX(), event.getY(), false, co));
    return true;
   }
   if (event.getAction() == MotionEvent.ACTION_MOVE) {
    arVertex.add(new Vertex(event.getX(), event.getY(), true, co));
    invalidate();//화면에 그림을 그림 -> onDraw()실행함.
    return true;
   }return false;
  }
  // 이곳은 canvas를 다시 새하얀 도화지로 만드는 버튼 처리이다.
  // onDraw()작동 구조를 이해한다면
  // arVeertex를 clear() 시켜주고 다시 그리면
  // 그릴 점에 대한 정보가 없어지므로 새하얀 도화지가 된다.
  @Override
  public void onClick(View v) {
    arVertex.clear();
    invalidate();
  }
 }
}
 
 
위에 예제소스를 바꿔서

View를

View mView = (View)findViewById(R.id.test);

이런식으로 xml에 있는 뷰로 집어 넣고 싶은데 어떻게

어떻게 하나요?

MyView가 View 상속받았는데

View mView = (View)findViewById(R.id.test); 여기서 mView랑 위에 소스에서 MyView나 같은 Veiw 아닌가요?ㅠㅠ

초보자에가 너무 어렵네요 ㅠㅠ

개념좀 잡아주실분...ㅠㅠ

mView = MyView; 이렇게 할려고 하니까 안되는거 같네요

profile