2015-12-03 133 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。 –