JavascriptProva

Visualizzazione post con etichetta zoom. Mostra tutti i post
Visualizzazione post con etichetta zoom. Mostra tutti i post

lunedì 2 maggio 2016

Codice per scrolling e zoom di un'immagine.

Ecco il codice che realizza un'immagine scrollabile e zoomabile:
public class MainActivity extends Activity {
 float scale=1f;
 ScaleGestureDetector SGD;
 
 RelativeLayout mainLayout;
 ImageView imageView;
 
 Bitmap bitmap;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  SGD = new ScaleGestureDetector(this, new ScaleListener());
  mainLayout=(RelativeLayout)findViewById(R.id.mainLayout);
  
  bitmap=BitmapFactory.decodeResource(getResources(), R.drawable.angurie2);
  final int larghezza=300;
  final int altezza=300;
  ImageView i=newImage(mainLayout,larghezza, altezza, 10, 10, bitmap, Color.BLACK, ScaleType.MATRIX);
    
  View.OnTouchListener onTouchListener=new View.OnTouchListener() {
   Matrix matrix=new Matrix();
   Matrix inversa=new Matrix();
   float X,Y,currentX, currentY;
   float deltaX, deltaY;
   @Override
   public boolean onTouch(View v, MotionEvent event) {
    switch(event.getAction()){
    case MotionEvent.ACTION_DOWN:
     X=event.getX();
     Y=event.getY();
     
     break;
    case MotionEvent.ACTION_MOVE:
     
     currentX=event.getX();
     currentY=event.getY();
     float[] pts={0,0};
     ((ImageView)v).getImageMatrix().invert(inversa);
     inversa.mapPoints(pts);
     
     
     if(currentX<X){
      if((pts[0]-currentX+X)<(bitmap.getWidth()-larghezza/scale)){
       deltaX=-pts[0]+currentX-X;
      }
      else{
       if((pts[0]-currentX+X)>(bitmap.getWidth()-larghezza/scale))
       deltaX=-(bitmap.getWidth()-larghezza/scale);
       
      }
     }
     if(currentX>X){
      if((pts[0]-currentX+X)>0){
       deltaX=-pts[0]+currentX-X;
      }
      else{
       if((pts[0]-currentX+X)<0) deltaX=0;
      }
     }
     
     if(currentY<Y){
      if((pts[1]-currentY+Y)<(bitmap.getHeight()-altezza/scale)){
       deltaY=-pts[1]+currentY-Y;
      }
      else{
       if((pts[1]-currentY+Y)>(bitmap.getHeight()-altezza/scale))
        deltaY=-(bitmap.getHeight()-altezza/scale);
       
      }
     }
     if(currentY>Y){
      if((pts[1]-currentY+Y)>0){
       deltaY=-pts[1]+currentY-Y;
      }
      else{
       if((pts[1]-currentY+Y)<0) deltaY=0;
      }
     }
     
     
     matrix.setTranslate(deltaX,deltaY);
     matrix.postScale(scale, scale);
     ((ImageView)v).setImageMatrix(matrix);
     X=currentX;
     Y=currentY;
     SGD.onTouchEvent(event);
     break;
    }
    return true;
   }
  };
  i.setOnTouchListener(onTouchListener);
 
  
  
  
 }

 class ScaleListener extends ScaleGestureDetector.SimpleOnScaleGestureListener{
  @Override
  public boolean onScale(ScaleGestureDetector SGD){
   scale *= SGD.getScaleFactor();
   return true;
  }
 }
 public ImageView newImage(ViewGroup layout, int larghezza, int altezza, 
   float X, float Y, Bitmap bitmap, int colore, 
   ScaleType scaletype){
  ImageView imageView=new ImageView(this);
  layout.addView(imageView);
  imageView.getLayoutParams().width=larghezza;
  imageView.getLayoutParams().height=altezza;
  imageView.setX(X);
  imageView.setY(Y);

  imageView.setImageBitmap(bitmap);
  imageView.setBackgroundColor(colore);
  imageView.setScaleType(scaletype);
  return imageView;
 }

} 
Ora devo provvedere a metterlo nell'applicazione...
Dopo averla opportunamente SALVATA!!!

Il codice per prendere l'immagine devo andare a ripescarmelo...

Eccolo:
  button.setOnClickListener(new View.OnClickListener() {
   
   @Override
   public void onClick(View v) {
    
    imageView.destroyDrawingCache();
    imageView.buildDrawingCache();
    Bitmap bmp=imageView.getDrawingCache();
    imgControllo.setImageBitmap(bmp);
    
   }
  });

martedì 29 marzo 2016

Promemoria: codice per il "pinch zoom" di un layout.

Ho sistemato il problema ponendo un minimo e un massimo:
public class MainActivity extends Activity {
 
  RelativeLayout mainLayout;
  LinearLayout ll;
  ScaleGestureDetector detector;
  float scala=1.f;
  int bmpWidth, bmpHeight;
  TextView textView;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        mainLayout=(RelativeLayout) findViewById(R.id.mainLayout);
        ll=new LinearLayout(this);
        RelativeLayout.LayoutParams params=new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
        params.width=300;
        params.height=200;
        ll.setLayoutParams(params);
        ll.setBackgroundColor(Color.GREEN);
        mainLayout.addView(ll);
        
