有人可以帮助我在位图放入自定义imageview之后获取位图的宽度。
这是我当前的代码,但此代码仅获取imageview的宽度。
// is place in the Activity
@Override
public void onWindowFocusChanged(boolean hasFocus){
int bW = myImageV.getWidth();
int bH = myImageV.getHeight();
{
答案 0 :(得分:1)
BitmapFactory.Options将帮助您在将位图加载到自定义ImageView之前获取位图的宽度和高度。试试这个..
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基本上是使用画布绘制位图的绑定区域。
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