달력

3

« 2024/3 »

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
2012. 8. 21. 21:35

안드로이드 - 자 그리기 안드로이드 이야기2012. 8. 21. 21:35

728x90
반응형

안드로이드 - 자 그리기

조회 수 1063 추천 수 0 2011.10.03 13:13:01

실제의 자..이다. 필요할지는 음..?

안드로이드정복 저자 홈피 소스에서

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="net.ock"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="7" />

<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".RullerActivity"
android:label="@string/app_name"
android:screenOrientation="landscape"
android:configChanges="orientation|keyboardHidden"
android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

</application>
</manifest>

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<net.ock.Ruler
android:id="@+id/ruler"
android:layout_width="wrap_content"
android:layout_height="0px"
android:layout_weight="1"
/>
<RadioGroup
android:id="@+id/rulerunit"
android:layout_width="fill_parent"
android:layout_height="0px"
android:layout_weight="1"
android:gravity="center"
android:orientation="horizontal"
android:checkedButton="@+id/mili"
android:background="#cccccc"
>
<RadioButton
android:id="@id/mili"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#0000ff"
android:text="Mili "
/>
<RadioButton
android:id="@+id/inch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ff0000"
android:text="Inch "
/>
</RadioGroup>
</LinearLayout>

package net.ock;

import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.View;
import android.widget.RadioGroup;

public class RullerActivity extends Activity {
Ruler mRuler;

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

mRuler = (Ruler)findViewById(R.id.ruler);
RadioGroup RadioUnit = (RadioGroup)findViewById(R.id.rulerunit);
RadioUnit.setOnCheckedChangeListener(mCheckRadio);
}

// 단위 변경시 룰러에게 알림
RadioGroup.OnCheckedChangeListener mCheckRadio = new RadioGroup.OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId) {
case R.id.mili:
mRuler.setMili(true);
break;
case R.id.inch:
mRuler.setMili(false);
break;
}
}
};
}

class Ruler extends View {
boolean mMili;

public Ruler(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}

public Ruler(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}

public Ruler(Context context) {
super(context);
init();
}

void init() {
mMili = true;
}

public void setMili(boolean aMili) {
mMili = aMili;
invalidate();
}

protected void onDraw(Canvas canvas) {
canvas.drawColor(Color.LTGRAY);
Paint Pnt = new Paint();
Pnt.setColor(Color.BLACK);
Pnt.setTextAlign(Paint.Align.CENTER);

int x, y;
Resources res = getResources();
DisplayMetrics dm = res.getDisplayMetrics();

// 텍스트는 15dip 크기
int textsize = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 15, dm);
Pnt.setTextSize(textsize);

// 1단위, 5단위, 10단위의 눈금 세로 길이 미리 계산
int one = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 20, dm);
int five = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 30, dm);
int ten = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 40, dm);

// 단위 변환에 사용할 상수 미리 구해 둠
int unitdim;
if (mMili) {
unitdim = TypedValue.COMPLEX_UNIT_MM;
} else {
unitdim = TypedValue.COMPLEX_UNIT_IN;
}

for (int unit = 0;;unit++) {
Pnt.setAntiAlias(false);
// 밀리미터는 1단위, 인치는 0.1단위로 눈금 표시
if (mMili) {
x = (int)TypedValue.applyDimension(unitdim, unit, dm);
} else {
x = (int)TypedValue.applyDimension(unitdim, unit/10.0f, dm);
}

// 뷰의 폭을 넘을 때까지 그린다.
if (x > getWidth()) {
break;
}

// 눈금의 길이 계산
if (unit % 10 == 0) {
y = ten;
} else if (unit % 5 == 0) {
y = five;
} else {
y = one;
}
canvas.drawLine(x, 0, x, y, Pnt);

// 매 10단위마다 텍스트 출력. 안티 알리아싱 적용
if (unit % 10 == 0) {
Pnt.setAntiAlias(true);
String text;
if (mMili) {
text = "" + unit;
} else {
text = "" + (unit / 10);
}
canvas.drawText(text, x, y + textsize, Pnt);
}
}
}
}

728x90
반응형
:
Posted by mapagilove