java - Android Spotlight 始终在拐角处

标签 java android

与 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/

相关文章:

java - 在oracle数据库中持久化的同时处理并发请求?

Android MVVM - 数据更改时更新 ViewModel

android - 菜单在 fragment 的 onCreateOptionsMenu 中多次膨胀调用

java - Spring AOP : Choosing advice seletively

java - 链表数据结构的同步问题?

java - 为什么使用 Gui 输入和监听器方法会出现 Null 指针异常?

java - 线程执行后刷新TableView

android - 如何在网站上检索图像的网址

android - 什么是 SharedPreferencesCompat?与 SharedPreferences

android - 根据地球上当前时间和位置计算太阳光方向