티스토리 뷰
package com.example.testapp;
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.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
public class MainActivity extends Activity {
final static int LINE =1, CIRCLE=2, Rac=3;
static int curShape=LINE;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new MyView(this));
setTitle("break");
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
super.onCreateOptionsMenu(menu);
menu.add(0,1,0,"선 그리기");
menu.add(0,2,0,"원 그리기");
menu.add(0,3,0,"사각형 그리기");
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item){
switch(item.getItemId()) {
case 1:
curShape = LINE;
return true;
case 2:
curShape = CIRCLE;
return true;
case 3:
curShape = Rac;
}
return super.onOptionsItemSelected(item);
}
private static class MyView extends View{
int startX=-1, startY=-1, stopX=-1, stopY=-1;
public MyView(Context context){
super(context);
}
@Override
public boolean onTouchEvent(MotionEvent event){
switch (event.getAction()){
case MotionEvent.ACTION_DOWN:
startX=(int)event.getX();
startY=(int)event.getY();
break;
case MotionEvent.ACTION_MOVE:
case MotionEvent.ACTION_UP:
stopX=(int)event.getX();
stopY=(int)event.getY();
this.invalidate();
break;
}
return true;
}
@Override
protected void onDraw(Canvas canvas){
super.onDraw(canvas);
Paint paint=new Paint();
paint.setAntiAlias(true);
paint.setStrokeWidth(5);
paint.setStyle(Paint.Style.STROKE);
paint.setColor(Color.RED);
switch (curShape){
case LINE:
canvas.drawLine(startX,startY,stopX,stopY,paint);
break;
case CIRCLE:
int radius = (int) Math.sqrt(Math.pow(stopX - startX, 2)
+ Math.pow(stopY - startY,2));
canvas.drawCircle(startX, startY, radius, paint);
break;
case Rac:
canvas.drawRect(startX, startY, stopX, stopY, paint);
}
}
}
}
'공부 > android' 카테고리의 다른 글
안드로이드 기기간 소켓 통신하기 - 기타 코드 (1) | 2013.08.16 |
---|---|
안드로이드 기기간 소켓 통신하기 - 클라이언트 코드 (2) | 2013.08.16 |
안드로이드 기기간 소켓 통신하기 - 서버 코드 (1) | 2013.08.16 |
안드로이드 기기간 소켓 통신하기 - 양방향 (16) | 2013.08.16 |