Zoom layout android studio

Android Pinch Zoom Layout Example

Pinch zoom ( in or out ) is an often-used gesture in android applications. It is very user-friendly and smooth to display something bigger or smaller to watch. This example will show you how to implement pinch zoom on an image in the android application.

1. Make Android App Pinch Zoom Enabled Steps.

  1. Create a class like OnPinchListner which extends ScaleGestureDetector.SimpleOnScaleGestureListener. This class will be used to monitor user pinch-zoom gestures.
  2. Override the onScale(ScaleGestureDetector detector) method of OnPinchListner class, this method will be invoked when the listener detects pinch-zoom gesture.
  3. Create class android.view.ScaleGestureDetector‘s object scaleGestureDetector with an instance of class OnPinchListener as the constructor input parameter.
  4. When the activity on touch event occurred, invoke scaleGestureDetector‘s onTouchEvent method to make it detect scale change gesture.

2. Android Pinch Zoom Example.

If you can not watch the above video, you can see it on the youtube URL https://youtu.be/iALniC31f5M

  1. There is an image view widget in this example.
  2. When you run the example in the emulator. Click the control key ( Windows ) or command key ( Mac OS) and the left mouse key anywhere on the screen at the same time, you can zoom in and out the image.

Источник

Zoom layout android studio

Need support, consulting, or have any other business-related question? Feel free to get in touch.

Like the project, make profit from it, or simply want to thank back? Please consider sponsoring!

A collection of flexible Android components that support zooming and panning of View hierarchies, images, video streams, and much more — either programmatically or through touch events.

  • ZoomLayout : a container that supports 2D pan and zoom to a View hierarchy, even supporting clicks [docs]
  • ZoomImageView : (yet another) ImageView that supports 2D pan and zoom [docs]
  • ZoomSurfaceView : A SurfaceView that supports 2D pan and zoom with OpenGL rendering [docs]
  • Powerful zoom APIs [docs]
  • Powerful pan APIs [docs]
  • Lightweight, no dependencies
  • Works down to API 16

In fact, ZoomLayout , ZoomImageView and ZoomSurfaceView are just very simple implementations of the internal ZoomEngine [docs]. The zoom engine lets you animate everything through constant updates, as long as you feed it with touch events, with a Matrix -based mechanism that makes it very flexible.

Читайте также:  Все операторы для android

If you like the project, make profit from it, or simply want to thank back, please consider supporting it through the GitHub Sponsors program! You can have your company logo here, get private support hours or simply help me push this forward.

Feel free to contact me for support, consulting or any other business-related question.

Please read the official website for setup instructions and documentation. You might also be interested in our changelog.

About

2D zoom and pan behavior for View hierarchies, images, video streams, and much more, written in Kotlin for Android.

Источник

anorth / ZoomLayout.java

This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters

