'Android'에 해당되는 글 16건

  1. 2010.08.13 [RelativeLayout] 0. 레이아웃 만들기
  2. 2010.08.13 XML 문법
  3. 2010.08.12 [CustomView] 4. Thread 이용하여 그림 움직이기
  4. 2010.08.12 [CustomView] 3. RobotView 그림 위치 지정 - 성공
  5. 2010.08.12 [CustomView] 1. 그림 정가운데 출력
  6. 2010.08.11 [CustomView] 0. RobotView 생성

[RelativeLayout] 0. 레이아웃 만들기

|

#RelativeLayout
RelativeLayoutTest.java
1. findViewById()로 버튼 객체 선택. final로 선언되어 있어야 함.
2. 버튼에 이벤트 리스너 설정.
package net.itisn.test;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

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

		//리소스에서 버튼을 가져옴
		final Button buttonCancel = (Button)this.findViewById(R.id.ButtonCancel);
		//리소스에서 에디트텍스트를 가져옴
		final EditText editText = (EditText)this.findViewById(R.id.EditText01);

		//취소 버튼에 이벤트 추가
		buttonCancel.setOnClickListener(new OnClickListener() {

			@Override//클릭했을 때 에디트텍스트에 문자열 출력
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				editText.setText("Button is Pushed.");
			}

		});
	}
}

main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
	xmlns:android="http://schemas.android.com/apk/res/android"
	android:layout_width="wrap_content"
	android:layout_height="wrap_content"
>
	<TextView
		android:text="@+id/TextView01"
		android:id="@+id/TextView01"
		android:layout_height="wrap_content"
		android:layout_width="fill_parent"
	></TextView>
	<EditText
		android:text="@+id/EditText01"
		android:id="@+id/EditText01"
		android:layout_below="@id/TextView01"
		android:layout_height="wrap_content"
		android:layout_width="fill_parent"
	></EditText>
	<Button
		android:layout_below="@id/EditText01"
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"
		android:text="Cancel"
		android:id="@+id/ButtonCancel"
	></Button>
	<Button
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"
		android:text="OK"
		android:id="@+id/ButtonOK"
		android:layout_below="@+id/EditText01"
		android:layout_toRightOf="@+id/ButtonCancel"
	></Button>
</RelativeLayout>



- END -

'Android' 카테고리의 다른 글

[ShapeEx]  (0) 2010.08.16
[.java로 View Control]  (0) 2010.08.16
XML 문법  (0) 2010.08.13
[CustomView] 4. Thread 이용하여 그림 움직이기  (0) 2010.08.12
[CustomView] 3. RobotView 그림 위치 지정 - 성공  (0) 2010.08.12
And

XML 문법

|
선언
<?xml version="버전 번호" encoding="인코딩 방식" standalone="yes|no"?>
<와 ?와 xml 사이에 공백이 있으면 안됨.

standalone
작성된 XML 문서를 XML 파서가 해석할 때 외부 DTD 문서를 참고해야 한다는 것을 XML 파서에게 알려주기 위한 목적
yes : DTD 문서를 참고하지 않고 XML 문서 해석
no : DTD 문서를 참고하여 XML 문서 해석
보통 생략하는데 생략시 no로 설정됨

DOM : 전체를 읽어서 추출
SAX : 파일을 읽으면서 처리

Ctrl + Shift + F : 자동으로 정렬, 옵션을 줄 수 있다.
Ctrl + I : 자동으로 정렬

XML 소스 정렬
Preference - XML - Editor - Split, Align


xmlns 네임스페이스
xmlns:android="http://schemas.android.com/apk/res/android"

android 네임스페이스
android:layout_width="fill_parent"

string.xml : Add > name, value 작성
main.xml : Text 태그의 android:id="@+id/text" -> android:text="@string/my_name"

And

[CustomView] 4. Thread 이용하여 그림 움직이기

|
#Thread 이용하여 그림 움직이기

[RobotView.java]
package net.itisn.test;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

public class RobotView extends View implements Runnable {
    private final static int STEP = 10;
    private Drawable image;
    private int viewWidth, viewHeight;
    private int imageWidth, imageHeight;
    private int x, y;

    public RobotView(Context context, AttributeSet attrs) {
    	super(context, attrs);
    	// TODO Auto-generated constructor stub
    	image = this.getResources().getDrawable(R.drawable.su);

    	Thread thread = new Thread(this);
    	thread.start();
    }

