Presentation is loading. Please wait.

Presentation is loading. Please wait.

Contents 학습목표 Canvas와 Paint 객체를 통해 화면에 원하는 도형을 그리고 속성을 변경하는 기본적인 방법에 대해 소개한다. 토스트로 메시지를 출력하는 방법과 스피커를 통해 소리를 출력하는 방법에 대해서도 알아본다. 학습내용 캔버스 그리기 객체 쉐이더 그외.

Similar presentations


Presentation on theme: "Contents 학습목표 Canvas와 Paint 객체를 통해 화면에 원하는 도형을 그리고 속성을 변경하는 기본적인 방법에 대해 소개한다. 토스트로 메시지를 출력하는 방법과 스피커를 통해 소리를 출력하는 방법에 대해서도 알아본다. 학습내용 캔버스 그리기 객체 쉐이더 그외."— Presentation transcript:

1

2 Contents 학습목표 Canvas와 Paint 객체를 통해 화면에 원하는 도형을 그리고 속성을 변경하는 기본적인 방법에 대해 소개한다. 토스트로 메시지를 출력하는 방법과 스피커를 통해 소리를 출력하는 방법에 대해서도 알아본다. 학습내용 캔버스 그리기 객체 쉐이더 그외 출력

3 5.4.1. 토스트 간단한 메시지 박스, 소리, 진동, 불빛, 시스템 로그기록 등 모두 출력.
토스트 : 짧은 문자열을 보여주는 작은 팝업 대화상자로 디버깅, 학습용으로 아주 유용하다. 변수값 수시로 찍을때는 메시지가 편리 토스트 생성 static Toast makeText(Context context, int resID, int duration) static Toast makeText(Context context, CharSequence text, int duration) // 컨텍스트로는 액티비티를 전달하므로 주로 this이며 이벤트 리스너 객체 안일 때는 this가 리스너를 칭하므로 액티비티명.this 로 지정한다. // 세번째 인수는 LENGTH_SHORT, LENGTH_LONG중 하나를지정 생성후 동작이나 배치 옵션을 변경할 수 있다. void setGravity (int gravity, int xOffset, int yOffset) void setMargin (flat horizontalMargin, float verticalMargin) void setText(CharSequence s) void setDuration (int duration) setView (View view)

4 토스트

5 5.4.1. 토스트 C05_Toast.xml <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <Button android:layout_height="wrap_content" android:text="짧은 메시지" /> android:text="긴 메시지" C05_Toast.xml <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="카운트 연속 출력" /> android:text="카운트 연속 출력2" android:text="커스텀 뷰 표시" </LinearLayout>

6 5.4.1. 토스트 C05_Toast.java public class MainActivity extends Activity {
Toast mToast = null; int count; String str; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); findViewById(R.id.shortmsg).setOnClickListener(mClickListener); findViewById(R.id.longmsg).setOnClickListener(mClickListener); findViewById(R.id.count1).setOnClickListener(mClickListener); findViewById(R.id.count2).setOnClickListener(mClickListener); findViewById(R.id.customview).setOnClickListener(mClickListener); } Button.OnClickListener mClickListener = new Button.OnClickListener() { public void onClick(View v) { switch (v.getId()) { case R.id.shortmsg: Toast.makeText(MainActivity.this, "잠시 나타나는 메시지", Toast.LENGTH_SHORT).show(); break; C05_Toast.java case R.id.longmsg: Toast.makeText(MainActivity.this, "조금 길게 나타나는 메시지", Toast.LENGTH_LONG).show(); break; case R.id.count1: str = "현재 카운트 = " + count++; if (mToast != null) { mToast.cancel(); } mToast = Toast.makeText(MainActivity.this, str, Toast.LENGTH_SHORT); mToast.show(); break; case R.id.count2: if (mToast == null) { } else { mToast.setText(str); } mToast.show(); break; case R.id.customview: LinearLayout linear = (LinearLayout)View.inflate(MainActivity.this, R.layout.activity_main, null); Toast t2 = new Toast(MainActivity.this); t2.setView(linear); t2.show(); } }};}

7 비프음 사운드는 이벤트의 발생 여부나 코드의 흐름을 분석하는데 유용하다. 디버깅은 빈번히 발생하는 이벤트를 확인하기에 번거롭지만 사운드는 흐름을 계속 유지 할 수 있다. MediaPlayer : 동영상 재생, 스트리밍까지 지원하므로 소리를 내는 단순한 비프음으로는 적당하지 않다. 비프음은 SoundPool, AudioManager 클래스면 충분하다. 안드로이드 지원 포멧 : wav, mp3, ogg res/raw 폴덜 : 재생할 소리 파일을 둔다.

