1

onflingメソッドに小さなアニメーションを追加するのに問題があります。すべてが正常に機能しています。ページを左から右、右から左にめくりますが、アニメーションはありません。いろいろ試してみましたが、アニメーションがうまくいきませんでした。フリッパーアニメーションやスワイプアニメーションを追加する方法を教えてください。コードは次のとおりです。

public class MainActivity extends CustomTitlebarActivityBase {

        // some random variables..

    // detect swipe left/right
    private GestureDetector gestureDetector;
    private MyGestureListener gestureListener;

public void onCreate(Bundle savedInstanceState) {
        Log.i(TAG, "Creating MainActivity");

        super.onCreate(savedInstanceState, true);

        setContentView(R.layout.main_view);

        // create related objects
        gestureListener = new MyGestureListener(MainActivity.this);
        gestureDetector = new GestureDetector( gestureListener );

        documentViewManager = new DocumentViewManager(this);
        documentViewManager.buildView();

        myContentManager = new myContentManager(documentViewManager);

        // force the screen to be populated
        myContentManager.updateText(true);

    }
// there are some other methods

   /** user swiped left */

    public void next() {
        if (getDocumentViewManager().getDocumentView().isPageNextOkay()) {
            CurrentPageManager.getInstance().getCurrentPage().next();
        }       
    }
    /** user swiped left */

    public void previous() {
        if (getDocumentViewManager().getDocumentView().isPagePreviousOkay()) {
            CurrentPageManager.getInstance().getCurrentPage().previous();
        }
    }
}


// here is the listener class

public class MyGestureListener extends SimpleOnGestureListener {

    // measurements in dips for density independence
    private static final int DISTANCE_DIP = 40;
    private int scaledDistance;

    private int minScaledVelocity;
    private MainActivity mainActivity;

    private boolean sensePageDownTap;

    private static final String TAG = "MyGestureListener";

    public MyGestureListener(MainActivity mainActivity) {
        super();
        this.mainActivity = mainActivity;
        scaledDistance = CommonUtils.convertDipsToPx(DISTANCE_DIP);
        minScaledVelocity = ViewConfiguration.get(mainActivity).getScaledMinimumFlingVelocity();
        // make it easier to swipe
        minScaledVelocity = (int)(minScaledVelocity*0.66);
    }

    @Override
    public void onLongPress(MotionEvent e) {
         // do something
    }
         // here is the onFling 
    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        // get distance between points of the fling
        double vertical = Math.abs( e1.getY() - e2.getY() );
        double horizontal = Math.abs( e1.getX() - e2.getX() );

        Log.d(TAG, "onFling vertical:"+vertical+" horizontal:"+horizontal+" VelocityX"+velocityX);

        if ( vertical > scaledDistance ) {
             return false;
        }
        // test horizontal distance and velocity

        else if ( horizontal > scaledDistance && Math.abs(velocityX) > minScaledVelocity ) {
            if (e1.getX() > e2.getX()) {
                mainActivity.next();
            }
            // left to right swipe
            else {
                mainActivity.previous();
            }
            return true;
        }
        return false;
    }
}
4

1 に答える 1

1

OnGestureListener を実装してみてください。サンプルコードはベロエです

public class MyActivity extends Activity implements GestureDetector.OnGestureListener {


private GestureDetector gestureScanner;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.yourlayout);

    this.gestureScanner = new GestureDetector(this);

}

@Override
public boolean dispatchTouchEvent(MotionEvent paramMotionEvent) {
    super.dispatchTouchEvent(paramMotionEvent);
    return this.gestureScanner.onTouchEvent(paramMotionEvent);
}


public boolean onDown(MotionEvent paramMotionEvent) {
    return false;
}

public boolean onFling(MotionEvent paramMotionEvent1, MotionEvent paramMotionEvent2, float paramFloat1,
        float paramFloat2) {

// your fling code goes here

    return true;
}

public void onLongPress(MotionEvent paramMotionEvent) {
}

public boolean onScroll(MotionEvent paramMotionEvent1, MotionEvent paramMotionEvent2, float paramFloat1,
        float paramFloat2) {
    return false;
}

public void onShowPress(MotionEvent paramMotionEvent) {
}

public boolean onSingleTapUp(MotionEvent paramMotionEvent) {
    return false;
}

}
于 2012-09-02T08:46:44.320 に答える