package au.id.alexn ;
import android.content.Context ;
import android.util.AttributeSet ;
import android.util.Log ;
import android.view.MotionEvent ;
import android.view.ScaleGestureDetector ;
import android.view.View ;
import android.widget.FrameLayout ;
/**
* Layout that provides pinch-zooming of content. This view should have exactly one child
* view containing the content.
*/
public class ZoomLayout extends FrameLayout implements ScaleGestureDetector . OnScaleGestureListener <
private enum Mode <
NONE ,
DRAG ,
ZOOM
>
private static final String TAG = » ZoomLayout » ;
private static final float MIN_ZOOM = 1.0f ;
private static final float MAX_ZOOM = 4.0f ;
private Mode mode = Mode . NONE ;
private float scale = 1.0f ;
private float lastScaleFactor = 0f ;
// Where the finger first touches the screen
private float startX = 0f ;
private float startY = 0f ;
// How much to translate the canvas
private float dx = 0f ;
private float dy = 0f ;
private float prevDx = 0f ;
private float prevDy = 0f ;
public ZoomLayout ( Context context ) <
super (context);
init(context);
>
public ZoomLayout ( Context context , AttributeSet attrs ) <
super (context, attrs);
init(context);
>
public ZoomLayout ( Context context , AttributeSet attrs , int defStyle ) <
super (context, attrs, defStyle);
init(context);
>
private void init ( Context context ) <
final ScaleGestureDetector scaleDetector = new ScaleGestureDetector (context, this );
this . setOnTouchListener( new View . OnTouchListener () <
@Override
public boolean onTouch ( View view , MotionEvent motionEvent ) <
switch (motionEvent . getAction() & MotionEvent . ACTION_MASK ) <
case MotionEvent . ACTION_DOWN :
Log . i( TAG , » DOWN » );
if (scale > MIN_ZOOM ) <
mode = Mode . DRAG ;
startX = motionEvent . getX() — prevDx;
startY = motionEvent . getY() — prevDy;
>
break ;
case MotionEvent . ACTION_MOVE :
if (mode == Mode . DRAG ) <
dx = motionEvent . getX() — startX;
dy = motionEvent . getY() — startY;
>
break ;
case MotionEvent . ACTION_POINTER_DOWN :
mode = Mode . ZOOM ;
break ;
case MotionEvent . ACTION_POINTER_UP :
mode = Mode . DRAG ;
break ;
case MotionEvent . ACTION_UP :
Log . i( TAG , » UP » );
mode = Mode . NONE ;
prevDx = dx;
prevDy = dy;
break ;
>
scaleDetector . onTouchEvent(motionEvent);
if ((mode == Mode . DRAG && scale >= MIN_ZOOM ) || mode == Mode . ZOOM ) <
getParent() . requestDisallowInterceptTouchEvent( true );
float maxDx = (child() . getWidth() — (child() . getWidth() / scale)) / 2 * scale;
float maxDy = (child() . getHeight() — (child() . getHeight() / scale)) / 2 * scale;
dx = Math . min( Math . max(dx, — maxDx), maxDx);
dy = Math . min( Math . max(dy, — maxDy), maxDy);
Log . i( TAG , » Width: » + child() . getWidth() + » , scale » + scale + » , dx » + dx
+ » , max » + maxDx);
applyScaleAndTranslation();
>
return true ;
>
>);
>
// ScaleGestureDetector
@Override
public boolean onScaleBegin ( ScaleGestureDetector scaleDetector ) <
Log . i( TAG , » onScaleBegin » );
return true ;
>
@Override
public boolean onScale ( ScaleGestureDetector scaleDetector ) <
float scaleFactor = scaleDetector . getScaleFactor();
Log . i( TAG , » onScale » + scaleFactor);
if (lastScaleFactor == 0 || ( Math . signum(scaleFactor) == Math . signum(lastScaleFactor))) <
scale *= scaleFactor;
scale = Math . max( MIN_ZOOM , Math . min(scale, MAX_ZOOM ));
lastScaleFactor = scaleFactor;
> else <
lastScaleFactor = 0 ;
>
return true ;
>
@Override
public void onScaleEnd ( ScaleGestureDetector scaleDetector ) <
Log . i( TAG , » onScaleEnd » );
>
private void applyScaleAndTranslation () <
child() . setScaleX(scale);
child() . setScaleY(scale);
child() . setTranslationX(dx);
child() . setTranslationY(dy);
>
private View child () <
return getChildAt( 0 );
>
>
Читайте также:  Android bluetooth клавиатура раскладка

This comment has been minimized.

Copy link Quote reply

jpfolador commented Sep 25, 2014

Do you have some example of how use your class?
Does it work with all layout? When you pinch the screen all objects get zoom (text, images, . ) ?
Thanks for your attention.

This comment has been minimized.

Copy link Quote reply

madhan123 commented Nov 21, 2014

When adding custom view to framelayout zoomlayout class not firing

Источник

Zoom layout android studio

This is a great tutorial

I am using this code in fragments but
java.lang.NullPointerException: Attempt to invoke virtual method ‘void android.widget.ZoomControls.setOnZoomInClickListener(android.view.View$OnClickListener)’

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ZoomControls;

import abdulrehman.islamictasawwufsilsalanaqshbandiyaowaisia.R;
import uk.co.senab.photoview.PhotoView;
import uk.co.senab.photoview.PhotoViewAttacher;

