如何将手势检测器添加到ViewPager

时间:2016-01-17 14:02:17

标签: android gesture android-pageradapter

我想做什么:

图像可以通过viewpager滑动,但人们可以通过手势检测器进行缩放或缩放。

问题:

我可以使用viewpager滑动图像,但不知道如何在我的Viewpager中添加手势检测器。

代码:

的活动:

    public class ImageSlider extends AppCompatActivity {
    private ViewPager viewPager;
    CustomSwipeAdpater customSwipeAdpater;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_image_slider);
        viewPager = (ViewPager) findViewById(R.id.pager);
        customSwipeAdpater = new CustomSwipeAdpater(this);
        viewPager.setAdapter(customSwipeAdpater);
    }
}

PagerAdpater:

    public class CustomSwipeAdpater extends PagerAdapter {
    private int[] image_resources = {R.drawable.wall,R.drawable.welcome};
    private Context context;
    private LayoutInflater inflater;
    private Animator mCurrentAnimator;
    private ImageView imageView;

    public CustomSwipeAdpater(Context context){
        this.context = context;

    }

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

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return (view == (RelativeLayout)object);
    }

    @Override
    public Object instantiateItem(ViewGroup container, final int position) {
        inflater = (LayoutInflater)context.getSystemService(context.LAYOUT_INFLATER_SERVICE);

        View item_view = inflater.inflate(R.layout. swipe_layout,container,false);
        imageView = (ImageView) item_view.findViewById(R.id.ivSliderImage);
        TextView textView = (TextView) item_view.findViewById(R.id.tvSliderImageText);
        imageView.setImageResource(image_resources[position]);
        textView.setText("Image:" + position);
        container.addView(item_view);
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            }
        });
        return item_view;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((RelativeLayout) object);
    }
}

如何在此代码中添加手势检测器?

1 个答案:

答案 0 :(得分:1)

Photoview库做你想要的东西也支持旋转。 创建touchAttacher并将图像传递给它,它就完成了。

以下是 PhotoViewAttacher 代码的一部分,它完全符合您的要求:

public PhotoViewAttacher(ImageView imageView, boolean zoomable) {
    mImageView = new WeakReference<>(imageView);

    imageView.setDrawingCacheEnabled(true);
    imageView.setOnTouchListener(this);

    ViewTreeObserver observer = imageView.getViewTreeObserver();
    if (null != observer)
        observer.addOnGlobalLayoutListener(this);

    // Make sure we using MATRIX Scale Type
    setImageViewScaleTypeMatrix(imageView);

    if (imageView.isInEditMode()) {
        return;
    }
    // Create Gesture Detectors...
    mScaleDragDetector = VersionedGestureDetector.newInstance(
            imageView.getContext(), this);

    mGestureDetector = new GestureDetector(imageView.getContext(),
            new GestureDetector.SimpleOnGestureListener() {

                // forward long click listener
                @Override
                public void onLongPress(MotionEvent e) {
                    if (null != mLongClickListener) {
                        mLongClickListener.onLongClick(getImageView());
                    }
                }
            });

    mGestureDetector.setOnDoubleTapListener(new DefaultOnDoubleTapListener(this));

    // Finally, update the UI so that we're zoomable
    setZoomable(zoomable);
}