RotarySelectorpublic class RotarySelector extends android.view.View Custom view that presents up to two items that are selectable by rotating a semi-circle from
left to right, or right to left. Used by incoming call screen, and the lock screen when no
security pattern is set. |
Fields Summary |
---|
public static final int | HORIZONTAL | public static final int | VERTICAL | private static final String | LOG_TAG | private static final boolean | DBG | private static final boolean | VISUAL_DEBUG | private static final android.media.AudioAttributes | VIBRATION_ATTRIBUTES | private OnDialTriggerListener | mOnDialTriggerListener | private float | mDensity | private android.graphics.Bitmap | mBackground | private android.graphics.Bitmap | mDimple | private android.graphics.Bitmap | mDimpleDim | private android.graphics.Bitmap | mLeftHandleIcon | private android.graphics.Bitmap | mRightHandleIcon | private android.graphics.Bitmap | mArrowShortLeftAndRight | private android.graphics.Bitmap | mArrowLongLeft | private android.graphics.Bitmap | mArrowLongRight | private int | mLeftHandleX | private int | mRightHandleX | private int | mRotaryOffsetX | private boolean | mAnimating | private long | mAnimationStartTime | private long | mAnimationDuration | private int | mAnimatingDeltaXStart | private int | mAnimatingDeltaXEnd | private android.view.animation.DecelerateInterpolator | mInterpolator | private android.graphics.Paint | mPaint | final android.graphics.Matrix | mBgMatrix | final android.graphics.Matrix | mArrowMatrix | private int | mGrabbedStateIf the user is currently dragging something. | public static final int | NOTHING_GRABBED | public static final int | LEFT_HANDLE_GRABBED | public static final int | RIGHT_HANDLE_GRABBED | private boolean | mTriggeredWhether the user has triggered something (e.g dragging the left handle all the way over to
the right). | private android.os.Vibrator | mVibrator | private static final long | VIBRATE_SHORT | private static final long | VIBRATE_LONG | private static final int | ARROW_SCRUNCH_DIPThe drawable for the arrows need to be scrunched this many dips towards the rotary bg below
it. | private static final int | EDGE_PADDING_DIPHow far inset the left and right circles should be | private static final int | EDGE_TRIGGER_DIPHow far from the edge of the screen the user must drag to trigger the event. | static final int | OUTER_ROTARY_RADIUS_DIPDimensions of arc in background drawable. | static final int | ROTARY_STROKE_WIDTH_DIP | static final int | SNAP_BACK_ANIMATION_DURATION_MILLIS | static final int | SPIN_ANIMATION_DURATION_MILLIS | private int | mEdgeTriggerThresh | private int | mDimpleWidth | private int | mBackgroundWidth | private int | mBackgroundHeight | private final int | mOuterRadius | private final int | mInnerRadius | private int | mDimpleSpacing | private android.view.VelocityTracker | mVelocityTracker | private int | mMinimumVelocity | private int | mMaximumVelocity | private int | mDimplesOfFlingThe number of dimples we are flinging when we do the "spin" animation. Used to know when to
wrap the icons back around so they "rotate back" onto the screen. | private int | mOrientationEither {@link #HORIZONTAL} or {@link #VERTICAL}. |
Constructors Summary |
---|
public RotarySelector(android.content.Context context)
this(context, null);
| public RotarySelector(android.content.Context context, android.util.AttributeSet attrs)Constructor used when this widget is created from a layout file.
super(context, attrs);
TypedArray a =
context.obtainStyledAttributes(attrs, R.styleable.RotarySelector);
mOrientation = a.getInt(R.styleable.RotarySelector_orientation, HORIZONTAL);
a.recycle();
Resources r = getResources();
mDensity = r.getDisplayMetrics().density;
if (DBG) log("- Density: " + mDensity);
// Assets (all are BitmapDrawables).
mBackground = getBitmapFor(R.drawable.jog_dial_bg);
mDimple = getBitmapFor(R.drawable.jog_dial_dimple);
mDimpleDim = getBitmapFor(R.drawable.jog_dial_dimple_dim);
mArrowLongLeft = getBitmapFor(R.drawable.jog_dial_arrow_long_left_green);
mArrowLongRight = getBitmapFor(R.drawable.jog_dial_arrow_long_right_red);
mArrowShortLeftAndRight = getBitmapFor(R.drawable.jog_dial_arrow_short_left_and_right);
mInterpolator = new DecelerateInterpolator(1f);
mEdgeTriggerThresh = (int) (mDensity * EDGE_TRIGGER_DIP);
mDimpleWidth = mDimple.getWidth();
mBackgroundWidth = mBackground.getWidth();
mBackgroundHeight = mBackground.getHeight();
mOuterRadius = (int) (mDensity * OUTER_ROTARY_RADIUS_DIP);
mInnerRadius = (int) ((OUTER_ROTARY_RADIUS_DIP - ROTARY_STROKE_WIDTH_DIP) * mDensity);
final ViewConfiguration configuration = ViewConfiguration.get(mContext);
mMinimumVelocity = configuration.getScaledMinimumFlingVelocity() * 2;
mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
|
Methods Summary |
---|
private void | dispatchTriggerEvent(int whichHandle)Dispatches a trigger event to our listener.
vibrate(VIBRATE_LONG);
if (mOnDialTriggerListener != null) {
mOnDialTriggerListener.onDialTrigger(this, whichHandle);
}
| private void | drawCentered(android.graphics.Bitmap d, android.graphics.Canvas c, int x, int y)Draw the bitmap so that it's centered
on the point (x,y), then draws it using specified canvas.
TODO: is there already a utility method somewhere for this?
int w = d.getWidth();
int h = d.getHeight();
c.drawBitmap(d, x - (w / 2), y - (h / 2), mPaint);
| private android.graphics.Bitmap | getBitmapFor(int resId)
return BitmapFactory.decodeResource(getContext().getResources(), resId);
| private int | getYOnArc(int backgroundWidth, int innerRadius, int outerRadius, int x)Assuming bitmap is a bounding box around a piece of an arc drawn by two concentric circles
(as the background drawable for the rotary widget is), and given an x coordinate along the
drawable, return the y coordinate of a point on the arc that is between the two concentric
circles. The resulting y combined with the incoming x is a point along the circle in
between the two concentric circles.
// the hypotenuse
final int halfWidth = (outerRadius - innerRadius) / 2;
final int middleRadius = innerRadius + halfWidth;
// the bottom leg of the triangle
final int triangleBottom = (backgroundWidth / 2) - x;
// "Our offense is like the pythagorean theorem: There is no answer!" - Shaquille O'Neal
final int triangleY =
(int) Math.sqrt(middleRadius * middleRadius - triangleBottom * triangleBottom);
// convert to drawing coordinates:
// middleRadius - triangleY =
// the vertical distance from the outer edge of the circle to the desired point
// from there we add the distance from the top of the drawable to the middle circle
return middleRadius - triangleY + halfWidth;
| private boolean | isHoriz()
return mOrientation == HORIZONTAL;
| private void | log(java.lang.String msg)
// Debugging / testing code
Log.d(LOG_TAG, msg);
| protected void | onDraw(android.graphics.Canvas canvas)
super.onDraw(canvas);
final int width = getWidth();
if (VISUAL_DEBUG) {
// draw bounding box around widget
mPaint.setColor(0xffff0000);
mPaint.setStyle(Paint.Style.STROKE);
canvas.drawRect(0, 0, width, getHeight(), mPaint);
}
final int height = getHeight();
// update animating state before we draw anything
if (mAnimating) {
updateAnimation();
}
// Background:
canvas.drawBitmap(mBackground, mBgMatrix, mPaint);
// Draw the correct arrow(s) depending on the current state:
mArrowMatrix.reset();
switch (mGrabbedState) {
case NOTHING_GRABBED:
//mArrowShortLeftAndRight;
break;
case LEFT_HANDLE_GRABBED:
mArrowMatrix.setTranslate(0, 0);
if (!isHoriz()) {
mArrowMatrix.preRotate(-90, 0, 0);
mArrowMatrix.postTranslate(0, height);
}
canvas.drawBitmap(mArrowLongLeft, mArrowMatrix, mPaint);
break;
case RIGHT_HANDLE_GRABBED:
mArrowMatrix.setTranslate(0, 0);
if (!isHoriz()) {
mArrowMatrix.preRotate(-90, 0, 0);
// since bg width is > height of screen in landscape mode...
mArrowMatrix.postTranslate(0, height + (mBackgroundWidth - height));
}
canvas.drawBitmap(mArrowLongRight, mArrowMatrix, mPaint);
break;
default:
throw new IllegalStateException("invalid mGrabbedState: " + mGrabbedState);
}
final int bgHeight = mBackgroundHeight;
final int bgTop = isHoriz() ?
height - bgHeight:
width - bgHeight;
if (VISUAL_DEBUG) {
// draw circle bounding arc drawable: good sanity check we're doing the math correctly
float or = OUTER_ROTARY_RADIUS_DIP * mDensity;
final int vOffset = mBackgroundWidth - height;
final int midX = isHoriz() ? width / 2 : mBackgroundWidth / 2 - vOffset;
if (isHoriz()) {
canvas.drawCircle(midX, or + bgTop, or, mPaint);
} else {
canvas.drawCircle(or + bgTop, midX, or, mPaint);
}
}
// left dimple / icon
{
final int xOffset = mLeftHandleX + mRotaryOffsetX;
final int drawableY = getYOnArc(
mBackgroundWidth,
mInnerRadius,
mOuterRadius,
xOffset);
final int x = isHoriz() ? xOffset : drawableY + bgTop;
final int y = isHoriz() ? drawableY + bgTop : height - xOffset;
if (mGrabbedState != RIGHT_HANDLE_GRABBED) {
drawCentered(mDimple, canvas, x, y);
drawCentered(mLeftHandleIcon, canvas, x, y);
} else {
drawCentered(mDimpleDim, canvas, x, y);
}
}
// center dimple
{
final int xOffset = isHoriz() ?
width / 2 + mRotaryOffsetX:
height / 2 + mRotaryOffsetX;
final int drawableY = getYOnArc(
mBackgroundWidth,
mInnerRadius,
mOuterRadius,
xOffset);
if (isHoriz()) {
drawCentered(mDimpleDim, canvas, xOffset, drawableY + bgTop);
} else {
// vertical
drawCentered(mDimpleDim, canvas, drawableY + bgTop, height - xOffset);
}
}
// right dimple / icon
{
final int xOffset = mRightHandleX + mRotaryOffsetX;
final int drawableY = getYOnArc(
mBackgroundWidth,
mInnerRadius,
mOuterRadius,
xOffset);
final int x = isHoriz() ? xOffset : drawableY + bgTop;
final int y = isHoriz() ? drawableY + bgTop : height - xOffset;
if (mGrabbedState != LEFT_HANDLE_GRABBED) {
drawCentered(mDimple, canvas, x, y);
drawCentered(mRightHandleIcon, canvas, x, y);
} else {
drawCentered(mDimpleDim, canvas, x, y);
}
}
// draw extra left hand dimples
int dimpleLeft = mRotaryOffsetX + mLeftHandleX - mDimpleSpacing;
final int halfdimple = mDimpleWidth / 2;
while (dimpleLeft > -halfdimple) {
final int drawableY = getYOnArc(
mBackgroundWidth,
mInnerRadius,
mOuterRadius,
dimpleLeft);
if (isHoriz()) {
drawCentered(mDimpleDim, canvas, dimpleLeft, drawableY + bgTop);
} else {
drawCentered(mDimpleDim, canvas, drawableY + bgTop, height - dimpleLeft);
}
dimpleLeft -= mDimpleSpacing;
}
// draw extra right hand dimples
int dimpleRight = mRotaryOffsetX + mRightHandleX + mDimpleSpacing;
final int rightThresh = mRight + halfdimple;
while (dimpleRight < rightThresh) {
final int drawableY = getYOnArc(
mBackgroundWidth,
mInnerRadius,
mOuterRadius,
dimpleRight);
if (isHoriz()) {
drawCentered(mDimpleDim, canvas, dimpleRight, drawableY + bgTop);
} else {
drawCentered(mDimpleDim, canvas, drawableY + bgTop, height - dimpleRight);
}
dimpleRight += mDimpleSpacing;
}
| protected void | onMeasure(int widthMeasureSpec, int heightMeasureSpec)
final int length = isHoriz() ?
MeasureSpec.getSize(widthMeasureSpec) :
MeasureSpec.getSize(heightMeasureSpec);
final int arrowScrunch = (int) (ARROW_SCRUNCH_DIP * mDensity);
final int arrowH = mArrowShortLeftAndRight.getHeight();
// by making the height less than arrow + bg, arrow and bg will be scrunched together,
// overlaying somewhat (though on transparent portions of the drawable).
// this works because the arrows are drawn from the top, and the rotary bg is drawn
// from the bottom.
final int height = mBackgroundHeight + arrowH - arrowScrunch;
if (isHoriz()) {
setMeasuredDimension(length, height);
} else {
setMeasuredDimension(height, length);
}
| protected void | onSizeChanged(int w, int h, int oldw, int oldh)
super.onSizeChanged(w, h, oldw, oldh);
final int edgePadding = (int) (EDGE_PADDING_DIP * mDensity);
mLeftHandleX = edgePadding + mDimpleWidth / 2;
final int length = isHoriz() ? w : h;
mRightHandleX = length - edgePadding - mDimpleWidth / 2;
mDimpleSpacing = (length / 2) - mLeftHandleX;
// bg matrix only needs to be calculated once
mBgMatrix.setTranslate(0, 0);
if (!isHoriz()) {
// set up matrix for translating drawing of background and arrow assets
final int left = w - mBackgroundHeight;
mBgMatrix.preRotate(-90, 0, 0);
mBgMatrix.postTranslate(left, h);
} else {
mBgMatrix.postTranslate(0, h - mBackgroundHeight);
}
| public boolean | onTouchEvent(android.view.MotionEvent event)Handle touch screen events.
if (mAnimating) {
return true;
}
if (mVelocityTracker == null) {
mVelocityTracker = VelocityTracker.obtain();
}
mVelocityTracker.addMovement(event);
final int height = getHeight();
final int eventX = isHoriz() ?
(int) event.getX():
height - ((int) event.getY());
final int hitWindow = mDimpleWidth;
final int action = event.getAction();
switch (action) {
case MotionEvent.ACTION_DOWN:
if (DBG) log("touch-down");
mTriggered = false;
if (mGrabbedState != NOTHING_GRABBED) {
reset();
invalidate();
}
if (eventX < mLeftHandleX + hitWindow) {
mRotaryOffsetX = eventX - mLeftHandleX;
setGrabbedState(LEFT_HANDLE_GRABBED);
invalidate();
vibrate(VIBRATE_SHORT);
} else if (eventX > mRightHandleX - hitWindow) {
mRotaryOffsetX = eventX - mRightHandleX;
setGrabbedState(RIGHT_HANDLE_GRABBED);
invalidate();
vibrate(VIBRATE_SHORT);
}
break;
case MotionEvent.ACTION_MOVE:
if (DBG) log("touch-move");
if (mGrabbedState == LEFT_HANDLE_GRABBED) {
mRotaryOffsetX = eventX - mLeftHandleX;
invalidate();
final int rightThresh = isHoriz() ? getRight() : height;
if (eventX >= rightThresh - mEdgeTriggerThresh && !mTriggered) {
mTriggered = true;
dispatchTriggerEvent(OnDialTriggerListener.LEFT_HANDLE);
final VelocityTracker velocityTracker = mVelocityTracker;
velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
final int rawVelocity = isHoriz() ?
(int) velocityTracker.getXVelocity():
-(int) velocityTracker.getYVelocity();
final int velocity = Math.max(mMinimumVelocity, rawVelocity);
mDimplesOfFling = Math.max(
8,
Math.abs(velocity / mDimpleSpacing));
startAnimationWithVelocity(
eventX - mLeftHandleX,
mDimplesOfFling * mDimpleSpacing,
velocity);
}
} else if (mGrabbedState == RIGHT_HANDLE_GRABBED) {
mRotaryOffsetX = eventX - mRightHandleX;
invalidate();
if (eventX <= mEdgeTriggerThresh && !mTriggered) {
mTriggered = true;
dispatchTriggerEvent(OnDialTriggerListener.RIGHT_HANDLE);
final VelocityTracker velocityTracker = mVelocityTracker;
velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
final int rawVelocity = isHoriz() ?
(int) velocityTracker.getXVelocity():
- (int) velocityTracker.getYVelocity();
final int velocity = Math.min(-mMinimumVelocity, rawVelocity);
mDimplesOfFling = Math.max(
8,
Math.abs(velocity / mDimpleSpacing));
startAnimationWithVelocity(
eventX - mRightHandleX,
-(mDimplesOfFling * mDimpleSpacing),
velocity);
}
}
break;
case MotionEvent.ACTION_UP:
if (DBG) log("touch-up");
// handle animating back to start if they didn't trigger
if (mGrabbedState == LEFT_HANDLE_GRABBED
&& Math.abs(eventX - mLeftHandleX) > 5) {
// set up "snap back" animation
startAnimation(eventX - mLeftHandleX, 0, SNAP_BACK_ANIMATION_DURATION_MILLIS);
} else if (mGrabbedState == RIGHT_HANDLE_GRABBED
&& Math.abs(eventX - mRightHandleX) > 5) {
// set up "snap back" animation
startAnimation(eventX - mRightHandleX, 0, SNAP_BACK_ANIMATION_DURATION_MILLIS);
}
mRotaryOffsetX = 0;
setGrabbedState(NOTHING_GRABBED);
invalidate();
if (mVelocityTracker != null) {
mVelocityTracker.recycle(); // wishin' we had generational GC
mVelocityTracker = null;
}
break;
case MotionEvent.ACTION_CANCEL:
if (DBG) log("touch-cancel");
reset();
invalidate();
if (mVelocityTracker != null) {
mVelocityTracker.recycle();
mVelocityTracker = null;
}
break;
}
return true;
| private void | reset()
mAnimating = false;
mRotaryOffsetX = 0;
mDimplesOfFling = 0;
setGrabbedState(NOTHING_GRABBED);
mTriggered = false;
| private void | setGrabbedState(int newState)Sets the current grabbed state, and dispatches a grabbed state change
event to our listener.
if (newState != mGrabbedState) {
mGrabbedState = newState;
if (mOnDialTriggerListener != null) {
mOnDialTriggerListener.onGrabbedStateChange(this, mGrabbedState);
}
}
| public void | setLeftHandleResource(int resId)Sets the left handle icon to a given resource.
The resource should refer to a Drawable object, or use 0 to remove
the icon.
if (resId != 0) {
mLeftHandleIcon = getBitmapFor(resId);
}
invalidate();
| public void | setOnDialTriggerListener(com.android.internal.widget.RotarySelector$OnDialTriggerListener l)Registers a callback to be invoked when the dial
is "triggered" by rotating it one way or the other.
mOnDialTriggerListener = l;
| public void | setRightHandleResource(int resId)Sets the right handle icon to a given resource.
The resource should refer to a Drawable object, or use 0 to remove
the icon.
if (resId != 0) {
mRightHandleIcon = getBitmapFor(resId);
}
invalidate();
| private void | startAnimation(int startX, int endX, int duration)
mAnimating = true;
mAnimationStartTime = currentAnimationTimeMillis();
mAnimationDuration = duration;
mAnimatingDeltaXStart = startX;
mAnimatingDeltaXEnd = endX;
setGrabbedState(NOTHING_GRABBED);
mDimplesOfFling = 0;
invalidate();
| private void | startAnimationWithVelocity(int startX, int endX, int pixelsPerSecond)
mAnimating = true;
mAnimationStartTime = currentAnimationTimeMillis();
mAnimationDuration = 1000 * (endX - startX) / pixelsPerSecond;
mAnimatingDeltaXStart = startX;
mAnimatingDeltaXEnd = endX;
setGrabbedState(NOTHING_GRABBED);
invalidate();
| private void | updateAnimation()
final long millisSoFar = currentAnimationTimeMillis() - mAnimationStartTime;
final long millisLeft = mAnimationDuration - millisSoFar;
final int totalDeltaX = mAnimatingDeltaXStart - mAnimatingDeltaXEnd;
final boolean goingRight = totalDeltaX < 0;
if (DBG) log("millisleft for animating: " + millisLeft);
if (millisLeft <= 0) {
reset();
return;
}
// from 0 to 1 as animation progresses
float interpolation =
mInterpolator.getInterpolation((float) millisSoFar / mAnimationDuration);
final int dx = (int) (totalDeltaX * (1 - interpolation));
mRotaryOffsetX = mAnimatingDeltaXEnd + dx;
// once we have gone far enough to animate the current buttons off screen, we start
// wrapping the offset back to the other side so that when the animation is finished,
// the buttons will come back into their original places.
if (mDimplesOfFling > 0) {
if (!goingRight && mRotaryOffsetX < -3 * mDimpleSpacing) {
// wrap around on fling left
mRotaryOffsetX += mDimplesOfFling * mDimpleSpacing;
} else if (goingRight && mRotaryOffsetX > 3 * mDimpleSpacing) {
// wrap around on fling right
mRotaryOffsetX -= mDimplesOfFling * mDimpleSpacing;
}
}
invalidate();
| private synchronized void | vibrate(long duration)Triggers haptic feedback.
final boolean hapticEnabled = Settings.System.getIntForUser(
mContext.getContentResolver(), Settings.System.HAPTIC_FEEDBACK_ENABLED, 1,
UserHandle.USER_CURRENT) != 0;
if (hapticEnabled) {
if (mVibrator == null) {
mVibrator = (android.os.Vibrator) getContext()
.getSystemService(Context.VIBRATOR_SERVICE);
}
mVibrator.vibrate(duration, VIBRATION_ATTRIBUTES);
}
|
|