/**
* A simple <@link Fragment>subclass.
*/
public class HomeFragment extends Fragment <
TextView textView;
ImageView imageView;
ZoomControls simpleZoomControls;
public HomeFragment() <
// Required empty public constructor
>

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) <
View view =inflater.inflate(R.layout.fragment_home, container, false);

final ImageView imageView = view.findViewById(R.id.HazratJeeIntro1);

ImageView imageView1 = view.findViewById(R.id.hazrajee1);
PhotoViewAttacher photoView2 = new PhotoViewAttacher(imageView1);
photoView2.update();

final TextView textView = view.findViewById(R.id.HazratJeeDescEng);

imageView.setOnTouchListener(new View.OnTouchListener() <
@Override
public boolean onTouch(View v, MotionEvent event) <
simpleZoomControls.show();
return false;
>
>);
// perform setOnZoomInClickListener event on ZoomControls
simpleZoomControls.setOnZoomInClickListener(new View.OnClickListener() <
@Override
public void onClick(View v) <
// calculate current scale x and y value of ImageView
float x = imageView.getScaleX();
float y = imageView.getScaleY();
// set increased value of scale x and y to perform zoom in functionality
imageView.setScaleX((float) (x + 1));
imageView.setScaleY((float) (y + 1));
// display a toast to show Zoom In Message on Screen
Toast.makeText(getContext(),”Zoom In”,Toast.LENGTH_SHORT).show();
// hide the ZoomControls from the screen
simpleZoomControls.hide();
>
>);
// perform setOnZoomOutClickListener event on ZoomControls
simpleZoomControls.setOnZoomOutClickListener(new View.OnClickListener() <
@Override
public void onClick(View v) <
// calculate current scale x and y value of ImageView
float x = imageView.getScaleX();
float y = imageView.getScaleY();
// set decreased value of scale x and y to perform zoom out functionality
imageView.setScaleX((float) (x – 1));
imageView.setScaleY((float) (y – 1));
// display a toast to show Zoom Out Message on Screen
Toast.makeText(getContext(),”Zoom Out”,Toast.LENGTH_SHORT).show();
// hide the ZoomControls from the screen
simpleZoomControls.hide();
>
>);
return view;
>
>

Читайте также:  Отважный спасатель для андроид

sir how i can implement these zoom controls in textview. /

Источник

Zoom layout android studio

A simple pinch-to-zoom ImageView library for Android with an emphasis on a smooth and natural feel.

Simply add a ZoomageView as you would any typical ImageView in Android. The scaleType that you set on your ZoomageView will determine the starting size and position of your ZoomageView’s image. This is the inherited ImageView.ScaleType from Android. With a ZoomageView, the fitCenter or centerInside scale types usually make the most sense to use, fitCenter being Android’s default scale type.

If using a ZoomageView with a view pager, it is recommended that ViewPager2 is used.

Restricts the bounds of the image so it does not wander outside the border of the ImageView when it’s smaller than the frame size, and restricts the bounds to stop at the edges of the ImageView when the image is larger than the frame size. Default value is false.

Image will animate back to its starting size whenever it is reset if true, and will snap back to its starting size when false. Default value is true.

Determines at what times the image will reset to its starting size. Note that UNDER, OVER, and ALWAYS all have the effect of resetting the image to its starting position if its size has not changed. Default value is UNDER.

This will cause the image to pull itself into view on-screen if it is partially off-screen. Default value is true.

The minimum allowed scale for the image. Ideally this should be less than 1, must be greater than 0, and must be less than maxScale. Default value is 0.6.

The maximum allowed scale for the image. Ideally this should be greater than 1, must be greater than 0, and must be greater than minScale. Default value is 8.

Sets whether zooming is allowed. Default value is true.

Sets whether translation is allowed. Default value is true.

Sets whether double tap to zoom functionality is enabled. Default is true.

Sets the scale factor for double tap to zoom functionality. Default is 3.

Special thanks to @mchowning for all his help

Источник

Оцените статью