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。示例代码是 beloe

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 回答