    @Override
    protected void onDraw(Canvas canvas) {
    	// TODO Auto-generated method stub
    	image.setBounds(x, y, x + imageWidth, y + imageHeight);
    	image.draw(canvas);

    	super.onDraw(canvas);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    	// TODO Auto-generated method stub
    	viewWidth = this.getWidth();
    	viewHeight = this.getHeight();

    	imageWidth = image.getIntrinsicWidth();
    	imageHeight = image.getIntrinsicHeight();

    	x = viewWidth / 2 - imageWidth / 2;
    	y = viewHeight / 2 - imageHeight / 2;

    	super.onSizeChanged(w, h, oldw, oldh);
    }

    @Override
    public void run() {
    	// TODO Auto-generated method stub
    	for (; ;) {
    		try {
    			Thread.sleep(500);
    			y = Math.min(viewHeight - imageHeight, y + STEP);
    			this.postInvalidate();
    		} catch(InterruptedException e) {
    			e.printStackTrace();
    		}
    	}
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
    	// TODO Auto-generated method stub
    	x = (int)event.getX();
    	y = (int)event.getY();
    	this.invalidate();

    	return super.onTouchEvent(event);
    }
}

[main.xml]
<?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"
    >

<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello"
    />

<net.itisn.test.RobotView
    android:id="@+id/su"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    />

</LinearLayout>



sleep을 쓸 경우 InterrupedException 예외 발생
따라서 try catch 문을 반드시 써주어야 함

static 클래스는 new 연산자 없이 바로 사용가능. 
Math.min
System.out.println(); 
원래 System sys = new System();
sys.out.println()

postInvalidate() 화면을 새로 그림..

ontouchEvent
x = (int)event.getx();
y = (int)event.gety();

this.invalidate();

return true;

- END

'Android' 카테고리의 다른 글

[RelativeLayout] 0. 레이아웃 만들기  (0) 2010.08.13
XML 문법  (0) 2010.08.13
[CustomView] 3. RobotView 그림 위치 지정 - 성공  (0) 2010.08.12
[CustomView] 1. 그림 정가운데 출력  (0) 2010.08.12
[CustomView] 0. RobotView 생성  (0) 2010.08.11
And

[CustomView] 3. RobotView 그림 위치 지정 - 성공

|
#onSizeChanged()에서 그림 위치 셋팅 - 그림이 정상적으로 그려짐
[RobotView.java]
onSizeChanged()를 오버라이드하여 그 안에 작성한다.
package net.itisn.test;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;

public class RobotView extends View {

    private Drawable image;
    private int viewWidth, viewHeight;
    private int imageWidth, imageHeight;
    private int x, y;

    public RobotView(Context context, AttributeSet attrs) {
    	super(context, attrs);
    	// TODO Auto-generated constructor stub
    	image = this.getResources().getDrawable(R.drawable.su);
    }

    @Override
    protected void onDraw(Canvas canvas) {
    	// TODO Auto-generated method stub
    	image.draw(canvas);
    	super.onDraw(canvas);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    	// TODO Auto-generated method stub
    	viewWidth = this.getWidth();
    	viewHeight = this.getHeight();

    	imageWidth = image.getIntrinsicWidth();
    	imageHeight = image.getIntrinsicHeight();

    	x = viewWidth / 2 - imageWidth / 2;
    	y = viewHeight / 2 - imageHeight / 2;
    	image.setBounds(x, y, x + imageWidth, y + imageHeight);

    	super.onSizeChanged(w, h, oldw, oldh);
    }
}

[main.xml]
<?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"
    >

<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello"
    />

<net.itisn.test.RobotView
    android:id="@+id/su"
    android:layout_height="wrap_content"
    android:layout_width="fill_parent"
    />

</LinearLayout>

'Android' 카테고리의 다른 글

[RelativeLayout] 0. 레이아웃 만들기  (0) 2010.08.13
XML 문법  (0) 2010.08.13
[CustomView] 4. Thread 이용하여 그림 움직이기  (0) 2010.08.12
[CustomView] 1. 그림 정가운데 출력  (0) 2010.08.12
[CustomView] 0. RobotView 생성  (0) 2010.08.11
And

[CustomView] 1. 그림 정가운데 출력

|
#RobotView 이미지 가운체 드로우
[RobotView.java]
package net.itisn.test;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;

public class RobotView extends View {

    private Drawable image;

