본문 바로가기
OldStory/Android

Compass - Ch4

by Alnilam 2011. 7. 23.
반응형

프로페셔널 안드로이드2 애플리케이션 개발 4장의 Compass (나침판)

실제 센서에서 값을 가지고 오지 않고 고정된 값을 사용하여 뷰만 그려준다.

src/com/paad/compass/CompassView.java
[java]
package com.paad.compass;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;
public class CompassView extends View{
public CompassView(Context context) {
super(context);
initCompassView();
}
public CompassView(Context context, AttributeSet attrs) {
super(context, attrs);
initCompassView();
}
public CompassView(Context context,
AttributeSet ats,
int defaultStyle) {
super(context, ats, defaultStyle);
initCompassView();
}
private Paint markerPaint;
private Paint textPaint;
private Paint circlePaint;
private String northString;
private String eastString;
private String southString;
private String westString;
private int textHeight;

protected void initCompassView() {
setFocusable(true);

Resources r = this.getResources();
circlePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
circlePaint.setColor(r.getColor(R.color.background_color));
circlePaint.setStrokeWidth(1);
circlePaint.setStyle(Paint.Style.FILL_AND_STROKE);

northString = r.getString(R.string.cardinal_north);
eastString = r.getString(R.string.cardinal_east);
southString = r.getString(R.string.cardinal_south);
westString = r.getString(R.string.cardinal_west);

textPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
textPaint.setColor(r.getColor(R.color.text_color));

textHeight = (int)textPaint.measureText("yY");

markerPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
markerPaint.setColor(r.getColor(R.color.marker_color));
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){
int measuredWidth = measure(widthMeasureSpec);
int measuredHeigh = measure(heightMeasureSpec);
int d = Math.min(measuredWidth, measuredHeigh);
setMeasuredDimension(d,d); // 뷰의 크기를 높이와 폭 중 작은 값으로 설정
}
private int measure(int measureSpec) {
int result = 0;
int specMode = MeasureSpec.getMode(measureSpec);
int specSize = MeasureSpec.getSize(measureSpec);

if(specMode == MeasureSpec.UNSPECIFIED) {
result = 200; // 경계가 지정되지 않은 경우 200
}else {
result = specSize; // 영역 전부를 사용
}
return result;
}
private float bearing;
public void setBearing(float _bearing) {
bearing = _bearing;
}
public float getBearing() {
return bearing;
}
@Override
protected void onDraw(Canvas canvas){
int px = getMeasuredWidth() / 2;
int py = getMeasuredHeight() / 2;
int radius = Math.min(px, py);

canvas.drawCircle(px, py, radius, circlePaint);
canvas.save();
canvas.rotate(-bearing, px, py);

int textWidth = (int)textPaint.measureText("W");
int cardinalX = px - textWidth/2;
int cardinalY = py - radius+textHeight;

for (int i = 0; i< 24; i++) {
canvas.drawLine(px, py-radius, px, py-radius+10, markerPaint);
canvas.save();
canvas.translate(0, textHeight);
if(i%6 == 0) {
String dirString = "";
switch(i) {
case(0) : {
dirString = northString;
int arrowY = 2*textHeight;
canvas.drawLine(px, arrowY, px-5, 3*textHeight,markerPaint);
canvas.drawLine(px, arrowY, px+5, 3*textHeight, markerPaint);
break;
}
case(6) : dirString = eastString; break;
case(12) : dirString = southString; break;
case(18) : dirString = westString; break;
}
canvas.drawText(dirString, cardinalX, cardinalY, textPaint);
}
else if(i%3 == 0) {
String angle = String.valueOf(i*15);
float angleTextWidth = textPaint.measureText(angle);
int angleTextX = (int)(px-angleTextWidth/2);
int angleTextY = py-radius+textHeight;
canvas.drawText(angle, angleTextX, angleTextY, textPaint);
}
canvas.restore();
canvas.rotate(15, px, py);
}
canvas.restore();
}
}

[/java]

src/com/paad/compass/Compass.java
[java]
package com.paad.compass;

import android.app.Activity;
import android.os.Bundle;

public class Compass extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.main);
CompassView cv = (CompassView)this.findViewById(R.id.compassView);
cv.setBearing(45);
/*
CompassView cv = new CompassView(this);
setContentView(cv);
cv.setBearing(45);
*/

}
}
[/java]

res/layout/main.xml
[xml]

[/xml]

res/values/color.xml
[xml]

#F555
#AFFF
#AFFF

[/xml]

res/values/strings.xml
[xml]

Compass
N
E
W
S

[/xml]

compass

compass

반응형

'OldStory > Android' 카테고리의 다른 글

갤럭시 S2  (0) 2011.10.22
ContactPicker  (0) 2011.08.29
To Do List - Ch4  (0) 2011.08.08
TodoList -Ch4  (0) 2011.07.14
ToDoList  (0) 2011.06.30