note

Android Paint로 그림그리기 본문

Android/기본

Android Paint로 그림그리기

투한 2012. 2. 1. 21:13




main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />

</LinearLayout>


main Activity
package com.hardrock.hellotest;

import android.app.Activity;
import android.os.Bundle;
import android.graphics.Canvas;
import android.content.Context;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.view.View;

public class PaintDemo extends Activity {
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(new ViewWithDraw(this));
	}

	private static class ViewWithDraw extends View{

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

		@Override
		protected void onDraw(Canvas canvas){
			//백그라운드 색깔지정
			canvas.drawColor(Color.BLACK);

			Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);

			//draw Line
			mPaint.setColor(Color.RED); //도형의 색깔지정
			mPaint.setStrokeWidth(10);	//도형의 두께
			canvas.drawLine(50, 0, 50, 100, mPaint);
						//시작 x 시작 y 끝x 끝y   paint객체

			mPaint.setColor(Color.GREEN);
			mPaint.setStrokeWidth(5);
			//alpha : 0(투명) ~255 (완전 불투명)
			for(int y = 30,alpha =255; alpha>2 ; alpha -=50, y+=10){
				//alpha값 지정(투명도)를 지정
				mPaint.setAlpha(alpha);
				canvas.drawLine(0,y,100,y,mPaint);
			}
			
			// draw Rect
			mPaint.setColor(Color.WHITE);
			// 테두리만 그린다.
			mPaint.setStyle(Paint.Style.STROKE);
			mPaint.setStrokeWidth(1);
			canvas.drawRect(120, 10, 120 + 80, 10 + 80, mPaint);
						 //left top    right     bottom
			
			mPaint.setColor(Color.MAGENTA);
			// 내부를 채운다
			mPaint.setStyle(Paint.Style.FILL);
			canvas.drawRect(220, 10, 220 + 80, 10 + 80, mPaint);

			// draw Arc(원호 원의 일부분)
			mPaint.setColor(Color.YELLOW);
			canvas.drawArc(new RectF(150, 120, 150 + 100, 120 + 100), 0, 50,
					true, mPaint); // 시작각도(0) , 끝각도(50), 중심사용여부(true)

			// draw Oval(타원도 포함한 원)
			mPaint.setColor(Color.GREEN);
			canvas.drawOval(new RectF(20, 250, 20 + 100, 250 + 50), mPaint);

			// draw RoundRect
			mPaint.setColor(Color.RED);
			canvas.drawRoundRect(new RectF(150, 250, 150 + 100, 250 + 50), 10,
					10, mPaint);//라운드의 정도(10),(10) 포토샵Father효과임

			// draw Path
			mPaint.setColor(Color.YELLOW);
			// 테두리만 그린다.
			mPaint.setStyle(Paint.Style.STROKE);
			mPaint.setStrokeWidth(2);

			Path mPath = new Path();
			mPath.moveTo(0, 0);
			mPath.lineTo(30, 60);
			mPath.lineTo(-30, 60);
			mPath.close();

			//전체 Path 가 위치할 x,y 좌표
			mPath.offset(150, 360);
			canvas.drawPath(mPath, mPaint);
		}
	}
}