JavascriptProva

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

Nessun commento:

Posta un commento