        detector=new ScaleGestureDetector(this,new listener());
        bmpWidth=300;
        bmpHeight=200;
        textView=(TextView)findViewById(R.id.textView1);
        
        drawMatrix();
        
    }
    
    public void drawMatrix(){
     bmpWidth=(int) (Math.round(bmpWidth*scala));
     bmpHeight=(int) (Math.round(bmpHeight*scala));
     LayoutParams p=(LayoutParams)ll.getLayoutParams();
     p.width=bmpWidth;
     if(bmpWidth>=400){
      bmpWidth=400;
      bmpHeight=(int)(400/1.5);
     }
     if(bmpWidth<=220){
      bmpWidth=220;
      bmpHeight=(int)(220/1.5);
     }
     p.height=bmpHeight;
     ll.setLayoutParams(p);
    
     
     textView.setText(bmpWidth+" "+bmpHeight);
    }
    
    @Override
    public boolean onTouchEvent(MotionEvent event){
     detector.onTouchEvent(event);
  return true;
     
    }
    
    class listener extends ScaleGestureDetector.SimpleOnScaleGestureListener{
     
     @Override
     public boolean onScale(ScaleGestureDetector detector){
      scala=detector.getScaleFactor();
      drawMatrix();
   return true;
      
     }
    }
    
}

Annotazione di codice per il pinch zoom

Ecco un codice che sembra funzionare, per il pinch zoom di una bitmap:
public class MainActivity extends Activity {
 
  RelativeLayout mainLayout;
  ImageView immagine;
  Bitmap bitmap;
  ScaleGestureDetector detector;
  float scala=1.f;
  int bmpWidth, bmpHeight;
  TextView textView;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        mainLayout=(RelativeLayout) findViewById(R.id.mainLayout);
        
        
        detector=new ScaleGestureDetector(this,new listener());
        immagine=(ImageView)findViewById(R.id.imageView1);
        textView=(TextView)findViewById(R.id.textView1);
        
        
        
        bitmap=BitmapFactory.decodeResource(getResources(), R.drawable.calcestruzzo);
        bmpWidth=bitmap.getWidth();
        bmpHeight=bitmap.getHeight();
        drawMatrix();
        
    }
    
    public void drawMatrix(){
     bmpWidth=(int) (bmpWidth*scala);
     bmpHeight=(int) (bmpWidth*scala);
     Bitmap resizedBitmap=Bitmap.createScaledBitmap(bitmap, bmpWidth, bmpHeight, false);
     immagine.setImageBitmap(resizedBitmap);
     textView.setText(bmpWidth+" - "+bmpHeight);
    }
    
    @Override
    public boolean onTouchEvent(MotionEvent event){
     detector.onTouchEvent(event);
  return true;
     
    }
    
    class listener extends ScaleGestureDetector.SimpleOnScaleGestureListener{
     
     @Override
     public boolean onScale(ScaleGestureDetector detector){
      scala=detector.getScaleFactor();
      
      drawMatrix();
   return true;
      
     }
    }
    
}

Me lo annoto prima di giocherellarci sopra...

sabato 26 marzo 2016

Zoomare l'immagine con due dita (ScaleGestureDetector e SimpleOnScaleGestureListener)

Ridimensionare un'immagine con due dita.

Ci sto combattendo un po'...

Credo di essere arrivato a qualche conclusione.
Esiste una classe che si chiama ScaleGestureDetector.
Questa classe viene creata con un costruttore che assume come parametri context e un listener, per cui va creato anche il listener.

Dopo aver scritto un codice funzionante modificando pedissequamente il codice trovato in rete, come al solito rompo il Mandala e lo ricostruisco daccapo a titolo di esercitazione...

public class MainActivity extends Activity {
 
 //dichiarazioni
 ScaleGestureDetector scaleGestureDetector;
 
 RelativeLayout mainLayout;
 ImageView imageView;
 Bitmap bitmap;
 int bitmapHeight,bitmapWidth;
 float scala=1.f;
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  scaleGestureDetector=new ScaleGestureDetector(this,new simpleOnScaleGestureListener());
  mainLayout=(RelativeLayout)findViewById(R.id.mainLayout);
  imageView=new ImageView(this);
  mainLayout.addView(imageView);
  
  bitmap=BitmapFactory.decodeResource(getResources(), R.drawable.calcestruzzo);
  bitmapHeight=bitmap.getHeight();
  bitmapWidth=bitmap.getWidth();  
  drawMatrix();
   
 }
 private void drawMatrix(){
  bitmapWidth*=scala;
  bitmapHeight*=scala;
  Bitmap resizedBitmap=Bitmap.createScaledBitmap(bitmap, bitmapWidth, bitmapHeight, false);
  imageView.setImageBitmap(resizedBitmap);
 }
 
 @Override
 public boolean onTouchEvent(MotionEvent event){
  scaleGestureDetector.onTouchEvent(event);
  return true;
  
 }
 private class simpleOnScaleGestureListener extends SimpleOnScaleGestureListener{
  
  @Override
  public boolean onScale(ScaleGestureDetector detector){
   scala=detector.getScaleFactor();
   drawMatrix();
   return true;
   
  }
 } 

}
Ecco: solo che aumentando e diminuendo le dimensioni un po' di volte mi allunga l'immagine...
Ma per il momento c'è da essere soddisfatti!