这是我活动的一部分:

private ImageView mImageView;
private int resource;

@Override
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  resource = getIntent().getIntExtra("res", -1);

  Matrix initMatrix = new Matrix();

  mImageView = new ImageView(getApplicationContext());
  mImageView.setScaleType( ImageView.ScaleType.MATRIX );
  mImageView.setImageMatrix( initMatrix );
  mImageView.setBackgroundColor(0);
  mImageView.setImageResource(resource);
}
.

我尝试在imageView中显示图像,使用矩阵为比例类型(我想稍后添加Multitouch)。但在用户开始交互之前,我希望将图像居中并符合ImageView。 我已经找到了有关如何解决它的答案,但我有一个问题: 要使用矩阵居中的图像,我需要知道其宽度和高度。当所有你拥有的是 int资源时,有没有办法获得图像大小

有帮助吗?

解决方案

使用bitmapfactory.decoderesource 获取资源的位图对象,然后从位图中可以轻松地检索 getheight getwidth

也不要忘记回收你的位图

编辑:

这种方式,您将获得一个生成的gentacicetagcode位图作为输出,但位数曲率为位和高度为位图设置。所以,在这种情况下,你不需要回收位图

BitmapFactory.Options dimensions = new BitmapFactory.Options(); 
dimensions.inJustDecodeBounds = true;
Bitmap mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.bitmap, dimensions);
int height = dimensions.outHeight;
int width =  dimensions.outWidth;
.

其他提示

对于没有读取DMON的评论的任何人。要执行此操作的代码如下所示:

final Options opt = new BitmapFactory.Options();
opt.inJustDecodeBounds = true;
BitmapFactory.decodeResource(getResources(), R.drawable.your_photo, opt);

opt.outHeight; // height of resource
opt.outWidth; // width of resource
.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top