将 onScroll 与 viewFlipper 一起使用

发布于 2024-12-17 20:26:24 字数 3318 浏览 0 评论 0原文

我想使用与iPhone画廊效果相同的效果。即,当我滚动图像时,下一个图像开始出现并相对于前一个图像向内移动。

我想在视图翻转器中使用它,到目前为止我可以移动第一张图像,但看不到下一张图像。

public class IphoneAnimationActivity extends Activity implements
    OnGestureListener {

private static final int SWIPE_THRESHOLD_VELOCITY = 1000;
GestureDetector gestureScanner;
ViewFlipper flipper;
View v;
ImageView imageView1;
ImageView imageView2;

LayoutInflater inflate;

Animation slideOutRight;
Animation slideInLeft;
Animation slideOutLeft;
Animation slideInRight;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    flipper = (ViewFlipper) findViewById(R.id.viewFlipper1);

    inflate = getLayoutInflater();

    v = inflate.inflate(R.layout.image_view, null);

    View x = v;
    flipper.addView(x);

    slideOutRight = AnimationUtils.loadAnimation(this,
            R.anim.slide_right_out_photocards);
    slideInRight = AnimationUtils.loadAnimation(this,
            R.anim.slide_right_in_photocards);

    slideOutLeft = AnimationUtils.loadAnimation(this,
            R.anim.slide_left_out_photocards);
    slideInLeft = AnimationUtils.loadAnimation(this,
            R.anim.slide_left_in_photocards);

    gestureScanner = new GestureDetector(this);

    flipper.setOnTouchListener(new OnTouchListener() {

        public boolean onTouch(View v, MotionEvent event) {
            return gestureScanner.onTouchEvent(event);
        }
    });

}

public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
        float velocityY) {

    // right to left swipe
    if (e1.getX() - e2.getX() > 0
            && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
        swipeLeft();
    }
    // left to right swipe
    else if (e2.getX() - e1.getX() > 0
            && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
        swipeRight();
    }
    return false;
}

private void swipeRight() {

    flipper.setInAnimation(slideInRight);
    flipper.setOutAnimation(slideOutRight);

    View y = inflate.inflate(R.layout.image_view, null);
    flipper.addView(y);
    flipper.setDisplayedChild(1);
    flipper.removeViewAt(0);
    // flipper.showNext();

}

private void swipeLeft() {

    flipper.setInAnimation(slideInLeft);
    flipper.setOutAnimation(slideOutLeft);

    View y = inflate.inflate(R.layout.image_view, null);
    flipper.addView(y);
    flipper.setDisplayedChild(1);
    flipper.removeViewAt(0);
}

@Override
public boolean onDown(MotionEvent e) {
    return false;
}

@Override
public void onLongPress(MotionEvent e) {

}

@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
        float distanceY) {

    if((e1.getX() - e2.getX()) > 0)
    {
        flipper.scrollBy(10, 0);            
        return true;
    }
    else if ((e1.getX() - e2.getX()) < 0) {
        flipper.scrollBy(-10, 0);           
        return true;
    }
    else            
    {

    }
    return false;
}

@Override
public void onShowPress(MotionEvent e) {

}

@Override
public boolean onSingleTapUp(MotionEvent e) {

    return false;
}

public boolean onTouchEvent(MotionEvent event) {
    return gestureScanner.onTouchEvent(event);
}

}

I want to use an effect same as iPhone gallery effect. i.e when i scroll an image the next image starts appearing and moves inwards with respect to the previous image.

I want to use this in view flipper, Till now i can move the first image but i can't see the next image.

public class IphoneAnimationActivity extends Activity implements
    OnGestureListener {

private static final int SWIPE_THRESHOLD_VELOCITY = 1000;
GestureDetector gestureScanner;
ViewFlipper flipper;
View v;
ImageView imageView1;
ImageView imageView2;

LayoutInflater inflate;

Animation slideOutRight;
Animation slideInLeft;
Animation slideOutLeft;
Animation slideInRight;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    flipper = (ViewFlipper) findViewById(R.id.viewFlipper1);

    inflate = getLayoutInflater();

    v = inflate.inflate(R.layout.image_view, null);

    View x = v;
    flipper.addView(x);

    slideOutRight = AnimationUtils.loadAnimation(this,
            R.anim.slide_right_out_photocards);
    slideInRight = AnimationUtils.loadAnimation(this,
            R.anim.slide_right_in_photocards);

    slideOutLeft = AnimationUtils.loadAnimation(this,
            R.anim.slide_left_out_photocards);
    slideInLeft = AnimationUtils.loadAnimation(this,
            R.anim.slide_left_in_photocards);

    gestureScanner = new GestureDetector(this);

    flipper.setOnTouchListener(new OnTouchListener() {

        public boolean onTouch(View v, MotionEvent event) {
            return gestureScanner.onTouchEvent(event);
        }
    });

}

public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
        float velocityY) {

    // right to left swipe
    if (e1.getX() - e2.getX() > 0
            && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
        swipeLeft();
    }
    // left to right swipe
    else if (e2.getX() - e1.getX() > 0
            && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
        swipeRight();
    }
    return false;
}

private void swipeRight() {

    flipper.setInAnimation(slideInRight);
    flipper.setOutAnimation(slideOutRight);

    View y = inflate.inflate(R.layout.image_view, null);
    flipper.addView(y);
    flipper.setDisplayedChild(1);
    flipper.removeViewAt(0);
    // flipper.showNext();

}

private void swipeLeft() {

    flipper.setInAnimation(slideInLeft);
    flipper.setOutAnimation(slideOutLeft);

    View y = inflate.inflate(R.layout.image_view, null);
    flipper.addView(y);
    flipper.setDisplayedChild(1);
    flipper.removeViewAt(0);
}

@Override
public boolean onDown(MotionEvent e) {
    return false;
}

@Override
public void onLongPress(MotionEvent e) {

}

@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
        float distanceY) {

    if((e1.getX() - e2.getX()) > 0)
    {
        flipper.scrollBy(10, 0);            
        return true;
    }
    else if ((e1.getX() - e2.getX()) < 0) {
        flipper.scrollBy(-10, 0);           
        return true;
    }
    else            
    {

    }
    return false;
}

@Override
public void onShowPress(MotionEvent e) {

}

@Override
public boolean onSingleTapUp(MotionEvent e) {

    return false;
}

public boolean onTouchEvent(MotionEvent event) {
    return gestureScanner.onTouchEvent(event);
}

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

烟─花易冷 2024-12-24 20:26:24

也许您应该使用 Gallery 小部件,或 ViewPager 来自 android 兼容性包?它们开箱即具有您想要的功能。

Maybe you should use Gallery widget, or ViewPager from android compatibility package instead? They have functionality you want right from the box.

还在原地等你 2024-12-24 20:26:24

从 android support lib v13 开始,ViewPager + PageTransformer 可用并且可能可以解决问题:
http://developer.android.com/training/animation/screen-slide .html#pagetransformer

as of android support lib v13, ViewPager + PageTransformer is available and might do the trick:
http://developer.android.com/training/animation/screen-slide.html#pagetransformer

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文