2013-05-21 18 views

risposta

3

vorrei suggerire di fare nel seguente modo (l'approccio è simile a quello in this question).

E.g. Lei ha il seguente codice XML (io non sono sicuro di quello che sono intestazione e le schede in modo che siano perse):

<ScrollView 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_height="match_parent" 
    android:layout_width="match_parent" 
    android:id="@+id/scroller"> 
     <ImageView 
      android:layout_height="wrap_content" 
      android:layout_width="wrap_content" 
      android:layout_gravity="center" 
      android:id="@+id/image" 
      android:src="@drawable/image001" 
      android:scaleType="fitXY" /> 
</ScrollView> 

Poi l'attività potrebbe essere simile al seguente:

public class MyActivity extends Activity { 

    private static final String TAG = "MyActivity"; 

    private ScrollView mScroll = null; 
    private ImageView mImage = null; 

    private ViewTreeObserver.OnGlobalLayoutListener mLayoutListener = new ViewTreeObserver.OnGlobalLayoutListener() { 
     @Override 
     public void onGlobalLayout() { 
      final Rect imageRect = new Rect(0, 0, mImage.getWidth(), mImage.getHeight()); 
      final Rect imageVisibleRect = new Rect(imageRect); 

      mScroll.getChildVisibleRect(mImage, imageVisibleRect, null); 

      if (imageVisibleRect.height() < imageRect.height() || 
        imageVisibleRect.width() < imageRect.width()) { 
       Log.w(TAG, "image is not fully visible"); 
      } else { 
       Log.w(TAG, "image is fully visible"); 
      } 

      mScroll.getViewTreeObserver().removeOnGlobalLayoutListener(mLayoutListener); 
     } 
    }; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     // Show the layout with the test view 
     setContentView(R.layout.main); 

     mScroll = (ScrollView) findViewById(R.id.scroller); 
     mImage = (ImageView) findViewById(R.id.image); 

     mScroll.getViewTreeObserver().addOnGlobalLayoutListener(mLayoutListener); 
    } 
} 

In caso di piccola immagine che registrerà: l'immagine è completamente visibile.

Tuttavia, si dovrebbe essere consapevoli di quanto segue incoerenza (come per la mia comprensione): se avete grande immagine, ma facendo scalare ad esso (ad esempio, si imposta android:layout_width="wrap_content") quando avrà l'aspetto in scala, ma effettiva ImageView altezza sarà come a tutta altezza dell'immagine (e ScrollView sarà anche a scorrimento), quindi potrebbe essere necessario adjustViewBounds. La ragione di questo comportamento è che FrameLayoutdoesn't care about layout_width and layout_height of childs.

Problemi correlati