Android Studio 10일차
2021. 11. 12. 17:43ㆍAndroid Development
간단 그림판 ver2.
더보기
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
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;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
//static : 공유,객체 생성 이전에 메모리 할당.
final int LINE = 1, CIRCLE = 2;
// 상수
int curShape = LINE;// 현재 도형
List<MyShape> myshape = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.activity_main);
setContentView(new MyGraphicView(this));
setTitle("간단 그림판");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
menu.add(0, 1, 0, "선 그리기");
menu.add(0, 2, 0, "원 그리기");
return true;
}
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case 1:
curShape = LINE; // 선
return true;
case 2:
curShape = CIRCLE; // 원
return true;
}
return super.onOptionsItemSelected(item);
}
private class MyGraphicView extends View {
int startX = -1, startY = -1, stopX = -1, stopY = -1;
// 좌표상 없는 값을 주고, 드래그시 실제 값으로 대체
public MyGraphicView(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();
MyShape shape = new MyShape();
shape.ShapeType = curShape;
shape.startX = startX;
shape.startY = startY;
shape.stopX = stopX;
shape.stopY = stopY;
myshape.add(shape);
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);
for (int i = 0; i < myshape.size(); i++) {
MyShape shape = myshape.get(i);
switch (shape.ShapeType) {
case LINE:
canvas.drawLine(shape.startX, shape.startY, shape.stopX, shape.stopY, paint);
break;
case CIRCLE:
// Constant expression required
int radius = (int) Math.sqrt(Math.pow(shape.stopX - shape.startX, 2)
+ Math.pow(shape.stopY - shape.startY, 2));
canvas.drawCircle(shape.startX, shape.startY, radius, paint);
break;
}
}
}
}
}
MainActivity
public class MyShape {
int ShapeType;
int startX, startY, stopX, stopY;
}
MyShape 클래스 생성해서 그림들을 배열에 담아
터치 이벤트마다 뿌려줌.
이미지의 기하학적 변화주기
더보기
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
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;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.activity_main);
setContentView(new MyGraphicView(this));
}
private class MyGraphicView extends View {
public MyGraphicView(Context context) {
super(context);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Bitmap picture = BitmapFactory.decodeResource(getResources(), R.drawable.testimage);
int cenX = this.getWidth() / 2;
int cenY = this.getHeight() / 2;
int picX = (this.getWidth() - picture.getWidth()) / 2;
int picY = (this.getHeight() - picture.getHeight()) / 2;
// canvas.rotate(45, cenX, cenY);
// canvas.drawBitmap(picture, picX, picY, null);//회전
// canvas.translate(-150, 200);
// canvas.drawBitmap(picture, picX, picY, null);//위치 이동
// canvas.scale(2, 2, cenX, cenY);
// canvas.drawBitmap(picture, picX, picY, null);//확대
canvas.skew(0.3f, 0.3f);
canvas.drawBitmap(picture, cenX, cenY, null);//비틀기
picture.recycle();
}
}
}
블러링 효과주기
더보기
package com.example.test24;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BlurMaskFilter;
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;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.activity_main);
setContentView(new MyGraphicView(this));
}
private class MyGraphicView extends View {
public MyGraphicView(Context context) {
super(context);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Bitmap picture = BitmapFactory.decodeResource(getResources(), R.drawable.lena256);
int picX = (this.getWidth() - picture.getWidth()) / 2;
int picY = (this.getHeight() - picture.getHeight()) / 2;
Paint paint = new Paint();
BlurMaskFilter bMask;
bMask = new BlurMaskFilter(30, BlurMaskFilter.Blur.INNER);
// BlurMaskFilter.Blur.NORMAL //테두리 흐릿
// BlurMaskFilter.Blur.INNER // 입체 액자처럼 안쪽으로만 입체감 표시
// BlurMaskFilter.Blur.OUTER // ' ' 바깥쪽으로만 입체감 표시
// BlurMaskFilter.Blur.SOLID //
paint.setMaskFilter(bMask); // 테두리 선을 강조하면서 블러 효과
canvas.drawBitmap(picture, picX, picY, paint);
picture.recycle();
}
}
}
설정 안될시 AndroidMenifest 에서 하드웨어 가속 false로 설정
엠보싱 효과 주기
더보기
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BlurMaskFilter;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.EmbossMaskFilter;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.activity_main);
setContentView(new MyGraphicView(this));
}
private class MyGraphicView extends View {
public MyGraphicView(Context context) {
super(context);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
int cenX = this.getWidth() / 2;
// int cenY = this.getHeight() / 2;
int cenY=200;
Paint paint = new Paint();
paint.setColor(Color.GRAY);
EmbossMaskFilter eMask;
eMask = new EmbossMaskFilter(new float[] { 3, 3, 3 }
, 0.5f, 5, 10);
// EmbossMaskFilter(xyz 빛의 방향, 빛의 밝기, 반사 계수, 블러링 크기);
paint.setMaskFilter(eMask);
canvas.drawCircle(cenX, cenY, 150, paint);
eMask = new EmbossMaskFilter(new float[] { 3, 3, 3 }
, 0.5f, 5, 10);
// EmbossMaskFilter(xyz 빛의 방향, 빛의 밝기, 반사 계수, 블러링 크기);
paint.setMaskFilter(eMask);
canvas.drawCircle(cenX, cenY+400, 150, paint);
eMask = new EmbossMaskFilter(new float[] { 3, 3, 3 }
, 0.5f, 5, 10);
// EmbossMaskFilter(xyz 빛의 방향, 빛의 밝기, 반사 계수, 블러링 크기);
paint.setMaskFilter(eMask);
canvas.drawCircle(cenX, cenY+800, 150, paint);
eMask = new EmbossMaskFilter(new float[] { 3, 3, 3 }
, 0.5f, 5, 10);
// EmbossMaskFilter(xyz 빛의 방향, 빛의 밝기, 반사 계수, 블러링 크기);
paint.setMaskFilter(eMask);
canvas.drawCircle(cenX, cenY+1200, 150, paint);
}
}
}
미니 포토샵 만들기.
더보기
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BlurMaskFilter;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.EmbossMaskFilter;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
ImageButton ibZoomin, ibZoomout, ibRotate, ibBlur, ibEmboss;
MyGraphicView graphicView;
float scaleX = 1, scaleY = 1, angle = 0;
boolean blur = false, embos = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setTitle("미니 포토샵 5555");
LinearLayout pictureLayout = findViewById(R.id.pictureLayout);
graphicView = new MyGraphicView(this);
pictureLayout.addView(graphicView);
ibZoomin = findViewById(R.id.ibZoomIn);
ibZoomout = findViewById(R.id.ibZoomOut);
ibRotate = findViewById(R.id.ibRotate);
ibBlur = findViewById(R.id.ibBlur);
ibEmboss = findViewById(R.id.ibEmbos);
ibZoomin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
scaleX = scaleX + 0.2f;
scaleY = scaleY + 0.2f;
graphicView.invalidate();
}
});
ibZoomout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
scaleX = scaleX - 0.2f;
scaleY = scaleY - 0.2f;
graphicView.invalidate();
}
});
ibRotate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
angle = angle + 20;
graphicView.invalidate();
}
});
ibBlur.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (blur == true)
blur = false;
else
blur = true;
graphicView.invalidate();
}
});
ibEmboss.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (embos == true)
embos = false;
else
embos = true;
graphicView.invalidate();
}
});
}
private class MyGraphicView extends View {
public MyGraphicView(Context context) {
super(context);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
int cenX = this.getWidth() / 2;
int cenY = this.getHeight() / 2;
canvas.scale(scaleX, scaleY, cenX, cenY);
canvas.rotate(angle, cenX, cenY);
Paint paint = new Paint();
if (blur == true) {
BlurMaskFilter bMask;
bMask = new BlurMaskFilter(50, BlurMaskFilter.Blur.NORMAL);
paint.setMaskFilter(bMask);
}
if (embos == true) {
EmbossMaskFilter eMask;
eMask = new EmbossMaskFilter(new float[]{1, 5, 1}
, 0.5f, 5, 50);
paint.setMaskFilter(eMask);
}
Bitmap picture = BitmapFactory.decodeResource(getResources(),
R.drawable.lena256);
int picX = (this.getWidth() - picture.getWidth()) / 2;
int picY = (this.getHeight() - picture.getHeight()) / 2;
canvas.drawBitmap(picture, picX, picY, paint);
picture.recycle();
}
}
}
액티비티를 사용한 선호도 투표.
더보기
내일 계속
'Android Development' 카테고리의 다른 글
Android Studio 12일차 (0) | 2021.11.16 |
---|---|
Android Studio 11일차 (0) | 2021.11.15 |
Android Studio 9일차 (0) | 2021.11.11 |
Android Studio 7일차 (0) | 2021.11.09 |
Android Studio 6일차 (0) | 2021.11.08 |