同样的样本 但在我的项目中,聚光灯始终显示在角落里 我该怎么办 ? 或同一个图书馆的任何报价?
这是我的代码
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();
答案 0 :(得分:0)
实际上,实际上是在实际放大和创建目标视图之前创建了Spotlight目标。为解决此问题,只有在视图变大后,才必须创建聚光灯目标。
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方法,可以从任何视图调用该方法。 Post方法采用一个可运行的线程,并确保一旦视图适当膨胀,此post可运行的所有内容都将被执行。
// 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();
答案 1 :(得分:0)
您的视图尚未准备好,您必须在doOnPreDraw
中调用它(我正在使用core-ktx lib)
binding.root.doOnPreDraw {
showSpotlight() // call it here
}
答案 2 :(得分:0)
其背后的原因是视图(要突出显示的元素)尚未绘制到根布局中,因此您试图在该视图上添加聚光灯。
下面的解决方案解决了我的问题。
view.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override public void onGlobalLayout() {
view.getViewTreeObserver().removeOnGlobalLayoutListener(this);
SimpleTarget target = new SimpleTarget.Builder(activity)
.setPoint(view)
.setRadius(radius)
.setTitle(title)
.setDescription(description)
.build();
Spotlight.with(activity)
.setDuration(500L)
.setDuration(500L)
.setAnimation(new DecelerateInterpolator(1f))
.setTargets(target)
.setOnSpotlightStartedListener(new OnSpotlightStartedListener() {
@Override
public void onStarted() {
}
})
.setOnSpotlightEndedListener(new OnSpotlightEndedListener() {
@Override
public void onEnded() {
App.getLocalStorage().write(sharedPrefKey, true);
}
})
.start();
}
});