简体   繁体   English

Android Spotlight Always on Corner point

[英]Android Spotlight Always on Corner point

same the sample but in my project , spotlight always show in corner what should i do ?与示例相同,但在我的项目中,聚光灯总是显示在角落里,我该怎么办? or any offer for this same library?或同一图书馆的任何优惠?

here is my code这是我的代码

    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();

and pictures和图片图一

图二

Actually your spotlight target is getting created before the target view is actually inflated and created.实际上,您的聚光灯目标是在目标视图实际膨胀和创建之前创建的。 To overcome this issue, you must create your spotlight target only once the view is inflated.要解决此问题,您必须仅在视图膨胀后创建聚光灯目标。

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;

so in above code sample, oneX and oneY is coming as 0 and so your spotlight target is moving to top-left corner and only a quarter is visible.所以在上面的代码示例中,oneX 和 oneY 变为 0,因此您的聚光灯目标移动到左上角,只有四分之一可见。

Make use of post method, which can be called from any view.使用可以从任何视图调用的 post 方法。 Post method takes a runnable thread and makes sure that everything in this post runnable is executed once the view is properly inflated. 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();

Your view was not ready yet, You have to call it in doOnPreDraw (I'm using core-ktx lib)您的视图尚未准备好,您必须在doOnPreDraw调用它(我使用的是 core-ktx lib)

 binding.root.doOnPreDraw {
        showSpotlight() // call it here
 }

The reason behind this is the view (element which you want to highlight) is not yet drawn into your root layout and you are trying to add spotlight on that.这背后的原因是视图(您要突出显示的元素)尚未绘制到您的根布局中,而您正试图在其上添加聚光灯。

The solution is below which solved my problem.下面的解决方案解决了我的问题。

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();

    }
});

Find your root layout from your xml file then从您的 xml 文件中找到您的根布局,然后

rootlayout.post { //write your code for creating targets and spotlight } rootlayout.post { // 编写用于创建目标和聚光灯的代码 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM