1

I am working on application in android, which displays some preset images in image view inside the Flipper. I want to implement the zoom to this image views, on pinch zoom.I have implemented the flipper and swipe movemet to change the imageview on swipe gestures. I want to now implement the pinch to zoom. Heres the xml for activity.

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#ffffffff"
        android:orientation="vertical" >

        <ViewFlipper
            android:id="@+id/view_flipper"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <RelativeLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" >

                <TextView
                    android:id="@+id/textView1"
                    style="@style/ImageTitle"
                    android:layout_height="wrap_content"
                    android:text="Alto 800" />

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerHorizontal="true"
                    android:layout_centerVertical="true"
                    android:layout_gravity="center"
                    android:adjustViewBounds="true"
                    android:scaleType="matrix"
                    android:src="@drawable/alto800" />

            </RelativeLayout>

            <RelativeLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" >

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerHorizontal="true"
                    android:layout_centerVertical="true"
                    android:layout_gravity="center"
                    android:adjustViewBounds="true"
                    android:scaleType="matrix"
                    android:src="@drawable/cheveroletbeat" />

                <TextView
                    style="@style/ImageTitle"
                    android:text="Cheverolet Beat" />
            </RelativeLayout>
 </ViewFlipper>

Heres the java code which Implemented

   public class MainActivity extends Activity {

        private static final int SWIPE_MIN_DISTANCE = 120;
        private static final int SWIPE_THRESHOLD_VELOCITY = 200;
        private ViewFlipper mViewFlipper;
        private Context mContext;
        public MediaPlayer mp ;
        @SuppressWarnings("deprecation")
        private final GestureDetector detector = new GestureDetector(
                new SwipeGestureDetector());

        int flag = 1;


        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            mp = MediaPlayer.create(getBaseContext(), R.raw.funtime);
            mp.setLooping(true);
            mp.start();

            mContext = this;

            mViewFlipper = (ViewFlipper) this.findViewById(R.id.view_flipper);
            mViewFlipper.setOnTouchListener(new OnTouchListener() {
                @Override
                public boolean onTouch(final View view, final MotionEvent event) {
                    detector.onTouchEvent(event);
                    return true;
                }
            });
   }@Override
        public void onDestroy()
        {
            super.onDestroy();
            mp.stop();
        }

        class SwipeGestureDetector extends SimpleOnGestureListener implements
                OnGestureListener {

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

                try {
                    // right to left swipe
                    if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE
                            && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                        mViewFlipper.setInAnimation(AnimationUtils.loadAnimation(
                                mContext, R.anim.right_in));
                        mViewFlipper.setOutAnimation(AnimationUtils.loadAnimation(
                                mContext, R.anim.left_out));
                        mViewFlipper.showNext();
                        MediaPlayer mediaPlayer = MediaPlayer.create(
                                getBaseContext(), R.raw.vroom);
                        mediaPlayer.start();
                        return true;
                    } else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE
                            && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                        mViewFlipper.setInAnimation(AnimationUtils.loadAnimation(
                                mContext, R.anim.left_in));
                        mViewFlipper.setOutAnimation(AnimationUtils.loadAnimation(
                                mContext, R.anim.right_out));

                        mViewFlipper.showPrevious();
                        MediaPlayer mediaPlayer = MediaPlayer.create(
                                getBaseContext(), R.raw.vroom);
                        mediaPlayer.start();
                        return true;
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                }

                return false;
            }
        }
    }

Please guide me on pinch to zoom part here.

Mayur
  • 789
  • 10
  • 37
  • see my answer here http://stackoverflow.com/questions/21633545/android-imageview-scaling-and-translating-issue – pskink Apr 20 '14 at 06:43

0 Answers0