RotaryEncoder
public
class
RotaryEncoder
extends Object
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
Inherited methods |
From
class
java.lang.Object
Object
|
clone()
|
boolean
|
equals(Object arg0)
|
void
|
finalize()
|
final
Class<?>
|
getClass()
|
int
|
hashCode()
|
final
void
|
notify()
|
final
void
|
notifyAll()
|
String
|
toString()
|
final
void
|
wait(long arg0, int arg1)
|
final
void
|
wait(long arg0)
|
final
void
|
wait()
|
|
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. |
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.
|