본문 바로가기

Android

캔버스 - setStyle

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.MyView5

        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;

     }

 

}

 

 

 

MyView5.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 MyView5 extends View {

 

     public MyView5(Context context) {

           super(context);

           // TODO Auto-generated constructor stub

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

     }

 

     public MyView5(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(Paint.ANTI_ALIAS_FLAG);

 

           Pnt.setStrokeWidth(8);

           Pnt.setColor(Color.RED);

           // 채우기

           Pnt.setStyle(Paint.Style.FILL);

           canvas.drawCircle(50, 50, 40, Pnt);

           // 외곽선그리기

           Pnt.setStyle(Paint.Style.STROKE);

           canvas.drawCircle(150, 50, 40, Pnt);

           // 외곽선및채우기

           Pnt.setStyle(Paint.Style.FILL_AND_STROKE);

           canvas.drawCircle(250, 50, 40, Pnt);

           // 파란색으로채우고빨간색으로외곽선그리기

           Pnt.setColor(Color.BLUE);

           Pnt.setStyle(Paint.Style.FILL);

           canvas.drawCircle(50, 150, 40, Pnt);

           Pnt.setColor(Color.RED);

           Pnt.setStyle(Paint.Style.STROKE);

           canvas.drawCircle(50, 150, 40, Pnt);

           // 빨간색으로외곽선그리고파란색으로채우기

           Pnt.setColor(Color.RED);

           Pnt.setStyle(Paint.Style.STROKE);

           canvas.drawCircle(150, 150, 40, Pnt);

           Pnt.setColor(Color.BLUE);

           Pnt.setStyle(Paint.Style.FILL);

           canvas.drawCircle(150, 150, 40, Pnt);     }

 

}

 



'Android' 카테고리의 다른 글

그리기 객체 - 텍스트  (0) 2013.03.29
그리기 객체 - Bitmap  (0) 2013.03.29
캔버스 - setStroke  (0) 2013.03.29
캔버스 -AntiAlias  (0) 2013.03.29
캔버스 - 복잡한 도형 그리기  (0) 2013.03.29