기술참고자료/Android | 2011. 4. 15. 22:16
package com.vartist.drawdemo;출처 : http://bestsiteinthemultiverse.com/2008/11/android-graphics-example/
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Rect;
import android.os.Bundle;
import android.view.View;
public class DrawDemo extends Activity {
DemoView demoview;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
demoview = new DemoView(this);
setContentView(demoview);
}
private class DemoView extends View{
public DemoView(Context context){
super(context);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
// 사용자 지정 그리기 코드
// 기억하기: y값은 하단을 기준으로 위를 향해 증가한다.
// x 는 좌측에서 우측으로 증가한다.
int x = 0;
int y = 0;
Paint paint = new Paint();
paint.setStyle(Paint.Style.FILL);
// 전체 캔버스를 화이트로 입혀준다.
paint.setColor(Color.WHITE);
canvas.drawPaint(paint);
// 다른 방법으로는 이렇게 한다:
// canvas.drawColor(Color.WHITE);
// 속이꽉찬 파란색 원을 그려보자.
paint.setColor(Color.BLUE);
canvas.drawCircle(20, 20, 15, paint);
// 안티알리아싱 기능을 이용하여 파란색 원을 그려보자.
paint.setAntiAlias(true);
paint.setColor(Color.BLUE);
canvas.drawCircle(60, 20, 15, paint);
// 한번 그려진 원들을 비교해보자.
// 첫번째 원은 톱니모양의 경계선이 존재한다.
// 두번째 원은 부드러운 경계선이 존재한다.
// 속이꽉찬 녹색 사각형을 그려보자.
paint.setAntiAlias(false);
paint.setColor(Color.GREEN);
canvas.drawRect(100, 5, 200, 30, paint);
// 삼각형을 생성하고 그려보자.
// 세개의 라인세그먼트를 저장하기 위해 Path 객체를 사용하자.
// 많은 위치에 그리기 위해 .offset을 사용하자.
// 노트 : 이 사각형은 0, 0의 위치에 있지 않다.
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(2);
paint.setColor(Color.RED);
Path path = new Path();
path.moveTo(0, -10);
path.lineTo(5, 0);
path.lineTo(-5, 0);
path.close();
path.offset(10, 40);
canvas.drawPath(path, paint);
path.offset(50, 100);
canvas.drawPath(path, paint);
// offset은 cumlative (?)하다.
// 다음엔 이전위치에서 50, 100 옮겨진 위치에 그려보자.
path.offset(50, 100);
canvas.drawPath(path, paint);
// STROKE 스타일을 이용하여 몇개의 텍스트를 그려보자.
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(1);
paint.setColor(Color.MAGENTA);
paint.setTextSize(30);
canvas.drawText("Style.STROKE", 75, 75, paint);
// FILL 스타일을 이용하여 몇개의 텍스트를 그려보자.
paint.setStyle(Paint.Style.FILL);
// 안티알리아싱을 켜보라.
paint.setAntiAlias(true);
paint.setTextSize(30);
canvas.drawText("Style.FILL", 75, 110, paint);
// 몇개의 회전된 텍스트를 그려보자.
// 텍스트의 너비와 높이를 구하자.
// 원하는 위치에 그려보자.
x = 75;
y = 185;
paint.setColor(Color.GRAY);
paint.setTextSize(25);
String str2rotate = "Rotated!";
// 회전된 텍스트를 그리기 앞서 결합된 사각형을 그려보자.
Rect rect = new Rect();
paint.getTextBounds(str2rotate, 0, str2rotate.length(), rect);
canvas.translate(x, y);
paint.setStyle(Paint.Style.FILL);
// 회전하지 않은 텍스트를 그려보자.
canvas.drawText("!Rotated", 0, 0, paint);
paint.setStyle(Paint.Style.STROKE);
canvas.drawRect(rect, paint);
// x, y로의 위치이동을 취소하자.
canvas.translate(-x, -y);
// 그려질 텍스트의 중앙을 기준으로 캔버스를 회전하자.
canvas.rotate(-45, x + rect.exactCenterX(), y + rect.exactCenterY());
// 회전된 텍스트를 그려보자.
paint.setStyle(Paint.Style.FILL);
canvas.drawText(str2rotate, x, y, paint);
// 회전을 취소하자.
canvas.restore();
canvas.drawText("After canvas.restore()", 50, 250, paint);
// 점선막대를 그려보자.
DashPathEffect dashPath = new DashPathEffect(new float[]{20,5}, 1);
paint.setPathEffect(dashPath);
paint.setStrokeWidth(8);
canvas.drawLine(0, 300 , 320, 300, paint);
}
}
}
단말정보 조회관련 정보 (0) | 2011.04.19 |
---|---|
Android Graphics Example - Bitmap Image (1) | 2011.04.16 |
[Android] Marquee effect in TextView (0) | 2011.04.06 |
[Android] Apostrophe not preceded by \ 관련 String 에러 해결방법 (0) | 2011.03.22 |
안드로이드 어플리케이션과 자바스크립트간의 통신샘플 (0) | 2011.03.22 |
Recent Comments