RotaryEncoder

public class RotaryEncoder
extends Object

java.lang.Object
   ↳ android.support.wearable.input.RotaryEncoder


Collection of helper methods for processing rotary encoder events. Most widgets already support these events, but custom behavior can be added if desired. To do so, either override onGenericMotionEvent(MotionEvent) or specify a custom handler via setOnGenericMotionListener(OnGenericMotionListener).

 public class MyCustomView extends View {
   // ...

   @Override
   public boolean onGenericMotionEvent(MotionEvent ev) {
     if (ev.getAction() == MotionEvent.ACTION_SCROLL
         && RotaryEncoder.isFromRotaryEncoder(ev)) {
       // Note that we negate the delta value here in order to get the right scroll direction.
       float delta = -RotaryEncoder.getRotaryAxisValue(ev)
           * RotaryEncoder.getScaledScrollFactor(getContext());
       scrollBy(0, Math.round(delta));
       return true;
     }
     return super.onGenericMotionEvent(ev);
   }

   // ...
 }
 
 

Summary

Public methods

static float getRotaryAxisValue(MotionEvent ev)
static float getScaledScrollFactor(Context context)
static boolean isFromRotaryEncoder(MotionEvent ev)

Checks whether a MotionEvent was generated by a rotary encoder.

Inherited methods

From class java.lang.Object

Public methods

getRotaryAxisValue

float getRotaryAxisValue (MotionEvent ev)

Parameters
ev MotionEvent

Returns
float The raw axis value (i.e. the amount the encoder was rotated) if the passed event is from a rotary encoder, and zero otherwise. This value should be multiplied against getScaledScrollFactor(Context) to determine the distance to scroll in pixels.

getScaledScrollFactor

float getScaledScrollFactor (Context context)

Parameters
context Context

Returns
float The scaling factor that rotary axis values should be multiplied against to determine how many pixels to scroll.

isFromRotaryEncoder

boolean isFromRotaryEncoder (MotionEvent ev)

Checks whether a MotionEvent was generated by a rotary encoder. Does not check the action type of the event; you should do this as well, e.g. event.getAction() == MotionEvent.ACTION_SCROLL. At the moment, rotary encoders only generate ACTION_SCROLL events, but they may generate other types of events in the future.

Parameters
ev MotionEvent

Returns
boolean True if this event was generated by a rotary encoder.