달력

5

« 2024/5 »

  • 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
반응형
728x90
반응형

안드로이드 개발을 하다보면 XML은 거의 필수적(?)으로 건드리는 경우가 많습니다.
물론 XML을 사용하지 않고 그냥 java 코드만으로 이루어진 프로젝트들도 많이 있지만,
XML을 사용할 경우 좀 더 간편하고 쉽게 어플리케이션의 레이아웃을 만들 수 있습니다.

개인적으로 가장 좋은 방법은 가중치(weight)를 줘서 화면 크기에 관계없는 레이아웃을 작성하는 것이
가장 좋다고 생각하지만...

[안드로이드] 화면 해상도에 관계없는 레이아웃(Layout) 만들기 - http://snowbora.com/422


가중치를 이용할 경우 XML 코드가 복잡해지고 가독성이 떨어지는 단점이 있습니다.
사실 dp(dip) 단위를 사용해도 다양한 디바이스들의 화면 해상도에 큰 영향을 안 받아야 하지만,
삼성의 갤럭시탭7인치를 비롯해서, 너무 괴상한 해상도의 디바이스들이 많아져서 dp(dip) 단위의
효용성이 많이 줄어들었습니다. 하지만, 그래도 알아두면 나쁠 건 없으니....;;;
한 번 나열해보도록 하겠습니다.


1.px
픽셀 단위. 화면의 1픽셀을 나타냅니다. 간단한 프로젝트에서는 오히려 사용하기 편할 수도 있습니다.

2. pt
포인트. 일반적으로 폰트 크기에서 사용합니다. 밀리미터나 인치처럼 모바일내가 아닌 바깥에서의 크기 단위입니다.
인치로 환산하면 1/72 인치입니다.

3. dp (dip)
dp라고 하든 dip라고 하든 상관없습니다. 화면의 dpi 크기가 160dpi 일 때, 1 dp는 1 픽셀입니다.
조금 추상적인 의미라서 사용하기 어렵기도 하고, 정확하지도 않습니다. -_-;;
하지만, 그나마 다양한 해상도를 지원하기 유용한 단위라고 볼 수도 있습니다.

px 단위와의 환산은 dip = px * (160 / density) 입니다.
만약, 480x800 단말기라고 하면, 480x800 단말의 dpi(density)는 240이기 때문에
환산 공식은 dip = px * (160/240) = px * (2/3) 가 됩니다.

4. sp
스케일과 독립적인 픽셀 크기입니다. dp 처럼 유동적인 크기이며, 주로 폰트 크기 단위로 많이 사용됩니다.

5. in
1 인치를 뜻합니다. 모바일 상에서의 크기가 아닌, 실제 1 인치 크기를 나타냅니다.
좀 애매한 단위겠죠? ^^; 1 인치는 2.54 cm입니다.

6. mm
1 밀리미터를 의미합니다.



정답은 없지만, 아무래도 그나마 추천드리는 단위는
크기로는 dp, 폰트에는 sp 단위를 쓰는 것이 가장 좋다고 생각합니다.
경우에 따라서는 px 단위까지는 쓸만한 것 같습니다.

728x90
반응형
:
Posted by mapagilove
728x90
반응형

SD Card 폴더 생성하기

Android/File

 

먼저 SD Card에 사용을 위해 퍼미션을 줘야한다.

 

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>

 

 

import java.io.*;
import android.app.*;
import android.os.*;
import android.widget.*;

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

String str = Environment.getExternalStorageState();
if ( str.equals(Environment.MEDIA_MOUNTED)) {

String dirPath = "/sdcard/android/data/pe.berabue.sdtest/temp";
File file = new File(dirPath);
if( !file.exists() ) // 원하는 경로에 폴더가 있는지 확인
file.mkdirs();
}
else
Toast.makeText(SDTest.this, "SD Card 인식 실패", Toast.LENGTH_SHORT).show();
}
}

 

 

 

