Hello, ๋๋'s world !
onDraw() ๋ฅผ ํ์ฉํ์ฌ ๊ทธ๋ฆผ๊ทธ๋ฆฌ๊ธฐ ๋ณธ๋ฌธ
๊พธ์คํ ์ ํ์ธ ์ฌ๋ฆฌํ ์คํธ๋ค ์ค์์ ๊ทธ๋ฆผ๊ทธ๋ฆฌ๋ ์ฌ๋ฆฌํ ์คํธ๋ฅผ ๋ณด๊ณ ๋ ์ฌ๋ผ ๊ทธ๋ฆผ๊ทธ๋ฆฌ๊ธฐ ์ฌ๋ฆฌํ ์คํธ ์ฑ์ ๋ง๋ค์ด๋ณด์๋ค.
์ฌ๋ฆฌํ ์คํธ ๋ง๊ณ ๋ ์ค์ ์ํ์์ ์นด๋๋ฅผ ๋ฐํํ ๋ ์ฌ์ธ์ ๋ง๋ค๊ฑฐ๋, ํด๋ํฐ์ผ๋ก ์๋ช ํ ๋ ์ด ๋ฉ์๋๊ฐ ์ฐ์ธ๋ค๊ณ ๋ณด๋ฉด ๋๋ค.
์๋๋ก์ด๋์์ ๊ทธ๋ฆผ๊ทธ๋ฆฌ๊ธฐ๋ฅผ ๊ตฌํํ๋ ค๋ฉด ๋ช๊ฐ์ง ํ์ํ ๋ฉ์๋๋ค์ด ์๋ค.
1. onTouchEvent()
- ํฐ์นํ๊ณณ์ ์ขํ๊ฐ์ ์ด์ฉํ์ฌ ๊ทธ๋ฆฌ๊ธฐ ์ถ๋ ฅ
- ๋๋ ์๋, ๋๋ฅด๋ฉด์ ์ด๋ํ ๋, ๋ผ์์๋๋ก ๋๋์ด์ง
2. onDraw()
- ๊ทธ๋ฆผ๊ทธ๋ฆฌ๊ธฐ๋ฅผ ์ฒ๋ฆฌํด์ฃผ๋ ๋ฉ์๋.
- Canvas์(๋ํ์ง ์ญํ/๊ทธ๋ฆฌ๋ ๋ด์ฉ) Paint(์,๊ฐ๋,๊ธ๊ผด/๊ทธ๋ฆฌ๋ ๋ฐฉ๋ฒ)๊ฐ ์๋ค.
1.activity_draw.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent">
<com.example.drawingpsychologicaltest.PaintView
android:id="@+id/paintview"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="230dp"
android:background="@color/colorPink"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"/>
<ImageView
android:id="@+id/skechbook_iv"
android:layout_width="wrap_content"
android:layout_height="220dp"
android:background="@drawable/skechbook_pencil"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"/>
<TextView
android:layout_width="230dp"
android:layout_height="wrap_content"
android:layout_alignLeft="@id/skechbook_iv"
android:layout_marginTop="60dp"
android:layout_marginLeft="70dp"
style="@style/explanation_tv"
android:text="์ฒ์์ ๊ฑท๊ณ ์๋ ๋น์ . ๊ทธ๊ณณ์์ ์ง์ ๋ฐ๊ฒฌํ์ต๋๋ค.
๊ทธ๊ณณ์์ ๋ณธ ์ง์ ๊ทธ๋ ค์ฃผ์ธ์."
/>
<ImageButton
android:id="@+id/eraser_img_btn"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignTop="@+id/skechbook_iv"
android:background="@drawable/eraser_icon"
android:layout_alignParentRight="true"
android:layout_marginTop="235dp"
android:layout_marginRight="10dp"
/>
<ImageView
android:id="@+id/bottom_iv"
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="@color/colorPink"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
/>
<Button
android:id="@+id/done_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/main_btn_customise"
android:layout_alignTop="@id/bottom_iv"
android:layout_marginTop="15dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
style="@style/done_tv"
android:text="@string/done_name"
/>
</RelativeLayout>
2. FingerPath.java
public class FingerPath {
public int color;
public int strokeWideth;
public Path path;
public FingerPath(int color, int strokeWideth, Path path) {
this.color = color;
this.strokeWideth = strokeWideth;
this.path = path;
}
}
3. PaintView.java
public class PaintView extends View {
public static int BRUSH_SIZE = 10;
public static final int DEFAULT_COLOR = Color.BLACK;
public static final int DEFAULT_BG_COLOR = Color.WHITE;
private static final float TOUCH_TOLERANCE = 4;
private float mX, mY;
private Path mPath;
private Paint mPaint;
private ArrayList<FingerPath> paths = new ArrayList<>();
private int currentColor;
private int backgroundColor = DEFAULT_BG_COLOR;
private int strokeWidth;
private Bitmap mBitmap;
private Canvas mCanvas;
private Paint mBitmapPaint = new Paint(Paint.DITHER_FLAG);
public PaintView(Context context) {
this(context, null);
}
public PaintView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setDither(true);
mPaint.setColor(DEFAULT_COLOR);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeJoin(Paint.Join.ROUND);
mPaint.setStrokeCap(Paint.Cap.ROUND);
mPaint.setXfermode(null);
mPaint.setAlpha(0xff); //255
}
public void init(DisplayMetrics metrics){
int heigh = metrics.heightPixels;
int width = metrics.widthPixels;
mBitmap = Bitmap.createBitmap(width, heigh, Bitmap.Config.ARGB_8888);
mCanvas = new Canvas(mBitmap);
currentColor = DEFAULT_COLOR;
strokeWidth = BRUSH_SIZE;
}
public void normal(){
}
public void clear(){
backgroundColor = DEFAULT_BG_COLOR;
paths.clear();
normal();
invalidate();
}
protected void onDraw(Canvas canvas){
canvas.save();
mCanvas.drawColor(backgroundColor);
for(FingerPath fp : paths){
mPaint.setColor(fp.color);
mPaint.setStrokeWidth(fp.strokeWideth);
mPaint.setMaskFilter(null);
mCanvas.drawPath(fp.path, mPaint);
}
canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);
canvas.restore();
}
private void touchStart(float x, float y){
mPath = new Path();
FingerPath fp = new FingerPath(currentColor, strokeWidth, mPath);
paths.add(fp);
mPath.reset();
mPath.moveTo(x, y);
mX = x;
mY = y;
}
private void touchMove(float x, float y){
float dx = Math.abs(x - mX);
float dy = Math.abs(y - mY);
if(dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE){
mPath.quadTo(mX, mY, (x + mX) / 2, (y + mY) / 2);
mX = x;
mY = y;
}
}
private void touchUp(){
mPath.lineTo(mX, mY);
}
public boolean onTouchEvent(MotionEvent event) {
float x = event.getX();
float y = event.getY();
switch (event.getAction()){
case MotionEvent.ACTION_DOWN:
touchStart(x, y);
invalidate();
break;
case MotionEvent.ACTION_MOVE:
touchMove(x, y);
invalidate(); //ํฐ์น ์ด๋์ค์๋ ๊ทธ๋ฆผ ์ถ๋ ฅ
break;
case MotionEvent.ACTION_UP:
touchUp();
invalidate();
break;
}
return true;
}
}
๋ทฐ๋ฅผ ๋ค์ ๊ทธ๋ฆฌ๋ ๊ฒฝ์ฐ๊ฐ ๋งค์ฐ ์์ฃผ ๋ฐ์ํ๋ฉฐ, ๋ค์์ ๊ทธ๋ฆฌ๊ธฐ ๊ฐ์ฒด๋ ๋ง์ ๋น์ฉ์ ๋ค์ฌ ์ด๊ธฐํํด์ผ ํ๋ค.
onDraw() ๋ฉ์๋ ๋ด์์ ๊ทธ๋ฆฌ๊ธฐ ๊ฐ์ฒด๋ฅผ ๋ง๋ค๋ฉด ์ฑ๋ฅ์ด ํฌ๊ฒ ์ ํ๋๊ณ UI๊ฐ ๋๋ฆฌ๊ฒ ํ์๋ ์ ์๋ค.
4.MainActivity
public class MainActivity extends AppCompatActivity {
public PaintView paintView;
private Button mDoneBtn;
private ImageButton mClearBtn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.avtivity_draw);
//์์ฑํ๋ฉด์ผ๋ก, ๊ทธ๋ฆผ๊ฒฐ๊ณผ ๋๊ฒจ์ฃผ๊ธฐ
mDoneBtn = (Button)findViewById(R.id.done_btn);
mDoneBtn.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
Intent intent = new Intent(
getApplicationContext(),
ResultActivity4.class);
startActivity(intent);
}
});
//์ง์ฐ๊ฐ ๋ฒํผ
mClearBtn = (ImageButton)findViewById(R.id.eraser_img_btn);
mClearBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
paintView.clear();
}
});
paintView = (PaintView)findViewById(R.id.paintview);
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getRealMetrics(metrics);
paintView.init(metrics);
}
}
๊ทธ๋ฆฌ๊ธฐ ์ฌ๋ฆฌํ ์คํธ๋ ์ฌ๋ฐ์ง๋ง, ๋ค์์๋ ํ์ด๋ธ๋ฆฌ๋๋ก MBTI๊ฐ์ ์ฌ๋ฆฌํ ์คํธ๋ ๋ง๋ค์ด ๋ณด๊ณ ์ถ๋ค ~.~
<๊ฐ๋ฐํ๊ฒฝ>
java version "1.8.0_271"
android API 10.0.0+ (R)
android studio "4.0.1"
<์ฐธ๊ณ >
developer.android.com/training/custom-views/custom-drawing?hl=ko
Do it! ์๋๋ก์ด๋ ์ฑ ํ๋ก๊ทธ๋๋ฐ(์ ์ฌ๊ณค ์ง์)
'๐ Android' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
Android Waveํจ๊ณผ ๋ฃ๊ธฐ WaveView (0) | 2021.01.20 |
---|---|
Fragment์ RecyclerView ์์ Fragment๋ก ๋ฐ์ดํฐ ์ ๋ฌํ๊ธฐ (0) | 2021.01.20 |
dialogFragment ์์ Fragment ๋ก ๋ฐ์ดํฐ ์ ๋ฌ (0) | 2021.01.18 |
RecyclerView (0) | 2021.01.08 |
DialogFragment (0) | 2021.01.07 |