Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Spotlight Always on Corner point

Tags:

java

android

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 picture one

picture two

like image 839
Ali Radman Avatar asked Dec 17 '17 05:12

Ali Radman


2 Answers

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.

Make use of post method, which can be called from any view. Post method takes a runnable thread and makes sure that everything in this post runnable is executed once the view is properly inflated.

// 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();
like image 182
Prakash Avatar answered Oct 10 '22 16:10

Prakash


Your view was not ready yet, You have to call it in doOnPreDraw (I'm using core-ktx lib)

 binding.root.doOnPreDraw {
        showSpotlight() // call it here
 }
like image 1
toidv Avatar answered Oct 10 '22 16:10

toidv