Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Example for a blog post
package com.hpstr.service;
import android.animation.ValueAnimator;
import android.content.Intent;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import com.hpstr.BaseApplication;
import com.hpstr.activity.MainActivity;
public final class WallpaperServiceCompat extends GLWallpaperService {
public WallpaperServiceCompat() {
super();
}
@Override public Engine onCreateEngine() {
return new MyEngine();
}
private final class MyEngine extends GLEngine {
private int width;
private final GestureDetector gestureDetector;
private boolean isOnOffsetsChangedWorking, doubleTapEnabled, infiniteScrollingEnabled;
private ValueAnimator compatValueAnimator;
private float numberOfPages;
private final GestureDetector.SimpleOnGestureListener onGestureListener = new GestureDetector.SimpleOnGestureListener() {
private float xOffset = 0f;
@Override public boolean onDoubleTap(final MotionEvent e) {
if (doubleTapEnabled) {
startActivity(new Intent(WallpaperServiceCompat.this, MainActivity.class).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
return true;
}
return false;
}
@Override public boolean onScroll(final MotionEvent e1, final MotionEvent e2, final float distanceX, final float distanceY) {
if (!isOnOffsetsChangedWorking) {
final float newXOffset = xOffset + distanceX / width / numberOfPages;
xOffset = newXOffset;
if (!infiniteScrollingEnabled) {
if (newXOffset > 1) {
xOffset = 1f;
} else if (newXOffset < 0) {
xOffset = 0f;
}
}
// translate by xOffset
}
return super.onScroll(e1, e2, distanceX, distanceY);
}
@Override public boolean onFling(final MotionEvent e1, final MotionEvent e2, final float velocityX, final float velocityY) {
if (!isOnOffsetsChangedWorking) {
float endValue = velocityX > 0
? (xOffset - (xOffset % (1f / numberOfPages)))
: (xOffset - (xOffset % (1f / numberOfPages)) + (1 / numberOfPages));
if (!infiniteScrollingEnabled) {
if (endValue < 0f) {
endValue = 0f;
} else if (endValue > 1f) {
endValue = 1f;
}
}
compatValueAnimator = ValueAnimator.ofFloat(xOffset, endValue);
compatValueAnimator.setDuration(150);
compatValueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override public void onAnimationUpdate(final ValueAnimator animation) {
xOffset = (float) animation.getAnimatedValue();
// translate by xOffset
}
});
compatValueAnimator.start();
}
return super.onFling(e1, e2, velocityX, velocityY);
}
};
private MyEngine() {
super();
isOnOffsetsChangedWorking = false;
gestureDetector = new GestureDetector(BaseApplication.INSTANCE, onGestureListener);
// setup variables
// doubleTapEnabled = ...
// infiniteScrollingEnabled = ...
// numberOfPages = ...
}
@Override public void onOffsetsChanged(final float xOffset, final float yOffset,
final float xOffsetStep, final float yOffsetStep,
final int xPixelOffset, final int yPixelOffset) {
// detect if onOffsetsChanged() is working properly
if (!isOnOffsetsChangedWorking && xOffset != 0.0f && xOffset != 0.5f) {
isOnOffsetsChangedWorking = true;
}
if (isOnOffsetsChangedWorking) {
// translate by xOffset
}
}
@Override public void onCreate(final SurfaceHolder surfaceHolder) {
super.onCreate(surfaceHolder);
}
@Override public void onDestroy() {
super.onDestroy();
}
@Override public void onSurfaceChanged(final SurfaceHolder holder, final int format, final int width, final int height) {
super.onSurfaceChanged(holder, format, width, height);
this.width = width;
}
@Override public void onSurfaceCreated(final SurfaceHolder holder) {
super.onSurfaceCreated(holder);
}
@Override public void onSurfaceDestroyed(final SurfaceHolder holder) {
super.onSurfaceDestroyed(holder);
}
@Override public void onVisibilityChanged(final boolean visible) {
super.onVisibilityChanged(visible);
}
@Override public void onTouchEvent(final MotionEvent event) {
if (!BaseApplication.INSTANCE.isActivityVisible) {
if (!isOnOffsetsChangedWorking && compatValueAnimator != null && compatValueAnimator.isRunning()) {
compatValueAnimator.cancel();
}
gestureDetector.onTouchEvent(event);
}
}
@Override public void onResume() {
super.onResume();
isOnOffsetsChangedWorking = false;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment