3

我在堆栈溢出中浏览了许多帖子,但找不到可能的解决方案。

package com.sourcebits.gallerygestures;

    import android.app.Activity;
    import android.graphics.Matrix;
    import android.graphics.PointF;
    import android.os.Bundle;
    import android.support.v4.view.PagerAdapter;
    import android.support.v4.view.ViewPager;
    import android.util.FloatMath;
    import android.util.Log;
    import android.view.MotionEvent;
    import android.view.View;
    import android.view.View.OnTouchListener;
    import android.view.ViewGroup;
    import android.widget.Button;
    import android.widget.ImageView;

    /**
     * 
     * @author Rajeev N B
     * 
     */

    public class GalleryActivity extends Activity implements OnTouchListener {

        /**
         * Activity Context
         */
        private GalleryActivity context;

        /**
         * List of drawables
         */
        private int[] mImages = new int[6];

        /**
         * ViewPager for displaying images
         */
        private ViewPager viewPager;

        /**
         * Matrices used for Pinch and Zoom
         */
        Matrix matrix = new Matrix();
        Matrix savedMatrix = new Matrix();

        // We can be in one of these 3 states
        static final int NONE = 0;
        static final int ZOOM = 2;
        int mode = NONE;

        // Remember some things for zooming
        PointF start = new PointF();
        PointF mid = new PointF();
        float oldDist = 1f;

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_gallery);
            context = GalleryActivity.this;
            viewPager = (ViewPager) findViewById(R.id.pager);
            ImagePagerAdapter adapter = new ImagePagerAdapter();
            viewPager.setAdapter(adapter);
        }

        /**
         * 
         * @author Rajeev N B Adapter for displaying the images
         * 
         */
        private class ImagePagerAdapter extends PagerAdapter {
            private int[] mImages = { R.drawable.background, R.drawable.koskula,
                    R.drawable.wallpaper, R.drawable.wallpap, R.drawable.gr,
                    R.drawable.balck };
            private int pos;
            private ImageView imageView;

            /**
             * Default Constructor
             */
            public ImagePagerAdapter() {

            }

            /**
             * Get the Count of the Pages for the View Pager
             */
            @Override
            public int getCount() {
                return mImages.length;
            }

            /**
             * Get the View from the Object
             */
            @Override
            public boolean isViewFromObject(View view, Object object) {
                pos = viewPager.getCurrentItem();
                return view == ((ImageView) object);
            }

            /**
             * Create a new Item in the viewPager
             */
            @Override
            public Object instantiateItem(ViewGroup container, final int position) {
                Button prev = (Button) findViewById(R.id.button1);
                Button next = (Button) findViewById(R.id.button2);

                next.setOnClickListener(new View.OnClickListener() {

                    public void onClick(View v) {
                        if (pos < mImages.length)
                            viewPager.setCurrentItem(pos + 1);
                        Log.v("Next", "" + pos);
                    }
                });

                prev.setOnClickListener(new View.OnClickListener() {

                    public void onClick(View v) {
                        if (pos > 0)
                            viewPager.setCurrentItem(pos - 1);
                        Log.v("Next", "" + pos);
                    }
                });

                imageView = new ImageView(context);
                int padding = context.getResources().getDimensionPixelSize(
                        R.dimen.padding_medium);
                imageView.setPadding(padding, padding, padding, padding);
                imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
                imageView.setImageResource(mImages[position]);
                ((ViewPager) container).addView(imageView, 0);
                imageView.setOnTouchListener(context);
                return imageView;
            }

            /**
             * Destroy the Items
             */
            @Override
            public void destroyItem(ViewGroup container, int position, Object object) {
                ((ViewPager) container).removeView((ImageView) object);
            }

        }

        /**
         * Pinch to zoom Functionality
         */
        public boolean onTouch(View v, MotionEvent event) {
            ImageView view = (ImageView) v;
            // Handle touch events here...
            switch (event.getAction() & MotionEvent.ACTION_MASK) {
            case MotionEvent.ACTION_DOWN:
                savedMatrix.set(matrix);
                start.set(event.getX(), event.getY());
                break;
            case MotionEvent.ACTION_POINTER_DOWN:
                oldDist = spacing(event);
                if (oldDist > 10f) {
                    savedMatrix.set(matrix);
                    midPoint(mid, event);
                    mode = ZOOM;
                }
                break;
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_POINTER_UP:
                mode = NONE;
                break;
            case MotionEvent.ACTION_MOVE:
                if (mode == ZOOM) {
                    float newDist = spacing(event);
                    if (newDist > 10f) {
                        matrix.set(savedMatrix);
                        float scale = newDist / oldDist;
                        matrix.postScale(scale, scale, mid.x, mid.y);
                    }
                }
                break;
            }

            view.setImageMatrix(matrix);
            return true; // indicate event was handled
        }

        /** Determine the space between the first two fingers */
        private float spacing(MotionEvent event) {
            float x = event.getX(0) - event.getX(1);
            float y = event.getY(0) - event.getY(1);
            return FloatMath.sqrt(x * x + y * y);
        }

        /** Calculate the mid point of the first two fingers */
        private void midPoint(PointF point, MotionEvent event) {
            float x = event.getX(0) + event.getX(1);
            float y = event.getY(0) + event.getY(1);
            point.set(x / 2, y / 2);
        }
    }

基本上是GalleryView使用ViewPager. 我正在根据可绘制数组中的位置膨胀图像视图。现在我想为我的应用程序实现 Pinch to Zoom 功能。但我无法ImageViewViewPager. 我在没有 的情况下测试了我的onTouchEvent捏缩放ViewPager,它工作正常。它不适用于 View Pager。我猜这ViewPager并没有为子视图进行触摸事件。我需要捏才能放大到ImageView里面ViewPager吗?

4

1 回答 1

0

如果您的 viewpager 使用的是 imageView,那么您可以使用 touchImageView。通过以下链接,您可以使用源代码来解决您的问题。

https://github.com/MikeOrtiz/TouchImageView/blob/master/src/com/example/touch/TouchImageView.java

最后,只需用这个自定义视图替换您当前的图像视图。如果在实施过程中有任何问题,请告诉我,我会尝试澄清这一点。

于 2013-07-16T11:41:27.250 回答