展示视图文本重心

15

有没有办法在 Showcase View 中设置标题和消息的位置?我看到该库使用函数来查找最佳位置,但我想覆盖它。


1
请详细说明您想要表达的内容,展示您的代码,然后解释您遇到的问题。 - Chintan Soni
更新了操作。仍然没有找到解决方法。 - arayray
在 Showcase View 的 Github 上提交了一个问题。https://github.com/Espiandev/ShowcaseView/issues/113 - arayray
1个回答

8

我刚刚深入研究了ShowcaseView的源代码,似乎没有内置的方法来设置文本位置。所以我对该库进行了自己的修改。以下是我所做的更改:

ShowcaseView类中添加了以下字段:

private boolean hasManualPostion = false;
private int xPosition, yPosition, width;

然后在ShowcaseView类的内部类Builder中添加以下方法:

        public Builder hasManualPosition(boolean hasManualPosition) {
        showcaseView.hasManualPostion = hasManualPosition;
        return this;
    }

    public Builder xPostion(int xPostion) {
        showcaseView.xPosition = xPostion;
        return this;
    }

    public Builder yPostion(int yPostion) {
        showcaseView.yPosition = yPostion;
        return this;
    }

接下来,将这个方法加入到TextDrawer类中:

public void setTestPostionManually(int xPosition, int yPosition) {
    mBestTextPosition[0] = xPosition;
    mBestTextPosition[1] = yPosition;
}

最后,将 ShowcaseView 中的 onPreDraw 方法更改为以下内容:

    @Override
public boolean onPreDraw() {
    boolean recalculatedCling = showcaseAreaCalculator.calculateShowcaseRect(showcaseX, showcaseY, showcaseDrawer);
    boolean recalculateText = recalculatedCling || hasAlteredText;
    if (recalculateText) {
        textDrawer.calculateTextPosition(getMeasuredWidth(), getMeasuredHeight(), this, shouldCentreText);
        if (hasManualPostion) {
            textDrawer.setTestPostionManually(xPosition, yPosition);
        }
    }
    hasAlteredText = false;
    return true;
}

现在,您应该像这样创建ShowcaseView
sv = new ShowcaseView.Builder(this, true)
                .setTarget(target)
                .setContentTitle(R.string.showcase_main_title)
                .setContentText(R.string.showcase_main_message)
                .setStyle(R.style.CustomShowcaseTheme2)
                .setShowcaseEventListener(this)
                .hasManualPosition(true)
                .xPostion(0)
                .yPostion(240)
                .build();

享受吧!


这是哪个版本?有没有办法让它在旧版中运行? - harveyslash
1
修改是针对版本5.0.0的。 - Sadegh
你知道在5.0.0中如何实现手部动画吗? - harveyslash
2
你不能将这个传递给 onPreDraw...你怎么能不测试就把它交给提问者呢? - Odaym
1
@Odaym 也许当他编写代码时,还没有 CalculateTextOnPreDraw 类(现在包含 onPreDraw 方法)。现在,您只需将 this 替换为 ShowcaseView.this,它就可以完美地工作了。 - Ashish Tanna
太好了!那确实有帮助,但如何将文本居中显示在屏幕上呢?我不想通过硬编码提供坐标。 - Adnan

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