Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the purpose of Drawable setHotspot on Android 5.0 (API 21)?

Looking at the Drawable docs, we have a new method setHotspot (float x, float y) with the description of:

Specifies the hotspot's location within the drawable.

With no other explanations on that page, I wonder what the purpose is.

like image 752
Randy Sugianto 'Yuku' Avatar asked Oct 20 '14 08:10

Randy Sugianto 'Yuku'


1 Answers

Hotspots are used to pipe touch events into RippleDrawable, but can be used by custom drawables as well. If you are implementing a custom View that manages its own drawables, you will need to call setHotspot() from the drawableHotspotChanged() method for touch-centered ripples to work correctly.

From View.java:

@Override
public boolean onTouchEvent(MotionEvent event) {
    ...
            case MotionEvent.ACTION_MOVE:
                drawableHotspotChanged(x, y);
    ...
}


/**
 * This function is called whenever the view hotspot changes and needs to
 * be propagated to drawables managed by the view.
 * <p>
 * Be sure to call through to the superclass when overriding this function.
 *
 * @param x hotspot x coordinate
 * @param y hotspot y coordinate
 */
public void drawableHotspotChanged(float x, float y) {
    if (mBackground != null) {
        mBackground.setHotspot(x, y);
    }
}

From FrameLayout.java, which manages its own mForeground drawable:

@Override
public void drawableHotspotChanged(float x, float y) {
    super.drawableHotspotChanged(x, y);

    if (mForeground != null) {
        mForeground.setHotspot(x, y);
    }
}
like image 50
alanv Avatar answered Sep 25 '22 07:09

alanv