mojo's Blog
안드로이드 프로그래밍 제 9장 연습문제 (5, 6번) 본문
5번 ) 확대, 축소, 회전, 밝게, 어둡게, 그레이 효과를 내도록 하는 프로그램 구현하기
Java Code
public class MainActivity extends AppCompatActivity {
Button btn1, btn2, btn3, btn4, btn5, btn6;
static float scaleX=1, scaleY=1, angle=0, color=1, sat=1;
MyGraphicView graphicView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
graphicView = new MyGraphicView(this);
setContentView(graphicView);
setTitle("연습하기");
}
public boolean onCreateOptionsMenu(Menu menu){
super.onCreateOptionsMenu(menu);
menu.add(0,1,0,"확대");
menu.add(0,2,0,"축소");
menu.add(0,3,0,"회전");
menu.add(0,4,0,"밝게");
menu.add(0,5,0,"어둡게");
menu.add(0,6,0,"그레이영상");
return true;
}
public boolean onOptionsItemSelected(MenuItem item){
switch(item.getItemId()){
case 1:
scaleX += 0.2f;
scaleY += 0.2f;
graphicView.invalidate();
return true;
case 2:
scaleX -= 0.2f;
scaleY -= 0.2f;
graphicView.invalidate();
return true;
case 3:
angle += 40;
graphicView.invalidate();
return true;
case 4:
color += 0.2f;
graphicView.invalidate();
return true;
case 5:
color -= 0.2f;
graphicView.invalidate();
return true;
case 6:
sat = (sat + 1) % 2;
graphicView.invalidate();
return true;
}
return super.onOptionsItemSelected(item);
}
private static class MyGraphicView extends View{
public MyGraphicView(Context context){
super(context);
}
protected void onDraw(Canvas canvas){
super.onDraw(canvas);
Bitmap picture = BitmapFactory.decodeResource(getResources(), R.drawable.lion);
Paint paint = new Paint();
int cenX = this.getWidth() / 2;
int cenY = this.getHeight() / 2;
canvas.scale(scaleX, scaleY, cenX, cenY);
canvas.rotate(angle,cenX,cenY);
float[] array = {color, 0, 0, 0, 0,
0, color, 0, 0, 0,
0, 0, color, 0, 0,
0, 0, 0, 1, 0};
ColorMatrix cm = new ColorMatrix(array);
if(sat == 0) cm.setSaturation(sat);
paint.setColorFilter(new ColorMatrixColorFilter(cm));
int picX = (this.getWidth() - picture.getWidth()) / 2;
int picY = (this.getHeight() - picture.getHeight()) / 2;
canvas.drawBitmap(picture, picX, picY, paint);
picture.recycle();
}
}
}
6번 ) 그린 도형이 계속 화면에 남아있도록 구현하기
Java Code
public class MainActivity extends AppCompatActivity {
final static int LINE = 1, CIRCLE = 2, RECTANGLE = 3;
final static int REDCOLOR = Color.RED, GREENCOLOR = Color.GREEN, BLUECOLOR = Color.BLUE;
static int curShape = LINE, curColor = REDCOLOR;
static int startX = -1, startY = -1, stopX = -1, stopY = -1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new MyGraphicView(this));
setTitle("연습하기");
}
public boolean onCreateOptionsMenu(Menu menu){
super.onCreateOptionsMenu(menu);
menu.add(0,1,0,"선 그리기");
menu.add(0,2,0,"원 그리기");
menu.add(0,3,0,"사각형 그리기");
SubMenu sMenu = menu.addSubMenu("색상 변경 >>");
sMenu.add(0,4,0,"빨강색");
sMenu.add(0,5,0,"초록색");
sMenu.add(0,6,0,"파랑색");
return true;
}
public boolean onOptionsItemSelected(MenuItem item){
switch(item.getItemId()){
case 1:
curShape = LINE;
startX = -1; startY = -1; stopX = -1; stopY = -1;
return true;
case 2:
curShape = CIRCLE;
startX = -1; startY = -1; stopX = -1; stopY = -1;
return true;
case 3:
curShape = RECTANGLE;
startX = -1; startY = -1; stopX = -1; stopY = -1;
return true;
case 4:
curColor = REDCOLOR;
startX = -1; startY = -1; stopX = -1; stopY = -1;
return true;
case 5:
curColor = GREENCOLOR;
startX = -1; startY = -1; stopX = -1; stopY = -1;
return true;
case 6:
curColor = BLUECOLOR;
startX = -1; startY = -1; stopX = -1; stopY = -1;
return true;
}
return super.onOptionsItemSelected(item);
}
private static class MyGraphicView extends View{
Vector<MyShape> v = new Vector<MyShape>();
public MyGraphicView(Context context){
super(context);
}
class MyShape{
int shapeType;
int startX, startY, endX, endY;
int color;
MyShape(int shapeType, int startX, int startY, int endX, int endY, int color){
this.shapeType = shapeType; this.startX = startX; this.startY = startY;
this.endX = endX; this.endY = endY; this.color = color;
}
}
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:
stopX = (int)event.getX();
stopY = (int)event.getY();
this.invalidate();
break;
case MotionEvent.ACTION_UP:
stopX = (int)event.getX();
stopY = (int)event.getY();
v.add(new MyShape(curShape, startX, startY, stopX, stopY, curColor));
this.invalidate();
break;
}
return true;
}
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(curColor);
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 RECTANGLE:
Rect rect = new Rect(startX,startY,stopX,stopY);
canvas.drawRect(rect, paint);
break;
}
for(int i=0; i<v.size(); i++){
paint.setColor(v.get(i).color);
switch(v.get(i).shapeType){
case LINE:
canvas.drawLine(v.get(i).startX, v.get(i).startY, v.get(i).endX, v.get(i).endY, paint);
break;
case CIRCLE:
int radius = (int)(Math.sqrt(Math.pow(v.get(i).endX-v.get(i).startX, 2)+Math.pow(v.get(i).endY-v.get(i).startY, 2)));
canvas.drawCircle(v.get(i).startX, v.get(i).startY, radius, paint);
break;
case RECTANGLE:
Rect rect = new Rect(v.get(i).startX, v.get(i).startY, v.get(i).endX, v.get(i).endY);
canvas.drawRect(rect, paint);
break;
}
}
}
}
}
'Android' 카테고리의 다른 글
액티비티와 인텐트 응용 (0) | 2021.09.04 |
---|---|
액티비티와 인텐트의 기본 (0) | 2021.09.04 |
Bitmap (0) | 2021.09.02 |
Graphic (0) | 2021.09.02 |
안드로이드 프로그래밍 제 8장 연습문제 6번 (0) | 2021.08.31 |
Comments