将 onScroll 与 viewFlipper 一起使用
我想使用与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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
也许您应该使用 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.
从 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