2015-12-03 31 views
-1

我用imageView來保存位圖,e,g當前位圖有100 x 100像素。使用下面的xml文件。imageView中的位圖高度和寬度

<ImageView 
android:id="@+id/pimage" 
android:layout_height="wrap_content" 
android:layout_width="wrap_content"/> 

但隨着imageView xml文件的變化,位圖的高度和寬度也發生變化。

<ImageView 
android:id="@+id/image" 
android:layout_height="400dp" 
android:layout_width="match_parent" 
android:layout_alignRight="@+id/colorbar"/> 

如何找到新的位圖高度和寬度?我需要使用高度和寬度作爲全局參數,我可以在同一個類中使用其他函數。

final int[] imageWidth = new int[1]; 
    vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { 
    @SuppressWarnings("deprecation") 
    @Override 
    public void onGlobalLayout() { 
     //get Image Width and height here 
     imageWidth[0] = image.getWidth(); 
     int imageHeight = image.getHeight(); 
     //Then remove layoutChange Listener 
     ViewTreeObserver vto = image.getViewTreeObserver(); 
     Log.d("Height12", Integer.toString(imageWidth[0])); 
     //phaseimage.getViewTreeObserver().removeOnPreDrawListener(this); 
     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { 
      vto.removeOnGlobalLayoutListener(this); 
     } else { 
      vto.removeGlobalOnLayoutListener(this); 
     } 
    } 
}); 
Log.d("Height12", Integer.toString(imageWidth[0])); 

裏面的功能是728以外的0

+0

你需要掛接到觸發之後的ImageView的準備顯示 – Bhargav

+0

您是否嘗試過我的回答@Sulabh蒂瓦里 –

+0

等一下我現在正在測試它,但是我用viewTreeObserver之前,但問題是事件該初始值始終爲0 –

回答

0

終於讓我找到解決我的問題。

int imageWidth; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.phase); 
    onWindowFocusChanged(hasWindowFocus()); 
    Log.d("log", Integer.toString(imageWidth))} 

@Override 
public void onWindowFocusChanged(boolean hasFocus){ 
    int width=phaseimage.getWidth(); 
    int height=phaseimage.getHeight(); 
    imageWidth = width; 
    } 
1

設定位圖到的ImageView後,使用ViewtreeObserver象下面這樣:

//Your Imageview Reference 
ImageView imageView = (ImageView) findViewById(R.id.imageView); 

final ViewTreeObserver vto = imageView.getViewTreeObserver(); 
vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { 
    @SuppressWarnings("deprecation") 
    @Override 
    public void onGlobalLayout() { 
     //get Image Width and height here 
     int imageWidth = imageView.getWidth(); 
     int imageHeight = imageView.getHeight(); 

     //Call anyfunction which uses width and height here 
     function(imageWidth,imageHeight); 

     //Then remove layoutChange Listener 
     ViewTreeObserver vto = imageView.getViewTreeObserver(); 
     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { 
      vto.removeOnGlobalLayoutListener(this); 
     } else { 
      vto.removeGlobalOnLayoutListener(this); 
     } 
    } 
}); 
+0

這將工作,我試過這個按鈕之前。並且您必須確保您的內容已設置。如果你在oncreate中嘗試這個,那麼你可能會得到一些錯誤。否則這個答案正在工作(嘗試並用按鈕測試) –

+0

在設置完所有內容後在oncreate()中,如果你使用它,它會起作用,因爲'onGlobalLayout ()'在viewTree更改時被調用。 –

+0

我在onCreate中使用了此方法,並且無法在此行中識別linearlayout ViewTreeObserver vto = linearLayout.getViewTreeObserver();此外,我無法在此方法之外使用imageWidth。 –