Android 5.0 (API 21)中,Drawable setHotspot的目的是什么?

10

浏览Drawable文档,我们发现一个新的方法setHotspot (float x, float y),它的描述是:

指定了可绘制对象中热点的位置。

在该页面没有其他解释,我想知道它的目的是什么。


我找到了这篇文章(但它有点旧):http://blahti.wordpress.com/2012/06/26/images-with-clickable-areas/ 或许这个热区有相同的意义... - krossovochkin
凭感觉,这是作为触摸感应设置的点的坐标吗? - Phantômaxx
1个回答

13

热点用于将触摸事件传输到RippleDrawable中,但也可以由自定义的drawable使用。如果您正在实现一个管理自己绘制的自定义View,您需要在drawableHotspotChanged()方法中调用setHotspot(),以便触摸中心的涟漪效果能够正确工作。

来自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);
    }
}

来自FrameLayout.java,它管理自己的mForeground drawable:

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

    if (mForeground != null) {
        mForeground.setHotspot(x, y);
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接