[Android] Vibration 진동

Twaun·2022년 9월 24일
2

Android

목록 보기
23/24

🤔 디바이스의 진동을 발생시켜보자!!

디바이스의 API에 따라 제공하는 클래스가 다르고 이에 따라 구현 방법이 조금씩 다르다. 아래와 같이 버전에 따라 3가지 방법이 모두 다르다.

  • BASE(API 1) ~ O(API 26)
  • O(API 26) ~ R(API 30)
  • S(API 31) ~ 현재

그럼 각 API 버전에 따라 구현 방법이 어떻게 달라지고 개선되었는지 알아보도록 하자!!

구현 방법

우선 AndroidManifest에서 진동 권한을 허용해야한다.

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

API 버전에 따라 다르지만 기본적인 구현 방법은 동일하다.

API 1~30 까지는 Vibrator 클래스를 제공했고 이후 API 31 부터는 VibratorManager 클래스를 제공한다.

진동 효과에 다음과 같은 설정에 가능하다.

  • 대기시간
  • 진동시간
  • 진동 세기
  • 반복 여부

버전마다의 차이가 이 설정들을 어떻게 하는지에 대한 차이라고 볼 수 있다.

설정이 끝나면 vibrate() 함수를 통해서 진동을 발생시킬 수 있다. 버전마다 인자를 다르게 받는다.

반복을 하는 경우에는 cancel() 함수를 통해서 진동을 취소할 수 있고 모든 버전이 동일하다.

O(API 26) 이전

진동 객체 선언은 API 31 이전에는 Vibrator를, API 31 이후에는 VibratorManager 를 사용한다.

val vibrator = context.getSystemService(Context.VIBRATOR_SERVICE) as Vibrator

1. 입력 시간 만큼 한번 진동

입력받은 시간(milisecond 단위) 만큼 1회 진동을 발생한다.

vibrator.vibrate(250)

2. (패턴 + 반복) 진동 발생

패턴은 long 타입의 배열로 짝수 인덱스(0 포함)는 대기시간(ms), 홀수 인덱스는 진동시간(ms)을 뜻한다.

val timing = longArrayOf(100, 100, 100, 150, 100, 200)

vibrator.vibrate(timing, -1)	// 반복X
vibrator.vibrate(timing, 0)		// 반복O

즉, 위 패턴은 100ms 만큼의 시간 간격으로 차례대로 100ms, 150ms, 200ms 시간 만큼의 진동이 울린다.

두 번쨰 인자로는 최소 한번의 실행 이후에 반복 여부를 결정한다.

O(API 26) ~ R(API 30) 사이

API 26 이후에는 VibrationEffect 이 등장한다.
안드로이드 공식문서 에서는 이것을 다음과 같이 설명한다.

A VibrationEffect describes a haptic effect to be performed by a Vibrator. These effects may be any number of things, from single shot vibrations to complex waveforms.

하나의 진동 객체에 single shot 부터 complex waveforms 까지 다양한 효과를 줄 수 있다고 하고 이에 대응하는 두 함수를 제공한다.

  • VibrationEffect.createOneShot()
  • VibrationEffect.createWaveform()

1. createOneShot()

말 그대로 한번 진동하는 효과를 만드는데, 진동 시간과 세기를 설정할 수 있다.

// createOneShot(long milliseconds, int amplitude)

val effect = VibrationEffect.createOneShot(100L, VibrationEffect.DEFAULT_AMPLITUDE)
vibrator.vibrate(effect)

진동 세기(amplitude)로 1~255 값을 입력할 수 있고, VibrationEffect에서 정해진 값을 사용할 수 있다.

public static final int DEFAULT_AMPLITUDE = -1;
public static final int EFFECT_CLICK = 0;
public static final int EFFECT_DOUBLE_CLICK = 1;
public static final int EFFECT_HEAVY_CLICK = 5;
public static final int EFFECT_TICK = 2;

2. createWaveform()

한번의 진동이 아닌 여러 진동으로 구성된 진동 웨이브 효과를 만든다.

// createWaveform(long[] timings, int[] amplitudes, int repeat)

val timings = longArrayOf(100, 200, 100, 200, 100, 200)
val amplitudes = intArrayOf(0, 50, 0, 100, 0, 200)

val effect = VibrationEffect.createWaveform(timings, amplitudes, 0)
vibrator.vibrate(effect)

함수를 구성하는 인자로 차례대로,

timings(long[])
: 대기시간, 진동시간으로 구성된 배열(이전 버전과 동일)

amplitudes(int[])
: 진동 세기를 의미하고 timings의 인덱스와 일치하게 구성해야한다.

repeat(int)
: 위 배열들로 설정된 효과의 반복 여부를 의미한다. 단, 이전 버전과 다른점은 단순히 반복여부를 따지는 것이 아니고 반복이 다시 시작되는 index를 정할 수 있다는 것이다. -1은 동일하게 반복X 를 의미하고, 반복을 원할 때는 최초 한번의 실행 이후 반복이 시작될 index를 작성하면 된다.

위 effect를 예시로 repeat에 따라 진행되는 index를 보며 이해를 돕자.
repeat : -1 / effect(index) : 012345
repeat : 0 / effect(index) : 012345 012345 012345 ... (반복)
repeat : 4 / effect(index) : 012345 45 45 45 ...(반복)

S(API 31) 이상

API 31 부터는 Vibrator 가 deprecated 되고 VibratorManager 를 제공한다.

val vibrator = context.getSystemService(Context.VIBRATOR_MANAGER_SERVICE) as VibratorManager

VibrationEffect에 이어서 CombinedVibration 이 새로 등장하고 다음과 같이 설명한다.

A CombinedVibration describes a combination of haptic effects to be performed by one or more Vibrators. These effects may be any number of things, from single shot vibrations to complex waveforms.

하나 이상의 진동 객체에 single shot 부터 complex waveforms 까지 다양한 효과를 줄 수 있다.

🤨 어라? 이전의 VibrationEffect과 무슨 차이지??

VibrationEffect는 하나의 진동 객체에 대해서 적용이 가능했다면 CombinedVibration는 하나 이상의 객체에 적용이 가능하게 하였다.

CombinedVibration 가 제공하는 createParallel()의 기능을 공식문서에서는 다음과 같이 말하고 있다.

Create a vibration that plays a single effect in parallel on all vibrators. A parallel vibration that takes a single VibrationEffect to be performed by multiple vibrators at the same time.

🧐 그렇다. CombinedVibration 은 여러 진동 객체에 대해 같은 VibrationEffect를 줄 수 있도록 하는 것이다.

그럼 이제 사용 방법에 대해 알아보자. 사실 큰 차이는 없다.

1. OneShot

이전 버전에 VibrationEffect를 생성하는 것까지 동일하고 이 VibrationEffect를 CombinedVibration.createParallel 함수를 통해 CombinedVibration를 생성하고 vibrate() 함수를 실행한다.

val vibrationEffect = VibrationEffect.createOneShot(100L, VibrationEffect.DEFAULT_AMPLITUDE)
val combinedVibration = CombinedVibration.createParallel(vibrationEffect)
vibrator.vibrate(combinedVibration)

2. WaveForm

이 역시 동일하다.

val timings = longArrayOf(100, 200, 100, 200, 100, 200)
val amplitudes = intArrayOf(0, 50, 0, 100, 0, 200)
val vibrationEffect = VibrationEffect.createWaveform(timings, amplitudes, 0)

val combinedVibration = CombinedVibration.createParallel(vibrationEffect)
vibrator.vibrate(combinedVibration)
profile
Android Developer

0개의 댓글