본문 바로가기

Android

[안드로이드 링크 정리] Fragment에서 Custom View Canvas 사용하기. [2019_11_25~ 2019_11_30]

반응형

Fragment 위에서  Custom_View 위에  canvas를 이용해 도형을 그리 것을 하기 위해 참고한 링크들을 첨부한다.

 

 

1. 먼저 CustomView & canvas를 활용해 Custom View 위에  도형을 그리는 예제를 참고하였다.

해당 예제를 이용하면 원 외에도 내가 원하는 도형을 그릴 수 있는 것을 볼 수 있다.

 

https://bitsoul.tistory.com/59

 

 

안드로이드 : 그래픽 기초 예제 Canvas, Paint, Path 사용

안드로이드 : 그래픽 기초 예제 Canvas, Paint, Path 사용 보통은 layout 파일로 setContentView(R.layout.activity_main); 와 같이 사용하여 화면을 표시하였지만, 그래픽을 사용하여 캔버스에 자유롭게 그린 그..

bitsoul.tistory.com

2. 프래그먼트 위에 해당 CustomView 띄우기 관련 링크 

-내가 한 방식과는 다르지만 해당 방식도 유용해 보이므로, 참고하는 것도 좋을 듯 하다.

https://stackoverflow.com/questions/42508280/android-customview-integrated-in-fragment

 

Android CustomView integrated in Fragment

I want to create a draw application. For this I have created a Custom DrawingView. To enable additional options during the drawing, the CustomView should be included in the activity_main.xml in a

stackoverflow.com

 

3.  내가 사용한 방식.

 

먼저 필자의 경우 Fragment에서 frameLayout_2라는 곳에 커스텀 View를 보여줄 예정이므로 다음과 같이 onCreateView라는 부분에 커스텀뷰를 보여줄 레이아웃을 다음과 같이 추가 해준다. 

 @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        Log.d(TAG, "onCreateView: 언제실행?");

       View rootView =inflater.inflate(R.layout.fragment_camera2_basic, container, false);

       frameLayout_2 = rootView.findViewById(R.id.in_frameLayout);
       //frameLayout= rootView.findViewById(R.id.layout_frame);


        return rootView;
        //return inflater.inflate(R.layout.fragment_camera2_basic, container, false);
    }

 

내가 그릴 customView의 경우 가장 상단에 위치해야 하므로, dispatchDraw에 원을 그리도록 하였다.

 

 dispatchDraw()를 오버라이드할 경우에는 다음과 같은 사항에 유의해야 한다.
 다음과 같이 오버라드하면 ViewGroup내의 모든 자식 View들이 모두 그려진 후에 개발자가 그리는 내용이 그 위에 그려진다. 반대로 super.dispatchDraw(canvas)를 개발자의 그리는 작업 후에 호출하면 그려진 그림 위에 자식 View가 나타난다.

https://micropilot.tistory.com/1573

 

Android ViewGroup.dispatchDraw()

ViewGroup.dispatchDraw() ViewGroup에 포함되어 있는 이 메소드는 ViewGroup이 다시 그려져야 할 경우에 자동으로 호출된다. View에 포함된 onDraw()는 해당 View가 다시 그려져야 할 경우에 자동으로 호출된다...

micropilot.tistory.com

https://stackoverflow.com/questions/4827848/ondraw-on-view-draws-behind-the-layout

 

onDraw() on View draws behind the layout

Ok, here's the deal. I want to move items in my extended gallery class to change the order of images. The way i'm doing it now: on long press remove the current selected item, use onDraw() to draw...

stackoverflow.com

 public class CustomView extends View {

        Paint paint = new Paint();
    
        int color;
    



        public CustomView(Context context) {
            super(context);

         

           color = ContextCompat.getColor(context, R.color.color_z_zero); // new

        

        }

        @Override
        protected void onDraw(Canvas canvas) {
        }

        @Override
        protected void dispatchDraw(Canvas canvas) {
            super.dispatchDraw(canvas);
          

                paint.setColor(color);
                   
                canvas.drawCircle(X,Y, 60, paint);


        }



    }

 

 

다음 onViewCreated 부분에 다음과 같이 Customview를 객체화 해주고, 커스텀뷰가 생성 될 위치를 지정해준다.

아래 방법이 싫을 경우 아래 customview.X와 customview.Y 지우고 CustomView 클래스에서 원하는 위치를 직접 입력해줘도 된다.

customView = new CustomView(getActivity().getApplicationContext()); 여기서 프래그먼트에서 사용하므로, getActivity().getApplicationContext()를 해주었다. Activity에서 사용한다면, getActivity() 또는 Activity.this를 사용하면 될 것으로 판단 된다.

 

 그 이후 위에서 선언한 frameLayout_2에 해당 view를 추가해주면 된다. 

   @Override
    public void onViewCreated(final View view, Bundle savedInstanceState) {

      
		//위에서 커스텀뷰 클래스 객세를 생성하고 그려질 뷰 위치를 선택해준다.
        customView = new CustomView(getActivity().getApplicationContext());
        
        customView.X = 100;
        customView.Y = 200;
        
        
       frameLayout_2.addView(customView);
        }

 

 

 

 

Web, Android, iOS 개발 문의 및 앱 웹 제작 문의

Email : funidea2020@naver.com

https://funidea.co.kr

 

https://funidea.co.kr/

AI 챗봇 & 사물인식 사물 인식, 인공지능 챗봇 등 Smart한 인공지능 기술을 통해 반복 업무를 줄여 보세요. 위치 기반 서비스 GPS 기능을 활용하여 현재 위치를 기준으로 주변 정보(은행, 음식점, 주�

funidea.co.kr

 

여성 신체 사이즈 기반 상품 추천 서비스!

나에게 딱 맞은 골라보자! Fit Me !

https://fit-me.kr/

 

핏미 - 사이즈 고민 없는 여성 쇼핑앱

더이상의 사이즈 고민은 그만! 수많은 여성 쇼핑몰을 한눈에! - 핏미

fit-me.kr

 

반응형