Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
313 views
in Technique[技术] by (71.8m points)

android - onOffsetsChanged not called by Touchwiz

Note: since it is a question specific to Samsung, I also asked it on their developer board.

I am currently implementing a Live Wallpaper in Android and I'm listening to the onOffsetsChanged() method in my wallpaper engine to change the wallpaper when the user swipes through his homescreens. This works great on my private Galaxy Tab with a CM9 custom rom. On my company device, a stock Galaxy S3, it does not work. Somehow Touchwiz doesn't call onOffsetsChanged when the homescreen is changed.

Googling for the topic didn't yield any significant results besides the description of this app, where the dev states: "Fixed scrolling on latest TouchWiz launcher where onOffsetsChanged() doesn't get called." Now I would just contact the dev, but unfortunately it is also a Samsung app.

Does anyone know a workaround to get the current offsets without relying on onOffsetsChanged? Has anyone run into this problem in their own wallpaper? Does anyone know if this is intentional or if I can assume that future Touchwiz versions will make use of the method again?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Some developers are using touch events instead of system onOffsetsChanged() to work with TouchWiz. I think, currently the only better way is using the hybrid event system, which will work in such way:

1) Always assume that onOffsetsChanged() message is not sent correctly (make boolean property defaulting to false).
2) This means that you should implement onTouch() method to make the proper imitation of onOffsetsChanged(). Listen to onTouch() only if the boolean property is still false.
3) When onOffsetsChanged() is called, check the xOffset param. If it's neither 0.0f nor 0.5f, then change the boolean property to true and listen only to onOffsetsChanged().

Code will be sth like:

public class myEngine extends WallpaperService.Engine {
    private boolean offsetChangedWorking = false;

    public void onOffsetsChanged (float xOffset, float yOffset, float xOffsetStep, float yOffsetStep, int xPixelOffset, int yPixelOffset) {
        if (offsetChangedWorking == false && xOffset != 0.0f && xOffset != 0.5f) {
            offsetChangedWorking = true;
        }

        if (offsetChangedWorking == true) {
            // Do sth here
        }
    }

    public void onTouchEvent(MotionEvent paramMotionEvent) {
        if (offsetChangedWorking == false) {
            // Do sth else here
        }
    }
}

This code is only an illustration. Note, that comparing floats with == is not correct, but it might work in this case.

Also, it looks like Samsung Parallax LWPs are working the same way. If you have a device with TouchWiz and some other properly working launcher (which sends onOffsetsChanged() normally), you can try it by yourself:

1) Set parallax LWP on TouchWiz first (important!) and see that it depends only on onTouchEvent()
2) Change the launcher to the other one. See that LWP now depends on onOffsetsChanged()
3) Change the launcher to TouchWiz again and see that swiping doesn't work for this LWP anymore.

So what i recommend to add is on every onResume() event change the boolean offsetChangedWorking to false again. This should prevent such bugs with launcher changes.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...