file.mkdirs() file.mkdir()의 차이점

 

mkdirs()는 원하는 경로의 상위 폴더가 없으면 상위 폴더까지 생성.

ex ) sdcard/android/data/aaa/bbb

bbb 폴더를 만드려는데 aaa폴더가 없다면 aaa 폴더까지 생성 한다.

 

mkdir()은 지정 폴더만 생성.

 

 

 

구글에서 권장하는 폴더명은

 

android/data/패키지명/폴더명

 

 

728x90
반응형
:
Posted by mapagilove
728x90
반응형

안드로이드 - [Android] 전체화면 투명 Activity  안드로이드 / 개발  2011/07/30 11:07

 http://blog.naver.com/gh2501/133922225


 
출처 : http://eddykudo.com/105

 


AndroidManifest.xml 파일에서

 

  <activity android:name=".SampleTransActivity" android:screenOrientation="landscape"
   android:theme="@android:style/Theme.Translucent.NoTitleBar.Fullscreen"
   android:label="@string/app_name"
   android:configChanges="orientation|keyboardHidden">
  </activity>
[출처] 안드로이드 - [Android] 전체화면 투명 Activity|작성자 실바

728x90
반응형
:
Posted by mapagilove
728x90
반응형

Java에서, 숫자 제곱 함수 사용 방법


소스 파일명: Example.java
public class Example {
public static void main(String[] args) {


////////////////////////////////////////////////
// 3의 제곱 (3의 2승) 구하기
System.out.println( Math.pow(3, 2) );
// 출력 결과: 9.0



////////////////////////////////////////////////
// 5의 세제곱 (5의 3승) 구하기
double result;
double i = 5;
double j = 3;

result = Math.pow(i, j);
System.out.println(result);
// 출력 결과: 125.0



////////////////////////////////////////////////
// 3의 네제곱 (3의 4승) 구하기
// 출력을 정수형으로 변환하기
int result2;
int i2 = 3;
int j2 = 4;

result2 = (int) Math.pow(i2, j2);
System.out.println(result2);
// 출력 결과: 81



}
}

 

728x90
반응형
:
Posted by mapagilove
728x90
반응형

SD Card 폴더 생성하기

Android/File

 

먼저 SD Card에 사용을 위해 퍼미션을 줘야한다.

 

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>

 

 

import java.io.*;
import android.app.*;
import android.os.*;
import android.widget.*;

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

String str = Environment.getExternalStorageState();
if ( str.equals(Environment.MEDIA_MOUNTED)) {

String dirPath = "/sdcard/android/data/pe.berabue.sdtest/temp";
File file = new File(dirPath);
if( !file.exists() ) // 원하는 경로에 폴더가 있는지 확인
file.mkdirs();
}
else
Toast.makeText(SDTest.this, "SD Card 인식 실패", Toast.LENGTH_SHORT).show();
}
}

 

 

 

file.mkdirs() file.mkdir()의 차이점

 

mkdirs()는 원하는 경로의 상위 폴더가 없으면 상위 폴더까지 생성.

ex ) sdcard/android/data/aaa/bbb

bbb 폴더를 만드려는데 aaa폴더가 없다면 aaa 폴더까지 생성 한다.

 

mkdir()은 지정 폴더만 생성.

 

 

 

구글에서 권장하는 폴더명은

 

android/data/패키지명/폴더명

 

 

 

 

728x90
반응형
:
Posted by mapagilove
728x90
반응형

6. 아답터 - 아이콘 리스트 뿌리기

Posted on March 12th, 2009 by christopherJ


