[ShapeEx]

|
#도형 그리기
ShapeView.java
1. drawText() 이용
2. moveTo(), lineTo() 이용
package net.itisn.test;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Paint.Style;
import android.view.View;

public class ShapeView extends View {

	public ShapeView(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
		this.setBackgroundColor(Color.WHITE);
	}

	@Override
	protected void onDraw(Canvas canvas) {
		// TODO Auto-generated method stub
		super.onDraw(canvas);
		
		// 그리기 객체 생성
		Paint paint = new Paint();
		paint.setAntiAlias(true);
		
		// 선 그리기
		paint.setStrokeWidth(20);	// 굵기
		paint.setStyle(Style.STROKE);	// 실선
		paint.setColor(Color.argb(255, 255, 0, 255));
		canvas.drawLine(0, 0, 200, 200, paint);
		
		// Path를 이용한 그리기
		paint.setStrokeWidth(10);	// 굵기
		paint.setStyle(Style.STROKE);
		paint.setColor(Color.argb(255, 0, 255, 255));
		Path path = new Path();
		path.moveTo(255, 210);
		path.lineTo(0, 0);
		canvas.drawPath(path, paint);
		
		
		
	}
}


도형 그리기
package net.itisn.test;

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.graphics.RectF;
import android.graphics.Paint.Style;
import android.view.View;

public class ShapeView extends View {

	public ShapeView(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
		this.setBackgroundColor(Color.WHITE);
	}

	@Override
	protected void onDraw(Canvas canvas) {
		// TODO Auto-generated method stub
		super.onDraw(canvas);
		
		// 그리기 객체 생성
		Paint paint = new Paint();
		paint.setAntiAlias(true);
		
		// 선 그리기
		paint.setStrokeWidth(20);	// 굵기
		paint.setStyle(Style.STROKE);	// 실선
		paint.setColor(Color.argb(255, 255, 0, 255));
		canvas.drawLine(0, 0, 200, 200, paint);
		
		// Path를 이용한 선 그리기
		paint.setStrokeWidth(10);
		paint.setStyle(Style.STROKE);
		paint.setColor(Color.argb(255, 0, 255, 255));
		Path path = new Path();
		path.moveTo(255, 210);
		path.lineTo(0, 0);
		canvas.drawPath(path, paint);
		
		// 사각형 그리기
		paint.setColor(Color.BLUE);
		paint.setStrokeWidth(2);
		canvas.drawRect(new Rect(100, 100, 200, 200), paint);
		canvas.drawRect(300, 20, 10, 500, paint);
		
		// 라운드 사각형 그리기
		paint.setColor(Color.argb(255, 255, 0, 255));
		canvas.drawRoundRect(new RectF(10, 10, 100, 100), 10, 10, paint);
		
		// 원 그리기
		canvas.drawCircle(50, 50, 40, paint);
	}
}



-end

'Android' 카테고리의 다른 글

[ImageView]  (0) 2010.08.17
[LifeCycle]  (0) 2010.08.17
[.java로 View Control]  (0) 2010.08.16
[RelativeLayout] 0. 레이아웃 만들기  (0) 2010.08.13
XML 문법  (0) 2010.08.13
And