Presentation is loading. Please wait.

Presentation is loading. Please wait.

Ch3. 캔버스 활용 3-2 도형으로 안드로이드 캐릭터 만들기.

Similar presentations


Presentation on theme: "Ch3. 캔버스 활용 3-2 도형으로 안드로이드 캐릭터 만들기."— Presentation transcript:

1 Ch3. 캔버스 활용 3-2 도형으로 안드로이드 캐릭터 만들기

2 프로그램 구성 프로젝트 생성 사용자 뷰 클래스 생성

3 실행결과

4 소스 코드 ShapeAppActivity.java
001: package my.test.canvas; 002: 003: import android.app.Activity; 004: import android.os.Bundle; 005: 006: public class ShapeAppActivity extends Activity { 007: 008: @Override 009: public void onCreate(Bundle savedInstanceState) { 010: super.onCreate(savedInstanceState); 011: 012: setContentView(new ShapeView(this)); 013: } 014: }

5 소스 코드 ShapeView.java 001: package my.test.canvas; 002:
003: import android.content.Context; 004: import android.content.res.Configuration; 005: import android.graphics.Canvas; 006: import android.graphics.Color; 007: import android.graphics.LinearGradient; 008: import android.graphics.Paint; 009: import android.graphics.Path; 010: import android.graphics.Point; 011: import android.graphics.RadialGradient; 012: import android.graphics.RectF; 013: import android.graphics.Shader; 014: import android.view.View; 015: 016: public class ShapeView extends View { 017: private final float COMM_WIDTH = 250; 018: private final float HEAD_HEIGHT = COMM_WIDTH - (COMM_WIDTH / 4); 019: private final float EYE_RADIUS = HEAD_HEIGHT / 12; 020: private final float MARGINE = EYE_RADIUS / 2;

6 소스 코드 ShapeView.java 021: private final float BODY_HEIGHT = HEAD_HEIGHT - COMM_WIDTH / 10; 022: private final float ARM_WIDTH = EYE_RADIUS * 5; 023: 024: private Paint paint; 025: private Point center; 026: 027: private RectF head_rect; 028: private RectF body_rect; 029: private RectF arm_rect; 030: 031: private float x1, y1, x2, y2; 032: 033: public ShapeView(Context context) { 034: super(context); 035: 036: center = new Point(); 037: paint = new Paint(); 038: 039: paint.setAntiAlias(true); 040:

7 소스 코드 ShapeView.java 041: setBackgroundColor(Color.WHITE); 042: } 043:
042: } 043: 044: @Override 045: protected void onDraw(Canvas canvas) { 046: center.x = getWidth() / 2; 047: 048: Configuration config = getResources().getConfiguration(); 049: if(config.orientation == Configuration.ORIENTATION_LANDSCAPE) { 050: center.y = getHeight() / 2; 051: } 052: else { 053: center.y = getHeight() / 3; 054: } 055: 056: headDraw(canvas, paint); 057: bodyDraw(canvas, paint); 058: 059: super.onDraw(canvas); 060: }

8 소스 코드 ShapeView.java 061: 062: private void headDraw(Canvas canvas, Paint paint) { 063: x1 = center.x - COMM_WIDTH / 2; 064: y1 = center.y - HEAD_HEIGHT; 065: x2 = center.x + COMM_WIDTH / 2; 066: y2 = center.y; 067: head_rect = new RectF(x1, y1, x2, y2); 068: 069: x1 = head_rect.centerX(); 070: y1 = head_rect.centerY(); 071: RadialGradient grad = new RadialGradient(x1, y1, head_rect.width(), Color.GREEN, Color.BLACK, Shader.TileMode.CLAMP); 072: 073: paint.setAlpha(255); 074: paint.setStyle(Paint.Style.FILL); 075: paint.setShader(grad); 076: 077: canvas.drawArc(head_rect, 0, -180, false, paint); 078: paint.setShader(null); 079: paint.setColor(Color.WHITE); 080: canvas.drawCircle(x1 - EYE_RADIUS * 3, y1 - EYE_RADIUS * 3, EYE_RADIUS, paint);

9 소스 코드 ShapeView.java 081: canvas.drawCircle(x1 + EYE_RADIUS * 3, y1 - EYE_RADIUS * 3, EYE_RADIUS, paint); 082: 083: antennaDraw(canvas, paint); 084: } 085: 086: private void antennaDraw(Canvas canvas, Paint paint) { 087: x1 = head_rect.centerX(); 088: y1 = head_rect.top; 089: 090: paint.setStyle(Paint.Style.STROKE); 091: paint.setStrokeWidth(10); 092: paint.setColor(Color.rgb(0, 80, 0)); 093: 094: canvas.drawLine(x1 - 60, y1 - 20, x1 - 40, y1+ 7, paint); 095: canvas.drawLine(x1 + 60, y1 - 20, x1 + 40, y1 + 7, paint); 096: } 097: 098: 099: private void bodyDraw(Canvas canvas, Paint paint) { 100: x1 = head_rect.left;

10 소스 코드 ShapeView.java 101: y1 = head_rect.bottom - HEAD_HEIGHT / 2 + MARGINE; 102: x2 = head_rect.right; 103: y2 = head_rect.bottom + BODY_HEIGHT; 104: body_rect = new RectF(x1, y1, x2, y2); 105: 106: x1 = body_rect.centerX(); 107: y1 = body_rect.top; 108: x2 = body_rect.centerX(); 109: y2 = body_rect.bottom; 110: LinearGradient grad = new LinearGradient(x1, y1, x2, y2, Color.RED, Color.BLACK, Shader.TileMode.CLAMP); 111: 112: paint.setStyle(Paint.Style.FILL); 113: paint.setShader(grad); 114: canvas.drawRoundRect(body_rect, 10, 10, paint); 115: 116: armDraw(canvas, paint); 117: markDraw(canvas, paint); 118: } 119: 120: private void armDraw(Canvas canvas, Paint paint) {

11 소스 코드 ShapeView.java 121: arm_rect = new RectF(0, body_rect.top, 0, body_rect.bottom - MARGINE * 2); 122: 123: x1 = body_rect.left - ARM_WIDTH; 124: y1 = arm_rect.top; 125: x2 = body_rect.left - MARGINE; 126: y2 = arm_rect.bottom; 127: RectF rect_left = new RectF(x1, y1, x2, y2); 128: 129: x1 = body_rect.right + MARGINE; 130: y1 = arm_rect.top; 131: x2 = body_rect.right + ARM_WIDTH; 132: y2 = arm_rect.bottom; 133: RectF rect_right = new RectF(x1, y1, x2, y2); 134: 135: paint.setStrokeWidth(20); 136: canvas.drawRoundRect(rect_left, 30, 30, paint); 137: canvas.drawRoundRect(rect_right, 30, 30, paint); 138: paint.setShader(null); 139: } 140:

12 소스 코드 ShapeView.java 141: private void markDraw(Canvas canvas, Paint paint) { 142: Path path = new Path(); 143: 144: paint.setStyle(Paint.Style.STROKE); 145: paint.setStrokeWidth(EYE_RADIUS * 2); 146: paint.setColor(Color.YELLOW); 147: paint.setAlpha(50); 148: 149: path.moveTo(body_rect.left + 40, body_rect.top + 40); 150: path.lineTo(center.x, body_rect.bottom - 70); 151: path.lineTo(body_rect.right - 40, body_rect.top + 40); 152: 153: canvas.drawPath(path, paint); 154: } 155: }


Download ppt "Ch3. 캔버스 활용 3-2 도형으로 안드로이드 캐릭터 만들기."

Similar presentations


Ads by Google