Autore Topic: Come impostare i margini del massimo ingrandimento e minimo ingrandimento  (Letto 225 volte)

Offline Hildian

  • Utente junior
  • **
  • Post: 60
  • Respect: 0
    • Google+
    • Mostra profilo
  • Dispositivo Android:
    Meizu M1 Note
  • Sistema operativo:
    Mac OS X 10.8.5 e Windows 7
salve a tutti ho un leggero problema ho bisogno di impostare i margini minimi e massimo di ingrandimento per la mia imageview vi posto il mio codice:
Codice (Java): [Seleziona]
package com.sorrentinoandrea.classi;

import com.sorrentinoandrea.itipacinottimobile.R;

import android.app.Activity;
import android.graphics.Matrix;
import android.graphics.PointF;
import android.os.Bundle;
import android.util.FloatMath;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageView;


public class P_A extends Activity implements OnTouchListener {
private static final String TAG = "Touch";


Matrix matrix = new Matrix();
Matrix savedMatrix = new Matrix();

static final int NONE = 0;
static final int DRAG = 1;
static final int ZOOM = 2;
int mode = NONE;


PointF start = new PointF();
PointF mid = new PointF();
float oldDist = 1f;

@Override
public void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_p_a);
   ImageView view = (ImageView) findViewById(R.id.tb_p_a);
   view.setScaleType(ImageView.ScaleType.FIT_CENTER);
   view.setOnTouchListener(this);  
}

public boolean onTouch(View v, MotionEvent event) {
   ImageView view = (ImageView) v;
 
   view.setScaleType(ImageView.ScaleType.MATRIX);
   float scale;

 
   switch (event.getAction() & MotionEvent.ACTION_MASK) {

   case MotionEvent.ACTION_DOWN:
      savedMatrix.set(matrix);
      start.set(event.getX(), event.getY());
      Log.d(TAG, "mode=DRAG" );
      mode = DRAG;
      break;
   case MotionEvent.ACTION_UP:
   case MotionEvent.ACTION_POINTER_UP:
      mode = NONE;
      Log.d(TAG, "mode=NONE" );
      break;
   case MotionEvent.ACTION_POINTER_DOWN:
      oldDist = spacing(event);
      Log.d(TAG, "oldDist=" + oldDist);
     
      if (oldDist > 5f) {
         savedMatrix.set(matrix);
         midPoint(mid, event);
         mode = ZOOM;
         Log.d(TAG, "mode=ZOOM" );
      }
      break;

   case MotionEvent.ACTION_MOVE:
      if (mode == DRAG)
      {
         matrix.set(savedMatrix);
         if (view.getLeft() >= -392)
         {
            matrix.postTranslate(event.getX() - start.x, event.getY() - start.y);
         }
      }
      else if (mode == ZOOM) {
         float newDist = spacing(event);
         Log.d(TAG, "newDist=" + newDist);
         if (newDist > 5f) {
            matrix.set(savedMatrix);
            scale = newDist/oldDist;
            matrix.postScale(scale, scale, mid.x, mid.y);
         }
      }
      break;
   }

   
   view.setImageMatrix(matrix);

   return true;
}

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);
}

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);
}
}

GRAZIE A TUTTI PER LA PAZIENZA!