How to have scrolling animation programmatically

2020-05-27 15:34发布

问题:

I'm trying to implement scroll animation for gallery programmatically.

Tried with setSelection(int pos, Boolean animate) and it's not working.

Is there anyway to override setSelection() method.

回答1:

Just now I have got this problem. I was need to move just one element of the gallery, so the best solution to me was to emulate key down event

myGallery.onKeyDown(KeyEvent.KEYCODE_DPAD_RIGHT, null);

or

myGallery.onKeyDown(KeyEvent.KEYCODE_DPAD_LEFT, null);


回答2:

Gallery.setSelection(int position, boolean animate);

Refer below URL: http://groups.google.com/group/android-developers/browse_thread/thread/9140fd6af3061cdf/7f89e53ae53e455b?lnk=gst&q=setselection#7f89e53ae53e455b

Soln: If you're still looking, I have two possible solutions for you, both slightly unpleasant:

(1) You can make the gallery do a fling with a chosen velocity, thus:

myGallery.onFling(null, null, velocity, 0);

By tweaking the velocity, you can set up values to move the selection by one or two in either direction. As the Gallery self-centers, you do not need to get the destination exactly right.

(2) As the Gallery source is available, you can modify it in order to implement your own Gallery. It doesn't look as though you need to add much code to be able to control a fling so as to end at your chosen selection.

I thought I was going to have to do (2), but found I could get away with (1) for my problem.



回答3:

Based on Kurru's excellent thinking of simulating clicking next or previous view.

//scroll forward or backward
private void scroll(int type){
    View selectedV = mG.getSelectedView();
    int idx = mG.indexOfChild(selectedV);
    switch(type){
        case FORWARD:
    default:
        if(idx<mG.getChildCount()-1)
            idx++;
        break;
    case BACKWARD:
        if(idx>0)
            idx--;          
        break;
    }
    //now scrolled view's child idx in gallery is gotten
    View nextView = mG.getChildAt(idx);
    //(x,y) in scrolled view is gotten
    int x = nextView.getLeft()+nextView.getWidth()/2;
    int y = nextView.getTop()+nextView.getHeight()/2;
    String out = String.format("x=%d, y=%d", x, y);
    Log.i(TAG+".scroll", out);

    //Kurru's simulating clicking view
    MotionEvent event = MotionEvent.obtain(100, 100, MotionEvent.ACTION_DOWN, x, y, 0);
    mG.onDown(event); 
    boolean res = mG.onSingleTapUp(null);
    Log.i(TAG+".scroll", "onSingleTapUp return =" + res);       
}


回答4:

I was looking through the Gallery source to see if I could get this feature. It looks like something is possible with this code. However I gave up before I could get it working. It seems like I wasn't passing in the correct coordinates so res always returned false. Would return true if it worked.

Just posting this here in-case someone else wants to give a go at fixing it! (Please post your solution if you manage it!)

Rect rect = new Rect();
gallery.getHitRect(rect);


int x = rect.centerX()+getWindowManager().getDefaultDisplay().getWidth();
int y = rect.centerY();

MotionEvent event = MotionEvent.obtain(100, 100, MotionEvent.ACTION_DOWN, x, y, 0);
timesGallery.onDown(event);
boolean res = timesGallery.onSingleTapUp(null);


回答5:

I made little change in code given by "Kurru". this is working now

Rect rect = new Rect();
    gallery.getHitRect(rect);

    int width = Math.abs(rect.width());
    if(!isForwardScroll){
        width = width * -1;
    }
    int x = rect.centerX()+width/2;
    int y = rect.centerY();

    MotionEvent event = MotionEvent.obtain(100, 100, MotionEvent.ACTION_DOWN, x, y, 0);
    gallery.onDown(event);
    boolean res = gallery.onSingleTapUp(null);