2013-05-21 141 views

回答

3

我建議做以下方式(該方法類似於一個在this question)。

E.g.您有以下XML(我不知道什麼是標題和標籤使它們被錯過):

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

然後活動可能如下所示:

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

在小圖像的情況下,將會記錄:圖像完全可見。但是,您應該瞭解以下不一致性(根據我的理解):如果您的圖像很大,但是要縮放(例如,您設置爲android:layout_width="wrap_content"),但它的外觀會縮放,但實際的高度將爲ImageView作爲圖像的全高(並且ScrollView將甚至滾動),因此可能需要adjustViewBounds。這種行爲的原因是,FrameLayoutdoesn't care about layout_width and layout_height of childs

相關問題