Java Android:为什么ImageView.getWidth()不';t对应于图像资源宽度?

Java Android:为什么ImageView.getWidth()不';t对应于图像资源宽度?,java,android,imageview,android-imageview,Java,Android,Imageview,Android Imageview,我的包中有一个“image950.png”正方形图像,大小为950x950像素(小于我的屏幕宽度) 当我这样设置时: <ImageView android:id="@+id/myImageView" android:layout_width="wrap_content" android:layout_height="wrap_content&q

我的包中有一个“image950.png”正方形图像,大小为950x950像素(小于我的屏幕宽度)

当我这样设置时:

 <ImageView
                android:id="@+id/myImageView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:scaleType="centerInside"
                android:visibility="visible"
                android:background="#FFFF00"
                android:src="@drawable/image950"
                />
我得到的是831像素而不是950像素。(注:我运行的是三星S20+)

为什么我会得到这个结果,如何得到预期的结果


谢谢

您需要使用位图来获取图像的大小:

Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(), 
        R.drawable.YOURIMAGE);


 Bitmap bitmap = bitmapOrg;
 ByteArrayOutputStream stream = new ByteArrayOutputStream();   
 bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);   
 byte[] imageInByte = stream.toByteArray(); 
 long lengthbmp = imageInByte.length; 

这是实际视图的宽度,而不是图像的宽度,我离我的电脑不近,但你可以尝试获取实际的可绘制内容,我认为context.resources.getDrawable(id).intrinsicWidth???沿着这条线的东西谢谢你的回答。但是如果有足够的空间绘制,为什么实际视图的宽度小于图像的宽度(=资源)?请在ImageView中探索ScaleType属性。这可能对你有很大帮助。谢谢。我尝试了所有ScaleType选项,但总是得到相同的错误值:(可能在视图的父视图上有填充,可能有一个强制执行最小填充或边距的主题,添加您正在使用的整个布局,也许我们可以看到一些东西,也可以检查父视图,它的getWidth()是否正确)还返回831?如果是这样,那么它在某种程度上受到父对象的约束谢谢,但这不是我想要的。我想知道一旦绘制imageView,它的宽度。据我所知,它应该与我的情况下位图的大小相匹配;事实并非如此,我不明白为什么……那么问题是,使用scaleType,您将更改大小va你的形象的价值。。
Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(), 
        R.drawable.YOURIMAGE);


 Bitmap bitmap = bitmapOrg;
 ByteArrayOutputStream stream = new ByteArrayOutputStream();   
 bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);   
 byte[] imageInByte = stream.toByteArray(); 
 long lengthbmp = imageInByte.length;