반응형
|
안드로이드(Android) Android.graphics.Color 에서 제공하는 컬러보기 샘플구현 |
|
환경 :Eclipse Mars, Android 4.2.2 |
이번예제는 Android.graphics.Color 에서 제공하는 컬러들이 어떤 종류가 있는지 알아보고 스핀위젯을 사용해 컬러들을 선택했을 때 화면 백그라운드에 적용해 봅니다.
▼ 안드로이드에서 제공하는 컬러는 다음과 같습니다. 이것을 배열로 만들어 배경화면에 각각 적용할려고 합니다.
▼ 메인 activity 에서는 스핀위젯을 등록하는데 setAdapter() 함수로 ArrayAdapter 객체를 셋팅합니다. 그리고 스핀에서 초기 선택값을 첫번째 열에 값으로 하기 위해 setSelecttion(0) 호출합니다.
spinnerColor = (Spinner) findViewById(R.id.colorspinner); spinnerColor.setAdapter(adapter); spinnerColor.setSelection(0);
▼ ArrayAdapter 객체를 만들기 위해 생성자 인수로 안드로이드에서 제공하는 스핀위젯 레이아웃 android.R.layout.simple_spinner_item 와 색깔값 String[] 배열을 넘기게 됩니다. 그리고 스핀을 드롭다운 했을 때 레이아웃도 안드로이드에서 제공하는 android.R.layout.simple_spinner_dropdown_item 사용합니다.
adapter = new ArrayAdapter<String>( this, android.R.layout.simple_spinner_item, color); adapter.setDropDownViewResource( android.R.layout.simple_spinner_dropdown_item);
▼ 다음은 스핀을 선택했을 때 바탕화면에 색깔을 적용하기 위한 이벤트를 등록합니다.
spinnerColor.setOnItemSelectedListener(new Spinner.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0,
View arg1, int arg2, long arg3) {
setBackgroundColor(spinnerColor.getSelectedItem().toString());
}
public void onNothingSelected(AdapterView<?> arg0) {
}
});
▼ 이렇게 스핀에 특정값을 선택했을 때 바탕화면의 색상을 바꾸는 전체 소스는 다음과 같습니다.
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.Toast;
public class AndroidGraphicsColorMainActivity extends Activity {
LinearLayout background;
Spinner spinnerColor;
private static final String[] color = {
"BLACK", "BLUE","CYAN", "DKGRAY", "GRAY",
"GREEN", "LTGRAY", "MAGENTA","RED",
"TRANSPARENT", "WHITE", "YELLOW" };
private ArrayAdapter<String> adapter;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_android_graphics_color_main);
background = (LinearLayout) findViewById(R.id.background);
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, color);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerColor = (Spinner) findViewById(R.id.colorspinner);
spinnerColor.setAdapter(adapter);
spinnerColor.setSelection(0);
spinnerColor.setOnItemSelectedListener(new Spinner.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
setBackgroundColor(spinnerColor.getSelectedItem().toString());
}
public void onNothingSelected(AdapterView<?> arg0) {
}
});
}
private void setBackgroundColor(String strColor) {
Toast.makeText(this, strColor, Toast.LENGTH_LONG).show();
if (strColor == "BLACK") {
background.setBackgroundColor(Color.BLACK);
} else if (strColor == "BLUE") {
background.setBackgroundColor(Color.BLUE);
} else if (strColor == "CYAN") {
background.setBackgroundColor(Color.CYAN);
} else if (strColor == "DKGRAY") {
background.setBackgroundColor(Color.DKGRAY);
} else if (strColor == "GRAY") {
background.setBackgroundColor(Color.GRAY);
} else if (strColor == "GREEN") {
background.setBackgroundColor(Color.GREEN);
} else if (strColor == "LTGRAY") {
background.setBackgroundColor(Color.LTGRAY);
} else if (strColor == "MAGENTA") {
background.setBackgroundColor(Color.MAGENTA);
} else if (strColor == "RED") {
background.setBackgroundColor(Color.RED);
} else if (strColor == "TRANSPARENT") {
background.setBackgroundColor(Color.TRANSPARENT);
} else if (strColor == "WHITE") {
background.setBackgroundColor(Color.WHITE);
} else if (strColor == "YELLOW") {
background.setBackgroundColor(Color.YELLOW);
}
}
}
▼ 메인 activity 을 꾸미는 전체 레이아웃 xml 입니다.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:background="#3F0099"
android:paddingBottom="@dimen/abc_action_bar_icon_vertical_padding"
android:paddingTop="@dimen/abc_action_bar_icon_vertical_padding"
android:text="Android.graphics.Color 컬러들 화면에 적용"
android:textColor="#FFFFFF" />
<LinearLayout
android:id="@+id/background"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Spinner
android:id="@+id/colorspinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
반응형
'안드로이드 개발' 카테고리의 다른 글
| 안드로이드(Android) ListActivity 를 이용해여 간단한 목록형 화면만들기 (0) | 2014.12.28 |
|---|---|
| 안드로이드(Android) 백그라운드 스레드 소스 Runnable 이용해 구현 (0) | 2014.12.26 |
| 안드로이드(Android) 백그라운드에서 Handler 를 통해 UI 변경 (1) | 2014.12.23 |
| 안드로이드(Android) This Handler class should be static or leaks might occur 해결 (6) | 2014.12.21 |
| 안드로이드(Android) Activity 의 라이프사이클(LifeCycle) 알아보기위한 예제 (0) | 2014.12.16 |
| 안드로이드(Android) SurfaceView 와 Thread 이용해 화면에서 움직이는 공구현 (0) | 2014.12.12 |
| 안드로이드(Android) SurfaceView 와 Thread 를 이용해여 사각형자동그리기 (0) | 2014.12.09 |
| 안드로이드(Android) getWidth() from the type Display is deprecated 수정 (0) | 2014.12.02 |
