관리 메뉴

moozi

안드로이드 그래픽 터치이벤트 본문

안드로이드개발강좌

안드로이드 그래픽 터치이벤트

moozi 2015. 11. 25. 10:06

package com.naver.combo2b_28;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Rect;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(new MyGraphic(this));
    }

    //inner class
    private static class MyGraphic extends View {
        int startX=-1, startY=-1, stopX=-1, stopY=-1;//시작,끝좌표 초기값

        public MyGraphic(Context context){
            super(context);
        }

        //터치이벤트처리
        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();
                    this.invalidate();
                    break;
            }

            return true;

        }


        protected void onDraw(Canvas canvas){
            super.onDraw(canvas);

            // Paint생성
            Paint paint = new Paint();
            paint.setAntiAlias(true);//테두리를 부드럽게.
            paint.setStrokeWidth(5);//선두께
            paint.setStyle(Paint.Style.STROKE);//색을 채우지 않음
            paint.setColor(Color.RED);//색상설정

            //선그리기
            canvas.drawLine(startX, startY, stopX, stopY, paint);

            //원그리기
            int radius=(int)Math.sqrt(Math.pow(stopX-startX, 2)+Math.pow(stopY-startY,2));//반지름구하기
            canvas.drawCircle(startX,startY,radius,paint);

        }

    }
}

'안드로이드개발강좌' 카테고리의 다른 글

카메라 기능 구현  (0) 2017.05.17
구글맵  (0) 2017.05.01
안드로이드 그래픽  (0) 2015.11.18
구글맵 마커 아이콘 red-dot  (0) 2015.11.17
genymotion gapp설치  (0) 2015.11.12
Comments