    public RobotView(Context context, AttributeSet attrs) {
    	super(context, attrs);
    	// TODO Auto-generated constructor stub
    	image = this.getResources().getDrawable(R.drawable.su);
    }

    @Override
    protected void onDraw(Canvas canvas) {
    	// TODO Auto-generated method stub
    	int viewWidth = this.getWidth();
    	int viewHeight = this.getHeight();

    	int imageWidth = image.getIntrinsicWidth();
    	int imageHeight = image.getIntrinsicHeight();

    	int x = viewWidth / 2 - imageWidth / 2;
    	int y = viewHeight / 2 - imageHeight / 2;

    	image.setBounds(x, y, x + imageWidth, y + imageHeight);
    	image.draw(canvas);

    	super.onDraw(canvas);
    }
}

[main.xml]
<?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"
    >

<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello"
    />

<net.itisn.test.RobotView
    android:id="@+id/su"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    />

</LinearLayout>

- END -

And

[CustomView] 0. RobotView 생성

|
#CustomView 만들기

View(Context context, AttributeSet attrs) {}
1. 안드로이드 OS의 현재 상태 정보를 관리하는 클래스
2. 속성 배열

java.lang.Object
ㄴandroid.content.Context

java.lang.Object
ㄴandroid.content.Context
  ㄴandroid.content.ContextWrapper
    ㄴandroid.view.ContextThemeWrapper
      ㄴandroid.app.Activity

Context는 OS입장에서 실행 중인 application을 관리하기 위한 자료구조라고 보시면 될 것 같습니다. 
Class를 생성할 때 또는 정보를 얻고자 할때 context가 parameter로 많이 사용이 되는데 
이는 Class memory를 할당하거나 정보를 얻고자 하는 memory block 정보를 검색하기 위해서 
어떠한 application에서 요청했는지가 필요하기 때문입니다. 

예를들어서 TextView를 생성하는 경우 내부적으로는 heap memory를 할당하고 
해당 memory pointer를 Application context 정보를 기반으로 관리하는 것이고 
Application이 종료하게 되면 해당 application이 사용하는 heap memory를 free하는데 사용합니다. 

AttributeSet은 일반적으로 layout xml에서 android:text="@string/hello"와 같이 설정을 하는데 
LayoutInflater class를 사용해서 TextView를 실시간으로 생성하게 되는 경우 
TextView의 속성을 지정할 때 사용하는 class입니다. 

AttributeSet은 SDK를 보시면 아시겠지만 XML에 기반한 class입니다.


[개요]
1. MyView Class 추가
SuperClass : android.view.View 선택
2. MyView(Context context, AttributeSet attrs) 생성자 추가
View 클래스는 반드시 생성자를 필요로 한다.
x 클릭 > 2번째 생성자 선택
3. 우클릭 > Source > Override 선택 (Alt + Shift + S > v)
onDraw() 선택
위치 지정
4. 그림 정보를 담을 image 변수 생성
private Drawable image; (자동 임포트 : Ctrl + Shift + O)
생성자 안의 super() 아래에서 image에 그림의 아이디를 할당 (R.java에 모든 리소스들이 자동 생성됨)
5. onDraw() 안의 super() 위에서 image 위치 지정 및 그리기
6. main.xml에 태그 삽입
@+id/myview_id : 새로운 아이디 생성하여 사용
@id/myview_id : 기존 아이디 사용
직접 생선한 클래스이므로 디자이너에는 없다. 따라서 직접 xml 코드를 작성해 주어야 한다.
특정 동작을 하는 애니메이션 같은 경우에 커스텀 뷰를 만들어 사용한다.

[RobotView.java]
package net.itisn.test;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;

public class RobotView extends View {

    private Drawable image;

    public RobotView(Context context, AttributeSet attrs) {
    	super(context, attrs);
    	// TODO Auto-generated constructor stub
    	image = this.getResources().getDrawable(R.drawable.su);
    }

    @Override
    protected void onDraw(Canvas canvas) {
    	// TODO Auto-generated method stub
    	image.setBounds(0, 0, 128, 128);
    	image.draw(canvas);
    	//layout(0, 0, 128, 128);

    	super.onDraw(canvas);
    }
}

[main.xml]
<?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"
    >

<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello"
    />

<net.itisn.test.MyView android:id="@+id/su"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    />
<net.itisn.test.SunView
    android:id="@+id/icon"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" 
    />

</LinearLayout>

And
prev | 1 | 2 | next