ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [14] Canvas
    Develpment/Android Using Sample 2020. 9. 13. 21:37

    1. Canvas


     Activity

     

    1
    2
    3
    4
    5
    6
    7
    8
    public class MainActivity extends AppCompatActivity {
     
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(new MyView(MainActivity.this));
        }
    }
    cs

    * setContentView() 에서 새로 만든 클래스를 인자로 넣어준다.


     MyView

     

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    public class MyView extends View {
     
        public MyView(Context context) {
            super(context);
        }
     
        @Override
        protected void onDraw(Canvas canvas) {
            Paint paint = new Paint();
     
            paint.setColor(Color.BLUE);
     
            //캔버스에 사각형 그리기.
            Rect rt = new Rect();
            // top    right
            // left   bottom
            rt.set(100200300400); // left , top, right, bottom
            canvas.drawRect(rt, paint);
     
            //캔버스에 원 그리기.
            paint.setColor(Color.RED);
            canvas.drawCircle(200600100, paint);
     
            //테두리만 있는 색상의 원
            paint.setStyle(Paint.Style.STROKE);
            canvas.drawCircle(400600100, paint);
        }
    }
    cs

    * onDraw() 매서드에서 그림을 그려준다.



    2. 결과 화면


     




    3. Canvas2


     MyView

     

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    public class MyView extends View {
     
        Paint paint;
        Path path;
        int x,y;
     
        public MyView(Context context) {
            super(context);
     
            paint = new Paint();
            path = new Path();
        }
     
        @Override
        protected void onDraw(Canvas canvas) {
            paint.setColor(Color.RED);
            paint.setStrokeWidth( 3 );
            paint.setStyle(Paint.Style.STROKE);
     
            canvas.drawPath(path, paint);
        }
     
        @Override
        public boolean onTouchEvent(MotionEvent event) {
            x = (int)event.getX();
            y = (int)event.getY();
     
            switch (event.getAction())
            {
                case MotionEvent.ACTION_DOWN:
                    path.moveTo(x,y);
                    break;
                case MotionEvent.ACTION_MOVE:
                    x = (int)event.getX();
                    y = (int)event.getY();
                    path.lineTo(x,y);
                    break;
            }
     
            //onDraw() 재호출
            invalidate();
     
            return true;
        }
    }
    cs

    * onTourchEvent() 를 활용하여 터치로 그림그리기.

    * invalidate() 를 호출하면 onDraw()가 다시 호출되어 그림이 그려진다.



    4. 결과화면


     


    'Develpment > Android Using Sample' 카테고리의 다른 글

    [13] SharedPreferences  (0) 2020.09.13
    [12] 앱종료 확인 (Handler 응용)  (0) 2020.09.13
    [11] Handler  (0) 2020.09.13
    [10] Intent  (0) 2020.09.13
    [9] Rating Bar  (0) 2020.09.13

    댓글

Designed by Tistory.