如何轻扫图像

我试图实现一个简单的图像库,其中我有一个图像在设备屏幕上一次显示。 当我们从左向右滑动屏幕时,它应该显示下一个图像。
为此,我实现了一个视图脚蹼并为其添加了图像视图。
但我不知道如何捕捉那个滑动事件。
谁能告诉我一个例子?


我需要为我的应用程序做同样的事情,并使用ViewPager :http://blog.sqisland.com/2012/09/android-swipe-image-viewer-with-viewpager.html

以前我使用了一个ImageSwitcher和一个GestureDetector :http://blog.sqisland.com/2012/07/android-swipe-image-viewer.html

ViewPager的代码更加简单,而且体验更好,因为图像在您的手指滑过屏幕时滑动。 我直接创建ImageView ,不需要片段。


你可以通过扩展PagerAdapter来使用ViewPager ......就像这样

    public class ImageAdapter extends PagerAdapter {
    private Context context;
    private int[] GalImages = new int[] {
            R.drawable.cap8, R.drawable.cap2, R.drawable.cap3, R.drawable.cap1,R.drawable.cap5,
            R.drawable.cap6, R.drawable.cap7, R.drawable.cap9,R.drawable.cap4,
            R.drawable.cap10

    };
    ImageAdapter(Context context)
    {
        this.context=context;
    }

    @Override
    public int getCount() {
        return GalImages.length;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }
    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        ImageView imageView = new ImageView(context);
        int padding = context.getResources().getDimensionPixelSize(R.dimen.activity_horizontal_margin);
        imageView.setPadding(padding, padding, padding, padding);
        imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
        imageView.setImageResource(GalImages[position]);
        container.addView(imageView, 0);
        return imageView;
    }
    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((ImageView) object);
    }
}

xml代码是......

 <android.support.v4.view.ViewPager
        android:id="@+id/mvieww"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

你也可以这样做,而不使用视图鳍状肢

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent"> 

<ImageView

                 android:layout_width="fill_parent"

                 android:layout_height="fill_parent"

                 android:layout_gravity="center"

                 android:gravity="center"

                 android:layout_margin="10dip"

                 android:id="@+id/image_place_holder"
                 />

 </RelativeLayout>

活动类

 public class MainActivity extends Activity  {

    private Integer[] mImageIds = { R.drawable.img1, R.drawable.img2,
        R.drawable.img3 };
private static final String DEBUG_TAG = "MainActivity ";
ImageView imageView;
float startXValue = 1;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.gallery);
    imageView = (ImageView) findViewById(R.id.image_place_holder);
    imageView.setImageResource(mImageIds[num]);
}

@Override
public boolean onTouchEvent(MotionEvent event) {    
    float endXValue = 0;
    float x1 = event.getAxisValue(MotionEvent.AXIS_X);
    int action = MotionEventCompat.getActionMasked(event);
    switch (action) {
        case (MotionEvent.ACTION_DOWN):
            startXValue = event.getAxisValue(MotionEvent.AXIS_X);

            return true;

        case (MotionEvent.ACTION_UP):
            endXValue = event.getAxisValue(MotionEvent.AXIS_X);
            if (endXValue > startXValue) {
                if (endXValue - startXValue > 100) {
                System.out.println("Left-Right");
                imageView.setImageResource(mImageIds[2]);
                }
            }else {
                if (startXValue -endXValue> 100) {
                System.out.println("Right-Left");
                imageView.setImageResource(mImageIds[0]);

                     }
                         }
        return true;


           default:
        return super.onTouchEvent(event);
     }

}

}
链接地址: http://www.djcxy.com/p/91221.html

上一篇: How to swipe images

下一篇: Implementing Circular Scrolling In PagerAdapter