Limits on an imageView do not work properly

1

I have an imageview, in which I manage an image of 1600x1000 pixels, I'm using the matrix class to scale it (do ZOOM) and to move it across the screen, the problem is that I'm trying to set limits so that the image does not lose and the limits do not work properly, sometimes they work and sometimes they let see a part of the screen that does not correspond to the image, this happens when I move the image from side to side < - - & gt ;; as an additional note the image is in the drawable-nodpi folder

package sdf.myapplication;


import android.content.Context;
import android.graphics.Matrix;
import android.graphics.Point;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Display;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
import android.view.WindowManager;

import android.widget.ImageView;

public class ZoomInZoomOut extends AppCompatActivity
{

ImageView im;
Matrix matrix =new Matrix();
Float scale=1f,scale2;
ScaleGestureDetector SGD;
float posX=0.0f,posY=0.0f,resX,resY,v1x=0,v2x=0,lasx=0,lasy=0;
Point xy=new Point();
boolean stat=false;


@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_zoom_in_zoom_out);


    Display display = ((WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
    im = (ImageView) findViewById(R.id.imageView);
    SGD= new ScaleGestureDetector(this, new ScaleListener());


    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
        display.getSize(xy);
        resX=xy.x;
        resY=xy.y;
    }else{
        resX=display.getWidth();
        resY=display.getHeight();
    }
    scale2=resX/1600;
    Log.e("RES",Float.toString(scale2));
    scale=scale2;
    matrix.setScale(scale,scale);


}

private class ScaleListener extends ScaleGestureDetector.SimpleOnScaleGestureListener{

    @Override
    public boolean onScale(ScaleGestureDetector detector) {
        scale=scale*detector.getScaleFactor();
        scale= Math.max(scale2,Math.min(scale,5f));
        matrix.setScale(scale,scale);
        im.setImageMatrix(matrix);
        v1x=0;
        v2x=0;
        return true;
    }


}

@Override
public boolean onTouchEvent(MotionEvent event) {

    float v1=0,v2=0;

    SGD.onTouchEvent(event);

    switch(event.getAction() & MotionEvent.ACTION_MASK){
        case MotionEvent.ACTION_MOVE:
            v1=event.getX()-posX;
            v2=event.getY()-posY;

            if(!stat){
                v1x=v1x+v1;
                v2x=v2x+v2;

                //if para el eje X
                if (v1x < (-1600 * scale + resX)) {
                    v1x = -1600 * scale + resX;
                    v1=0;
                } else if (v1x > 0) {
                    v1x = 0;
                    v1=0;
                }

                //if para el eje Y
                if(v2x < (-1000 * scale + resY)) {
                    v2x = -1000 * scale + resY;
                    v2=0;
                } else if (v2x > 0) {
                    v2x = 0;
                    v2=0;
                }

                matrix.postTranslate(v1,v2);

            }



            im.setImageMatrix(matrix);
            break;

        case MotionEvent.ACTION_POINTER_DOWN:
            stat=true;
            v1x=0;
            v2x=0;
            break;

        case MotionEvent.ACTION_DOWN:
            posY=event.getY()-v2;
            posX=event.getX()-v1;
            break;

        case MotionEvent.ACTION_POINTER_UP:
            stat=false;
            break;
        case MotionEvent.ACTION_UP:
            lasx=v1;
            lasy=v2;
            break;

    }
    Log.e("v1",Float.toString(v1));


    return true;
}

}
    
asked by Andres Camacho 15.06.2016 в 22:39
source

1 answer

1

There is a subtle way to zoom the images. This gives you the typical effect of approach, displacement, distancing, etc.

First define your image in the XML

<DondeGuardasteLaClase.TouchImageView
        android:id="@+id/imageviewimagenrow"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="fitCenter"
 />

And this works with the next class. So you must create the next class in your project to make it work.

link

You can download it and paste it into your project. Or you can simply create a TouchImageView name class in your project and copy all the code inside. In both you will have to modify the package and change it to your app.

    
answered by 23.06.2016 в 06:55