Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android zoom and rotate image view

Can anyone tell me example of image view which accommodates both zoom and rotate functionality on pinch.

I am not able to find it out.

like image 772
Dhrupal Avatar asked Apr 01 '13 08:04

Dhrupal


Video Answer


1 Answers

Check the photoview library by Chris Banes.

Usage:

here is a sample provided which shows how to use the library in a more advanced way, but for completeness, here is all that is required to get PhotoView working:

<com.github.chrisbanes.photoview.PhotoView
    android:id="@+id/photo_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>
PhotoView photoView = (PhotoView) findViewById(R.id.photo_view);
photoView.setImageResource(R.drawable.image);

That's it!

Several methods, such as photo.setRotationBy(10); for a rotation and mPhotoView.setScaleType(ImageView.ScaleType.CENTER); for a zoom are provided. More examples, from the sample include:

        public boolean onMenuItemClick(MenuItem item) { // Line 71
            switch (item.getItemId()) {
                case R.id.menu_zoom_toggle:
                    mPhotoView.setZoomable(!mPhotoView.isZoomable());
                    item.setTitle(mPhotoView.isZoomable() ? R.string.menu_zoom_disable : R.string.menu_zoom_enable);
                    return true;

                case R.id.menu_scale_fit_center:
                    mPhotoView.setScaleType(ImageView.ScaleType.CENTER);
                    return true;

                case R.id.menu_scale_fit_start:
                    mPhotoView.setScaleType(ImageView.ScaleType.FIT_START);
                    return true;

                case R.id.menu_scale_fit_end:
                    mPhotoView.setScaleType(ImageView.ScaleType.FIT_END);
                    return true;

                case R.id.menu_scale_fit_xy:
                    mPhotoView.setScaleType(ImageView.ScaleType.FIT_XY);
                    return true;

                case R.id.menu_scale_scale_center:
                    mPhotoView.setScaleType(ImageView.ScaleType.CENTER);
                    return true;

                case R.id.menu_scale_scale_center_crop:
                    mPhotoView.setScaleType(ImageView.ScaleType.CENTER_CROP);
                    return true;

                case R.id.menu_scale_scale_center_inside:
                    mPhotoView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
                    return true;

                case R.id.menu_scale_random_animate:
                case R.id.menu_scale_random:
                    Random r = new Random();

                    float minScale = mPhotoView.getMinimumScale();
                    float maxScale = mPhotoView.getMaximumScale();
                    float randomScale = minScale + (r.nextFloat() * (maxScale - minScale));
                    mPhotoView.setScale(randomScale, item.getItemId() == R.id.menu_scale_random_animate);

                    showToast(String.format(SCALE_TOAST_STRING, randomScale));

                    return true;
                case R.id.menu_matrix_restore:
                    if (mCurrentDisplayMatrix == null)
                        showToast("You need to capture display matrix first");
                    else
                        mPhotoView.setDisplayMatrix(mCurrentDisplayMatrix);
                    return true;
                case R.id.menu_matrix_capture:
                    mCurrentDisplayMatrix = new Matrix();
                    mPhotoView.getDisplayMatrix(mCurrentDisplayMatrix);
                    return true;
            }
            return false;
        }
    });

and

        public boolean onMenuItemClick(MenuItem item) {
            switch (item.getItemId()) {
                case R.id.action_rotate_10_right:
                    photo.setRotationBy(10);
                    return true;
                case R.id.action_rotate_10_left:
                    photo.setRotationBy(-10);
                    return true;
                case R.id.action_toggle_automatic_rotation:
                    toggleRotation();
                    return true;
                case R.id.action_reset_to_0:
                    photo.setRotationTo(0);
                    return true;
                case R.id.action_reset_to_90:
                    photo.setRotationTo(90);
                    return true;
                case R.id.action_reset_to_180:
                    photo.setRotationTo(180);
                    return true;
                case R.id.action_reset_to_270:
                    photo.setRotationTo(270);
                    return true;
            }
            return false;
        }

You can see all the samples here.

like image 63
fedepaol Avatar answered Sep 27 '22 19:09

fedepaol