我试图计算这个形象,这将从网络使用此行 imgLoader.DisplayImage(URL,R.drawable.thumbnail_background,图像)下载尺寸;
。
的问题是,orgHeight变为零,并且无法除以零。但是,为什么是这样的 orgHeight
0?
//添加ImageView的,并计算其尺寸
//假设你的布局是一个的LinearLayout作为它的根
的LinearLayout布局=(的LinearLayout)findViewById(R.id.layout);
ImageView的形象=(ImageView的)findViewById(R.id.photo);
ImageLoader的imgLoader =新ImageLoader的(getApplicationContext());
imgLoader.DisplayImage(URL,R.drawable.thumbnail_background,图像);
INT newHeight = getWindowManager()getDefaultDisplay()的getHeight()/ 2。。;
INT orgWidth = image.getWidth();
INT orgHeight = image.getHeight();
//仔细检查我的数学,这应该是对的,尽管
INT newWidth =(INT)Math.floor((orgWidth * newHeight)/ orgHeight);
//使用RelativeLayout.LayoutParams如果你的父母是一个RelativeLayout的
LinearLayout.LayoutParams PARAMS =新LinearLayout.LayoutParams(
newWidth,newHeight);
image.setLayoutParams(PARAMS);
image.setScaleType(ImageView.ScaleType.CENTER_CROP);
layout.updateViewLayout(图像,则params);
我的ImageView的XML是这样的:
< ImageView的
机器人:ID =@ + ID /摄
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_weight =1
/>
解决方案
的意见尚未铺设在的onCreate()
方法,因此它们的尺寸都为0帖子一个的Runnable
从的onCreate()
,以获得正确的值:
image.post(新的Runnable(){
@覆盖
公共无效的run(){
INT newHeight = getWindowManager()getDefaultDisplay()的getHeight()/ 2。。;
INT orgWidth = image.getWidth();
INT orgHeight = image.getHeight();
//仔细检查我的数学,这应该是对的,尽管
INT newWidth =(INT)Math.floor((orgWidth * newHeight)/ orgHeight);
//使用RelativeLayout.LayoutParams如果你的父母是一个RelativeLayout的
LinearLayout.LayoutParams PARAMS =新LinearLayout.LayoutParams(
newWidth,newHeight);
image.setLayoutParams(PARAMS);
image.setScaleType(ImageView.ScaleType.CENTER_CROP);
layout.updateViewLayout(图像,则params);
}
});
I'm trying to calculate the dimension for this image, which will be downloaded from the web using this lineimgLoader.DisplayImage(url, R.drawable.thumbnail_background, image);
.
The problem is that orgHeight becomes zero and you can't divide by zero. But why is this orgHeight
0?
// Add the imageview and calculate its dimensions
//assuming your layout is in a LinearLayout as its root
LinearLayout layout = (LinearLayout)findViewById(R.id.layout);
ImageView image = (ImageView)findViewById(R.id.photo);
ImageLoader imgLoader = new ImageLoader(getApplicationContext());
imgLoader.DisplayImage(url, R.drawable.thumbnail_background, image);
int newHeight = getWindowManager().getDefaultDisplay().getHeight() / 2;
int orgWidth = image.getWidth();
int orgHeight = image.getHeight();
//double check my math, this should be right, though
int newWidth = (int) Math.floor((orgWidth * newHeight) / orgHeight);
//Use RelativeLayout.LayoutParams if your parent is a RelativeLayout
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
newWidth, newHeight);
image.setLayoutParams(params);
image.setScaleType(ImageView.ScaleType.CENTER_CROP);
layout.updateViewLayout(image, params);
My imageView xml is like this:
<ImageView
android:id="@+id/photo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
/>
解决方案
The views aren't yet laid in the onCreate()
method so their dimensions are 0. Post a Runnable
from onCreate()
to get the proper values:
image.post(new Runnable() {
@Override
public void run() {
int newHeight = getWindowManager().getDefaultDisplay().getHeight() / 2;
int orgWidth = image.getWidth();
int orgHeight = image.getHeight();
//double check my math, this should be right, though
int newWidth = (int) Math.floor((orgWidth * newHeight) / orgHeight);
//Use RelativeLayout.LayoutParams if your parent is a RelativeLayout
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
newWidth, newHeight);
image.setLayoutParams(params);
image.setScaleType(ImageView.ScaleType.CENTER_CROP);
layout.updateViewLayout(image, params);
}
});