imageView =new ImageView(this); bitmap=BitmapFactory.decodeResource(getResources(), R.drawable.facciadaculo); int larghezza=bitmap.getWidth(); int altezza=bitmap.getHeight(); LayoutParams params=new LayoutParams(200,100); imageView.setLayoutParams(params); imageView.setScaleType(ScaleType.CENTER); imageView.setImageBitmap(bitmap); imageView.setBackgroundColor(Color.BLACK); mainLayout.addView(imageView); OnTouchListener onTouchListener=new View.OnTouchListener() { @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(); int scrollByX=(int)(X-currentX); int scrollByY=(int)(Y-currentY); v.scrollBy(scrollByX, scrollByY); X=currentX; Y=currentY; break; } return true; } }; imageView.setOnTouchListener(onTouchListener); }Bene.
Annotata questa, procedo a sperimentare un po' oltre...
Provo a usare il metodo scrollTo(int,int).
OnTouchListener onTouchListener=new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { switch(event.getAction()){ case MotionEvent.ACTION_DOWN: imageView.scrollTo(0, 0); break; } return true; } }; imageView.setOnTouchListener(onTouchListener);Giocando con ScaleType e con ScrollTo posso modificare sia la posizione iniziale della bitmap nei confronti della ImageView sia l'entità dello scorrimento della bitmap.
Adesso quello che mi interessa è prendere le coordinate della bitmap.
Se uso ScaleType.MATRIX, ottengo il posizionamento iniziale nell'angolo superiore sinistro della Bitmap, quindi zero.
Ora sposto di 50 e 50:
switch(event.getAction()){ case MotionEvent.ACTION_DOWN: imageView.scrollTo(50,50); break; }
Sposto mediante il Touch di 50,50:
E adesso quindi so di avere una finestra che esplora la bitmap secondo queste coordinate:
Angolo superiore sinistro: 50,50; Angolo inferiore destro: 150,150 (dato che le dimensioni della finestra sono rispettivamente 100 e 100).
Provo quindi a ritagliare un'immagine di queste coordinate e dimensioni.
Per vederla, però, devo creare una nuova ImageView.
Ecco il codice:
imageView =new ImageView(this); bitmap=BitmapFactory.decodeResource(getResources(), R.drawable.facciadaculo); int larghezza=bitmap.getWidth(); int altezza=bitmap.getHeight(); LayoutParams params=new LayoutParams(100,100); imageView.setLayoutParams(params); imageView.setScaleType(ScaleType.MATRIX); imageView.setImageBitmap(bitmap); imageView.setBackgroundColor(Color.BLACK); mainLayout.addView(imageView); imgControllo=new ImageView(this); LayoutParams p=new LayoutParams(100,100); p.leftMargin=10; p.topMargin=200; imgControllo.setLayoutParams(p); mainLayout.addView(imgControllo); OnTouchListener onTouchListener=new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { switch(event.getAction()){ case MotionEvent.ACTION_DOWN: imageView.scrollTo(50,50); Bitmap newBmp=Bitmap.createBitmap(bitmap,50,50,100,100); imgControllo.setImageBitmap(newBmp); break; } return true; } }; imageView.setOnTouchListener(onTouchListener);Questo codice prende la bitmap e, dopo lo spostamento calcolato a partire dall'angolo superiore sinistro della bitmap, calcola il ritaglio da praticare nella bitmap in relazione allo spostamento, ne crea una nuova bitmap e la pone nell'ImageView di controllo.
Ecco: prima dello spostamento:
E dopo lo spostamento con ritaglio e copia nella seconda ImageView.
Bene.
Ecco, ho creato un codice che permette di individuare le coordinate della bitmap sottostante.
Me lo copio qui, grezzo grezzo e con residui di un codice precedente che resta inutilizzato, in modo da poter attingere ad esso in caso di eventuali vuoti di memoria nella riscrittura di un codice fatto meglio
public class MainActivity extends Activity { float X; float Y; float currentX, currentY; ImageView imageView; ImageView imageView2; RelativeLayout mainLayout; RelativeLayout layout; Bitmap bitmap; Matrix matrix; ImageView imgControllo; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mainLayout=(RelativeLayout)findViewById(R.id.mainLayout); int LayLeft, LayTop, LayWidth, LayHeight; int ImgLeft, ImgTop, ImgWidth, ImgHeight; LayLeft=0; LayTop=0; LayWidth=100; LayHeight=100; ImgLeft=0; ImgTop=0; //CREAZIONE DEL LAYOUT layout=new RelativeLayout(this); BitmapDrawable sfondo=(BitmapDrawable)this.getResources().getDrawable(R.drawable.cartellanuova); layout.setBackground(sfondo); //settaggio dei parametri LayoutParams lParams=new LayoutParams(LayWidth,LayHeight); lParams.leftMargin=LayLeft; lParams.topMargin=LayTop; layout.setLayoutParams(lParams); //CREAZIONE DELL'IMMAGINE imageView =new ImageView(this); bitmap=BitmapFactory.decodeResource(getResources(), R.drawable.facciadaculo); int larghezza=bitmap.getWidth(); int altezza=bitmap.getHeight(); LayoutParams params=new LayoutParams(100,100); imageView.setLayoutParams(params); imageView.setScaleType(ScaleType.CENTER); bitmap=Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(),matrix, false); imageView.setImageBitmap(bitmap); imageView.setBackgroundColor(Color.BLACK); mainLayout.addView(imageView); imgControllo=new ImageView(this); LayoutParams p=new LayoutParams(100,100); p.leftMargin=10; p.topMargin=200; imgControllo.setLayoutParams(p); imgControllo.setBackgroundColor(Color.BLACK); mainLayout.addView(imgControllo); OnTouchListener onTouchListener=new View.OnTouchListener() { Matrix matrix=imageView.getImageMatrix(); @Override public boolean onTouch(View v, MotionEvent event) { ((ImageView)v).getImageMatrix().invert(matrix); switch(event.getAction()){ case MotionEvent.ACTION_DOWN: X=event.getX(); Y=event.getY(); float[] pts={0f,0f}; matrix.mapPoints(pts); Log.v(""+pts[0],""+pts[1]); break; } return true; } }; imageView.setOnTouchListener(onTouchListener); } @Override public void onWindowFocusChanged(boolean hasFocus){ super.onWindowFocusChanged(hasFocus); Log.v("LARGHEZZA DI IMAGEVIEW",""+imageView.getWidth()); Log.v("ALTEZZA DI IMAGEVIEW",""+imageView.getHeight()); } }
Nessun commento:
Posta un commento