본문 바로가기

정보기술, IT/IT source

android 화면 touch시 이미지 생성하기.

public class MyView extends View {
 
 public static final int VIEW_W = 360;
 public static final int VIEW_H = 700;
 
 MyShape s;
 
 Bitmap bitmap  ;

 public MyView(Context context) {
  super(context);
  BitmapFactory.decodeResource
  (getResources(), R.drawable.ic_launcher);
  
 }

 @Override
 protected void onDraw(Canvas c) {//콜백. 시스템에서 부른다. 선언부에 on이 붙어 있는 것은 콜백시스템이다. 
  //BitmapFactory.decodeResource(res, id);  res에 있는 것 중 id에 해당하는 것만 가지고 온다.


  if(s!=null) s.display(c,bitmap);
 }


 @Override
 public boolean onTouchEvent(MotionEvent event) {//콜백. 시스템에서 부른다.
//  Log.i("touch","Touch:"+event.getAction()+","+event.getX()); Log 창에 나타내 준다.
  super.onTouchEvent(event);
  
  if(event.getAction()==MotionEvent.ACTION_DOWN){
   s = new MyShape((int)event.getX(), (int)event.getY());
   invalidate(); //repaint와 같은 효과. 무효화된 영역을 다시 칠해라,라는 뜻.
  }
  
  return true;
 }
  
// 간단히 10개를 그려줄 수 있는 다른 코딩. 
// protected void onDraw(Canvas canvas) {
//  for(int i =0;i<10;i++){
//   MyShape s = new MyShape();
//   s.display(canvas);
//  }
// }
 

}; 


(MyShape클래스는 따로 구성하여야 한다.)