8 비프음 SoundPool : 음원파일을 리소스 폴더에 복사한 후 SoundPool 객체의 load 메소드가 미리 읽어두고 play 메소드로 재생을 한다. 여러 소리를 한번에 낼수도 있고 구형 장비에서 소리가 두 번씩 재생되기도 함. SoundPool (int maxStreams, int streamType, int srcQuality) int play (int soundID, float leftVolume, float rightVolume, int priority, int loop,float rate) AudioManager : 원래 볼륨 조절이나 벨 모드 등을 조정하는 관리 클래스. 시스템이 미리 정의해 노은 소리 재생. 시스템 설정에 따라 소리가 나지 않을 수도 있다.

9 5.4.2. 비프음 C05_Beep.xml <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <Button android:layout_height="wrap_content" android:text="SoundPool" /> android:text="AudioManager" </LinearLayout>

10 5.4.2. 비프음 C05_Beep.xml <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <Button android:layout_height="wrap_content" android:text="SoundPool" /> android:text="AudioManager" </LinearLayout>

11 비프음 C05_Beep.java public class C05_Beep extends Activity { SoundPool mPool; int mDdok; AudioManager mAm; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.c05_beep); mPool = new SoundPool(1, AudioManager.STREAM_MUSIC, 0); mDdok = mPool.load(this, R.raw.ddok, 1); mAm = (AudioManager)getSystemService(AUDIO_SERVICE); findViewById(R.id.play1).setOnClickListener(mClickListener); findViewById(R.id.play2).setOnClickListener(mClickListener); } C05_Beep.java Button.OnClickListener mClickListener = new Button.OnClickListener() { public void onClick(View v) { MediaPlayer player; switch (v.getId()) { case R.id.play1: mPool.play(mDdok, 1, 1, 0, 0, 1); break; case R.id.play2: mAm.playSoundEffect(AudioManager.FX_KEYPRESS_STANDARD); } };

12 비프음 사운드 파일을 열 때는 다음 정적 메서드를 호출한다. Static MediaPlayer creat (Context context, int resid) 인수로는 켄텍스트와 리소스 ID를 전달한다. 리소스 파일이 raw 폴더에 저장되므로 ID는 통상 R.raw.id식으로 부여된다. 다음 메서드로 재생 및 중지를 한다. public void start () – 비동기 방식으로 동작 public void stop () public void seekTo (int msec) public void pause ()

13 진동 진동 : 사용자에게 사건의 발생을 알려주는 중요 수단. 진동 기능은 Vibrator 시스템 서비스에 의해 제공. Object Activity.getSystemService (String name) void vibrate (long milliseconds) void vibrate (long[] pattern, int repeat) void cancel () 진동 시간 1/1000초로 지정가능 진동의 패턴을 정수 배열로 정의한 후 반복 재생 가능 (배열 홀수 번호는 대기 시간, 짝수번호는 진동할 시간) repeat : 배열상의 진동 시작 위치를 지정 0이면 처음부터 반복. -1이면 반복하지 않음. cancel : 진동 중지

14 진동 진동은 장비의 특정 부품을 사용하는 기능이므로 사용하겠다는 선언 퍼미션(Permission)을 매니페스트에 요청해야 한다. 진동기능을 사용하려면 VIBRATE 퍼미션이 필요하다. AndroidManifest.xml에 꼭 선언해야 한다. <uses-permission android:name="android.permission.VIBRATE"/> </manifest>

15 비프음 C05_Vibrate.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android=" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="16sp" android:text="버튼을 누르면 진동합니다." /> <Button android:onClick="mOnClick" android:text="한번 진동" C05_Vibrate.xml <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:onClick="mOnClick" android:text="계속 진동" /> android:text="진동 중지" </LinearLayout>

16 5.4.2. 비프음 C05_Vibrate..java public void mOnClick(View v) {
public class C05_Vibrate extends Activity { Vibrator mVib; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.c05_vibrate); mVib = (Vibrator)getSystemService(Context.VIBRATOR_SERVICE); } protected void onDestroy() { super.onDestroy(); mVib.cancel(); C05_Vibrate..java public void mOnClick(View v) { switch (v.getId()) { case R.id.btnvibrate1: mVib.vibrate(500); break; case R.id.btnvibrate2: mVib.vibrate(new long[] {100, 50 , 200, 50 }, 0); case R.id.btnvibratestop: mVib.cancel(); }


Download ppt "Contents 학습목표 Canvas와 Paint 객체를 통해 화면에 원하는 도형을 그리고 속성을 변경하는 기본적인 방법에 대해 소개한다. 토스트로 메시지를 출력하는 방법과 스피커를 통해 소리를 출력하는 방법에 대해서도 알아본다. 학습내용 캔버스 그리기 객체 쉐이더 그외."

Similar presentations


Ads by Google