본문 바로가기

Android

캔버스 - 복잡한 도형 그리기

activity_main.xml

 

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:paddingBottom="@dimen/activity_vertical_margin"

    android:paddingLeft="@dimen/activity_horizontal_margin"

    android:paddingRight="@dimen/activity_horizontal_margin"

    android:paddingTop="@dimen/activity_vertical_margin"

    tools:context=".MainActivity" >

 

    <com.example.viewex01.MyView3

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"/>

 

</RelativeLayout>

 

 

MainActivity.java

 

package com.example.viewex01;

 

import android.os.Bundle;

 

 

import android.app.Activity;

import android.content.Context;

import android.graphics.Canvas;

import android.graphics.Color;

import android.graphics.Paint;

import android.view.Menu;

import android.view.View;

 

public class MainActivity2 extends Activity {

 

     @Override

     protected void onCreate(Bundle savedInstanceState) {

           super.onCreate(savedInstanceState);

           setContentView(R.layout.activity_main);

          

     }

    

     @Override

     public boolean onCreateOptionsMenu(Menu menu) {

           // Inflate the menu; this adds items to the action bar if it is present.

           getMenuInflater().inflate(R.menu.main, menu);

           return true;

     }

 

}

 

 

 

MyView3.java

 

package com.example.viewex01;

 

import android.content.Context;

 

import android.graphics.Canvas;

import android.graphics.Color;

import android.graphics.Paint;

import android.graphics.RectF;

import android.util.AttributeSet;

import android.view.View;

 

public class MyView3 extends View {

 

     public MyView3(Context context) {

           super(context);

           // TODO Auto-generated constructor stub

           System.out.println("MyView2(Context context) 호출");

     }

 

     public MyView3(Context context, AttributeSet attrs) {

           super(context, attrs);

           // TODO Auto-generated constructor stub

           System.out.println("MyView2(Context context, AttributeSet attrs) 호출");

     }

 

    

 

     @Override

    

           protected void onDraw(Canvas canvas) {

                // TODO Auto-generated method stub

                //super.onDraw(canvas);

               

               

                Paint pnt = new Paint();

                pnt.setColor(Color.LTGRAY);

                canvas.drawPaint(pnt);

                // 빨간색 둥근 사각형

                RectF r = new RectF(10,10,100,100);

                pnt.setColor(Color.RED);

                canvas.drawRoundRect(r, 10, 10, pnt);

                //타원

                r.set(110, 10, 150, 100);

                canvas.drawOval(r, pnt);

                //분홍색 반달

                pnt.setColor(Color.MAGENTA);

                r.set(10,110,100,200);

                canvas.drawArc(r, 10, 150, false, pnt);

                //분홍색 파이

                r.set(110, 110, 200, 200);

                canvas.drawArc(r, 10, 150, true, pnt);

                // 파란색 다각선

                pnt.setColor(Color.BLUE);

                float[] pts={10,210,50,250,50,250,110,220};

                canvas.drawLines(pts, pnt);

                //검정색 3

                pnt.setColor(Color.BLACK);

                float[] pts2={20,210,50,240,100,220};

                canvas.drawPoints(pts2, pnt);

               

     }

    

}

 



 

'Android' 카테고리의 다른 글

캔버스 - setStroke  (0) 2013.03.29
캔버스 -AntiAlias  (0) 2013.03.29
캔버스(canvas)  (0) 2013.03.29
이미지 뷰(ImageView) - 이미지 버튼(ImageButton)  (0) 2013.03.28
이미지 뷰(imageView) - 확대 모드  (0) 2013.03.28