Added in API level 26

VibrationEffect


abstract class VibrationEffect : Parcelable
kotlin.Any
   ↳ android.os.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.

Summary

Nested classes

A builder for waveform effects defined by their envelope, designed to provide a consistent haptic perception across devices with varying capabilities.

A composition of haptic elements that are combined to be playable as a single VibrationEffect.

A builder for waveform effects described by its envelope.

Constants
static Int

The default vibration strength of the device.

static Int

A click effect.

static Int

A double click effect.

static Int

A heavy click effect.

static Int

A tick effect.

Inherited constants
Public methods
open static VibrationEffect!
createOneShot(milliseconds: Long, amplitude: Int)

Create a one shot vibration.

open static VibrationEffect
createPredefined(effectId: Int)

Create a predefined vibration effect.

open static VibrationEffect

Creates a new VibrationEffect that repeats the given effect indefinitely.

open static VibrationEffect

Creates a new VibrationEffect by merging the preamble and repeating vibration effect.

open static VibrationEffect!
createWaveform(timings: LongArray!, repeat: Int)

Create a waveform vibration, using only off/on transitions at the provided time intervals, and potentially repeating.

open static VibrationEffect!
createWaveform(timings: LongArray!, amplitudes: IntArray!, repeat: Int)

Create a waveform vibration.

open Int

open static VibrationEffect.Composition

Start composing a haptic effect.

Inherited functions
Properties
static Parcelable.Creator<VibrationEffect!>

Constants

DEFAULT_AMPLITUDE

Added in API level 26
static val DEFAULT_AMPLITUDE: Int

The default vibration strength of the device.

Value: -1

EFFECT_CLICK

Added in API level 29
static val EFFECT_CLICK: Int

A click effect. Use this effect as a baseline, as it's the most common type of click effect.

Value: 0

EFFECT_DOUBLE_CLICK

Added in API level 29
static val EFFECT_DOUBLE_CLICK: Int

A double click effect.

Value: 1

EFFECT_HEAVY_CLICK

Added in API level 29
static val EFFECT_HEAVY_CLICK: Int

A heavy click effect. This effect is stronger than EFFECT_CLICK.

Value: 5

EFFECT_TICK

Added in API level 29
static val EFFECT_TICK: Int

A tick effect. This effect is less strong compared to EFFECT_CLICK.

Value: 2

Public methods

createOneShot

Added in API level 26
open static fun createOneShot(
    milliseconds: Long,
    amplitude: Int
): VibrationEffect!

Create a one shot vibration.

One shot vibrations will vibrate constantly for the specified period of time at the specified amplitude, and then stop.

Parameters
milliseconds Long: The number of milliseconds to vibrate. This must be a positive number.
amplitude Int: The strength of the vibration. This must be a value between 1 and 255, or DEFAULT_AMPLITUDE.
Return
VibrationEffect! The desired effect.

createPredefined

Added in API level 29
open static fun createPredefined(effectId: Int): VibrationEffect

Create a predefined vibration effect.

Predefined effects are a set of common vibration effects that should be identical, regardless of the app they come from, in order to provide a cohesive experience for users across the entire device. They also may be custom tailored to the device hardware in order to provide a better experience than you could otherwise build using the generic building blocks.

This will fallback to a generic pattern if one exists and there does not exist a hardware-specific implementation of the effect.

Parameters
effectId Int: The ID of the effect to perform: EFFECT_CLICK, EFFECT_DOUBLE_CLICK, EFFECT_TICK Value is android.os.VibrationEffect#EFFECT_TICK, android.os.VibrationEffect#EFFECT_CLICK, android.os.VibrationEffect#EFFECT_HEAVY_CLICK, or android.os.VibrationEffect#EFFECT_DOUBLE_CLICK
Return
VibrationEffect The desired effect. This value cannot be null.

createRepeatingEffect

open static fun createRepeatingEffect(effect: VibrationEffect): VibrationEffect

Creates a new VibrationEffect that repeats the given effect indefinitely.

