Fre*_*ind 13 android image drawable image-size
代码很简单:
<ImageView android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="@drawable/cat"/>
Run Code Online (Sandbox Code Playgroud)
注意ImageView用于fill_parent
宽度和高度.
图像cat
是一个小图像,它将放大以适合ImageView,并同时保持宽高比.
我的问题是如何获得图像的显示尺寸?我试过了:
imageView.getDrawable().getIntrinsicHeight()
Run Code Online (Sandbox Code Playgroud)
但它是图像的原始高度cat
.
我试过了:
imageView.getDrawable().getBounds()
Run Code Online (Sandbox Code Playgroud)
但哪个回归Rect(0,0,0,0)
.
小智 48
以下将有效:
ih=imageView.getMeasuredHeight();//height of imageView
iw=imageView.getMeasuredWidth();//width of imageView
iH=imageView.getDrawable().getIntrinsicHeight();//original height of underlying image
iW=imageView.getDrawable().getIntrinsicWidth();//original width of underlying image
if (ih/iH<=iw/iW) iw=iW*ih/iH;//rescaled width of image within ImageView
else ih= iH*iw/iW;//rescaled height of image within ImageView
Run Code Online (Sandbox Code Playgroud)
(iw x ih)现在表示视图中图像的实际重新缩放(宽度x高度)(换句话说,显示图像的大小)
编辑:我认为一个更好的方式来编写上面的答案(和一个使用int的答案):
final int actualHeight, actualWidth;
final int imageViewHeight = imageView.getHeight(), imageViewWidth = imageView.getWidth();
final int bitmapHeight = ..., bitmapWidth = ...;
if (imageViewHeight * bitmapWidth <= imageViewWidth * bitmapHeight) {
actualWidth = bitmapWidth * imageViewHeight / bitmapHeight;
actualHeight = imageViewHeight;
} else {
actualHeight = bitmapHeight * imageViewWidth / bitmapWidth;
actualWidth = imageViewWidth;
}
return new Point(actualWidth,actualHeight);
Run Code Online (Sandbox Code Playgroud)
这是一个帮助函数,用于获取imageView中图像的边界。
/**
* Helper method to get the bounds of image inside the imageView.
*
* @param imageView the imageView.
* @return bounding rectangle of the image.
*/
public static RectF getImageBounds(ImageView imageView) {
RectF bounds = new RectF();
Drawable drawable = imageView.getDrawable();
if (drawable != null) {
imageView.getImageMatrix().mapRect(bounds, new RectF(drawable.getBounds()));
}
return bounds;
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
16483 次 |
最近记录: |