gpt4 book ai didi

java - Android Spotlight 始终在拐角处

转载 作者:行者123 更新时间:2023-12-01 13:47:02 25 4
gpt4 key购买 nike

与 sample 相同
但在我的项目中,聚光灯总是出现在角落里
我应该怎么办 ?
或同一个图书馆的任何报价?

这是我的代码

    View one = findViewById(R.id.img_drawer);
int[] oneLocation = new int[2];
one.getLocationInWindow(oneLocation);
float oneX = oneLocation[0] + one.getWidth() / 2f;
float oneY = oneLocation[1] + one.getHeight() / 2f;
// make an target
SimpleTarget firstTarget = new SimpleTarget.Builder(TripActivity.this).setPoint(oneX, oneY)
.setRadius(100f)
.setTitle("first title")
.setDescription("first description")
.build();
Spotlight.with(TripActivity.this)
.setDuration(1000L)
.setAnimation(new DecelerateInterpolator(2f))
.setTargets(firstTarget)
.setOnSpotlightStartedListener(new OnSpotlightStartedListener() {
@Override
public void onStarted() {
Toast.makeText(TripActivity.this, "spotlight is started", Toast.LENGTH_SHORT)
.show();
}
})
.setOnSpotlightEndedListener(new OnSpotlightEndedListener() {
@Override
public void onEnded() {
Toast.makeText(TripActivity.this, "spotlight is ended", Toast.LENGTH_SHORT).show();
}
})
.start();

和图片
picture one

picture two

最佳答案

实际上,您的聚光灯目标是在目标 View 实际膨胀和创建之前创建的。要克服这个问题,您必须仅在 View 膨胀后创建聚光灯目标。

View one = findViewById(R.id.img_drawer);
int[] oneLocation = new int[2];
one.getLocationInWindow(oneLocation);
float oneX = oneLocation[0] + one.getWidth() / 2f;
float oneY = oneLocation[1] + one.getHeight() / 2f;

所以在上面的代码示例中, oneX 和 oneY 为 0,所以你的聚光灯目标移动到左上角,只有四分之一是可见的。

使用 post 方法,可以从任何 View 调用。 Post 方法采用一个可运行线程,并确保在 View 正确膨胀后执行此可运行后的所有内容。
// create globally
SimpleTarget firstTarget = null;

// do this in onCreate of Activity or in onCreateView of Fragment
View one = findViewById(R.id.img_drawer);

one.post(new Runnable(){
@Override
public void run(){
int[] oneLocation = new int[2];
one.getLocationInWindow(oneLocation);
float oneX = oneLocation[0] + one.getWidth() / 2f;
float oneY = oneLocation[1] + one.getHeight() / 2f;

// make a target
firstTarget = new SimpleTarget.Builder(TripActivity.this)
.setPoint(oneX, oneY)
.setRadius(100f)
.setTitle("first title")
.setDescription("first description")
.build();
}
});

// Do this in onStart of Activity or in onViewCreated of Fragment
if (firstTarget != null)
Spotlight.with(TripActivity.this)
.setDuration(1000L)
.setAnimation(new DecelerateInterpolator(2f))
.setTargets(firstTarget)
.setOnSpotlightStartedListener(new OnSpotlightStartedListener() {
@Override
public void onStarted() {
Toast.makeText(TripActivity.this, "spotlight is started", Toast.LENGTH_SHORT).show();
}
})
.setOnSpotlightEndedListener(new OnSpotlightEndedListener() {
@Override
public void onEnded() {
Toast.makeText(TripActivity.this, "spotlight is ended", Toast.LENGTH_SHORT).show();
}
})
.start();

关于java - Android Spotlight 始终在拐角处,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47852594/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com