Hello, ๋‚˜๋‚˜'s world !

onDraw() ๋ฅผ ํ™œ์šฉํ•˜์—ฌ ๊ทธ๋ฆผ๊ทธ๋ฆฌ๊ธฐ ๋ณธ๋ฌธ

๐Ÿ’š Android

onDraw() ๋ฅผ ํ™œ์šฉํ•˜์—ฌ ๊ทธ๋ฆผ๊ทธ๋ฆฌ๊ธฐ

Nana0 2021. 1. 8. 16:28

 

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! ์•ˆ๋“œ๋กœ์ด๋“œ ์•ฑ ํ”„๋กœ๊ทธ๋ž˜๋ฐ(์ •์žฌ๊ณค ์ง€์Œ)

Comments