The input vibration must not be a repeating vibration. If it is, an IllegalArgumentException will be thrown.

Parameters
effect VibrationEffect: The VibrationEffect that will be repeated. This value cannot be null.
Return
VibrationEffect A VibrationEffect that repeats the effect indefinitely. This value cannot be null.
Exceptions
java.lang.IllegalArgumentException if the effect is already a repeating vibration.

createRepeatingEffect

open static fun createRepeatingEffect(
    preamble: VibrationEffect,
    repeatingEffect: VibrationEffect
): VibrationEffect

Creates a new VibrationEffect by merging the preamble and repeating vibration effect.

Neither input vibration may already be repeating. An IllegalArgumentException will be thrown if either input vibration is set to repeat indefinitely.

Parameters
preamble VibrationEffect: The starting vibration effect, which must be finite. This value cannot be null.
repeatingEffect VibrationEffect: The vibration effect to be repeated indefinitely after the preamble. This value cannot be null.
Return
VibrationEffect A VibrationEffect that plays the preamble once followed by the `repeatingEffect` indefinitely. This value cannot be null.
Exceptions
java.lang.IllegalArgumentException if either preamble or repeatingEffect is already a repeating vibration.

createWaveform

Added in API level 26
open static fun createWaveform(
    timings: LongArray!,
    repeat: Int
): VibrationEffect!

Create a waveform vibration, using only off/on transitions at the provided time intervals, and potentially repeating.

In effect, the timings array represents the number of milliseconds before turning the vibrator on, followed by the number of milliseconds to keep the vibrator on, then the number of milliseconds turned off, and so on. Consequently, the first timing value will often be 0, so that the effect will start vibrating immediately.

This method is equivalent to calling createWaveform(long[],int[],int) with corresponding amplitude values alternating between 0 and DEFAULT_AMPLITUDE, beginning with 0.

To cause the pattern to repeat, pass the index into the timings array at which to start the repetition, or -1 to disable repeating. Repeating effects will be played indefinitely and should be cancelled via Vibrator.cancel().

Parameters
timings LongArray!: The pattern of alternating on-off timings, starting with an 'off' timing, and representing the length of time to sustain the individual item (not cumulative).
repeat Int: The index into the timings array at which to repeat, or -1 if you don't want to repeat indefinitely.
Return
VibrationEffect! The desired effect.

createWaveform

Added in API level 26
open static fun createWaveform(
    timings: LongArray!,
    amplitudes: IntArray!,
    repeat: Int
): VibrationEffect!

Create a waveform vibration.

Waveform vibrations are a potentially repeating series of timing and amplitude pairs, provided in separate arrays. For each pair, the value in the amplitude array determines the strength of the vibration and the value in the timing array determines how long it vibrates for, in milliseconds.

To cause the pattern to repeat, pass the index into the timings array at which to start the repetition, or -1 to disable repeating. Repeating effects will be played indefinitely and should be cancelled via Vibrator.cancel().

Parameters
timings LongArray!: The timing values, in milliseconds, of the timing / amplitude pairs. Timing values of 0 will cause the pair to be ignored.
amplitudes IntArray!: The amplitude values of the timing / amplitude pairs. Amplitude values must be between 0 and 255, or equal to DEFAULT_AMPLITUDE. An amplitude value of 0 implies the motor is off.
repeat Int: The index into the timings array at which to repeat, or -1 if you don't want to repeat indefinitely.
Return
VibrationEffect! The desired effect.

describeContents

Added in API level 26
open fun describeContents(): Int
Return
Int a bitmask indicating the set of special object types marshaled by this Parcelable object instance. Value is either 0 or android.os.Parcelable#CONTENTS_FILE_DESCRIPTOR

startComposition

Added in API level 30
open static fun startComposition(): VibrationEffect.Composition

Start composing a haptic effect.

Return
VibrationEffect.Composition This value cannot be null.

Properties

CREATOR

Added in API level 26
static val CREATOR: Parcelable.Creator<VibrationEffect!>