아답터만 냅다 팠는데 벌써 다섯번째 예제가 됩니다. 이번에는 android.R.drawable 밑에 정의되어있는 시스템 아이콘을 리스트하는 프로그램을 만들었습니다. 프로그램은 SimpleAdapter 사용하는데 내부적으로 android.R.layout.activity_list_item 라는 시스템에서 제공하는 레이아웃을 사용합니다. 시스템 레이아웃를 직접보고 싶으면 SDK tools/lib/res/default/layout 밑에 있는 XML 파일들을 참조합니다. 개발자 Reference http://developer.android.com/reference/android/R.layout.html 있습니다.
.
android.R.layout.activity_list_item
모습은 다음과 같습니다.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="fill_parent"

    android:layout_height="wrap_content"

    android:paddingTop="1dip"

    android:paddingBottom="1dip"

    android:paddingLeft="6dip"

    android:paddingRight="6dip">

 

    <ImageView android:id="@+id/icon"

        android:layout_width="24dip"

        android:layout_height="24dip"/>

 

    <TextView android:id="@android:id/text1"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_gravity="center_horizontal"

        android:paddingLeft="6dip" />

</LinearLayout>

내용을 보면 레이아웃을 활용하는 것이 얼마나 안드로이드 프로그래밍을 쉽게 할수있습니다. 아이콘이미지의 리소스를 간단히 @+id/icon 매칭하여 ImageView 이용하여 아이콘 이미지를 출력합니다.
.
참고로 다른 두개의 가장 많이 사용하는 시스템 레이아웃을 보면

.
simple_list_item_1:

<TextView xmlns:android="http://schemas.android.com/apk/res/android"

        android:id="@android:id/text1"

        style="?android:attr/listItemFirstLineStyle"

        android:paddingTop="2dip"

        android:paddingBottom="3dip"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content" />

.
simple_list_item_2:

<TwoLineListItem xmlns:android="http://schemas.android.com/apk/res/android"

        android:paddingTop="2dip"

        android:paddingBottom="2dip"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content">

 

        <TextView android:id="@android:id/text1"

               android:layout_width="fill_parent"

               android:layout_height="wrap_content"

               style="?android:attr/listItemFirstLineStyle"/>

 

        <TextView android:id="@android:id/text2"

               android:layout_width="fill_parent"

               android:layout_height="wrap_content"

               android:layout_below="@android:id/text1"

               style="?android:attr/listItemSecondLineStyle" />

</TwoLineListItem>

.
이제 소스를 봅니다.

public class Adapter6 extends ListActivity {

 

        @Override

        public void onCreate( Bundle savedInstanceState ) {

               super.onCreate(savedInstanceState);

 

               HashMap<String,Integer> iconList = getConstants();

               ArrayList<HashMap<String,Object>> mList = new ArrayList<HashMap<String,Object>>();

               for(String iconName:iconList.keySet()) {

                       HashMap<String,Object> item = new HashMap<String,Object>();

                       item.put("col1",iconName);

                       item.put("col2",iconList.get(iconName));

                       mList.add(item);  

               }

 

               SimpleAdapter adapter6 = new SimpleAdapter(

                       this,

                       mList,

                       android.R.layout.activity_list_item,

                       new String[] { "col1","col2" },

                       new int[] { android.R.id.text1, android.R.id.icon }  );

               setListAdapter(adapter6);

 

        }

 

        private HashMap<String,Integer> getConstants() {

               Class<?> c = android.R.drawable.class;

               HashMap<String,Integer> iconList = new HashMap<String,Integer>();

               Field[] flds = c.getFields();

               for(Field f : flds) {

                       if(f.getType().equals(Integer.TYPE)) {

                               try {

                                      iconList.put(f.getName(), f.getInt(null));

                               } catch(Exception e) { }

                       }

               }

               return iconList;

        }

}

간단합니다.
(1)
아이콘을 참조할수있는 가장 좋은곳이 android.R.drawable 이므로 그곳에 가서 모든 int 읽었습니다

(2)
이름과 대응되는 Constant 넘버를 iconList 라는 HashMap 저장하고
(3)
이를 바탕으로 화면에 뿌릴때 사용될 ArrayList 만들었습니다.
(4)
ArrayList SimpleAdapter 넘깁니다.

 

 

728x90
반응형
:
Posted by mapagilove
반응형