Android 插入后如何在自定义ImageView中获取位图宽度和高度
有人能帮我得到位图的宽度后,它被放置在一个自定义的imageview 这是我当前的代码,但此代码仅获取imageview的宽度Android 插入后如何在自定义ImageView中获取位图宽度和高度,android,imageview,Android,Imageview,有人能帮我得到位图的宽度后,它被放置在一个自定义的imageview 这是我当前的代码,但此代码仅获取imageview的宽度 // is place in the Activity @Override public void onWindowFocusChanged(boolean hasFocus){ int bW = myImageV.getWidth(); int bH = myImageV.getHeight(); { 选项将帮助您在将位图加载到自定义图像视图之前获取位图的宽度和
// is place in the Activity
@Override
public void onWindowFocusChanged(boolean hasFocus){
int bW = myImageV.getWidth();
int bH = myImageV.getHeight();
{
选项将帮助您在将位图加载到自定义图像视图之前获取位图的宽度和高度。试试这个
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(getResources(), R.id.myimage, options);
int imageHeight = options.outHeight;
int imageWidth = options.outWidth;
将位图添加到自定义图像视图后,如果要计算位图的宽度和高度,应该像这样使用Ract和canvas,简单地说,Ract基本上是使用canvas绘制位图的绑定区域
Drawable drawable = myImageView.getDrawable();
//you should call after the bitmap drawn
Rect bounds = drawable.getBounds();
int width = bounds.width();
int height = bounds.height();
Matrix m = new Matrix();
m.set(myImageView.getImageMatrix());
float[] values = new float[9];
m.getValues(values);
int bitmapWidth = values[Matrix.MSCALE_X]*drawable.getIntrinsicWidth(); //your bitmap's width
int bitmapHeight = values[Matrix.MSCALE_Y]*drawable.getIntrinsicHeight(); //your the bitmap's height
嗨@Radhe。我们要把你的密码放进去。?是在myImageV.setImageDrawable(drawable1)之后吗;?我想要自定义imageview中位图的宽度作为高度。谢谢在图像视图内设置位图之前,是否要计算宽度和高度?请参考此号码。抱歉误解。我想在自定义imageview sir.Hi@Radhe上显示位图后,获取位图的宽度和高度。你应该阅读我的最新问题。我正在寻找一种方法,以获得w和高度的位图后,被放在自定义图像视图,而不是在把它。这有助于我浮动宽度=值[Matrix.MSCALE_X]*backgroundImage.getDrawable().getIntrinsicWidth();我从matrixHi@Radhe那里得到的,我编辑了你的答案。单独使用drawable.getIntrinsicWidth()和drawable.getIntrinsicHeight()将无法获得正确的宽度。因为我的自定义imageview设置为scaletype=fitCenter。因此,它可能会扩大规模。我不需要矩形边界。