Android 获取ImageView绘制区域的高度和宽度
如果我有一个(800*800像素)的图像视图。当我试图在这个ImageView之外画一条线时,它会画出来Android 获取ImageView绘制区域的高度和宽度,android,imageview,size,android-canvas,Android,Imageview,Size,Android Canvas,如果我有一个(800*800像素)的图像视图。当我试图在这个ImageView之外画一条线时,它会画出来 myBitmap = Bitmap.createBitmap(800,800,Bitmap.Config.ARGB_8888); myCanvas = new Canvas(myBitmap); myPaint = new Paint(); myImageView.setImageBitmap(myBitmap); myCanvas.drawLine(
myBitmap = Bitmap.createBitmap(800,800,Bitmap.Config.ARGB_8888);
myCanvas = new Canvas(myBitmap);
myPaint = new Paint();
myImageView.setImageBitmap(myBitmap);
myCanvas.drawLine(-100, -100, 600, 600, myPaint);
即使起点在外面
现在我想得到它们的总尺寸,我的意思是[900900]。我不知道可以从哪个组件获得它(myCanvas
或myBitmap
或myImageView
)
我希望我的问题是清楚的
多谢各位 我想你需要的是: 使用
ImageView.getDrawable().getInstrinsicWidth()
和getIntrinsicHeight()
都将返回原始尺寸
获取显示图像的实际尺寸的唯一方法是提取并使用用于显示图像的变换矩阵。这必须在测量阶段之后完成,这里的示例显示它在自定义图像视图的onMeasure()
的覆盖中调用:
public class SizeAwareImageView extends ImageView {
public SizeAwareImageView(Context context) {
super(context);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
// Get image matrix values and place them in an array
float[] f = new float[9];
getImageMatrix().getValues(f);
// Extract the scale values using the constants (if aspect ratio maintained, scaleX == scaleY)
final float scaleX = f[Matrix.MSCALE_X];
final float scaleY = f[Matrix.MSCALE_Y];
// Get the drawable (could also get the bitmap behind the drawable and getWidth/getHeight)
final Drawable d = getDrawable();
final int origW = d.getIntrinsicWidth();
final int origH = d.getIntrinsicHeight();
// Calculate the actual dimensions
final int actW = Math.round(origW * scaleX);
final int actH = Math.round(origH * scaleY);
}
}
注意:一般来说,要从代码中获取图像转换矩阵(如在活动中),函数是ImageView.getImageMatrix()-例如myImageView.getImageMatrix()我认为在位图
外部绘制的内容丢失了,您可以通过旋转图像视图来检查这一点,您将看到,在位图
之外的是黑色,即使您绘制了位图 可能与否重复我看到了这一点,当使用myImageView.getDrawable().getIntrinsicWidth()
和myImageView.getDrawable().getIntrinsicHeight()
时,它给了我800 X 800
我的画布.getWidth()和myCanvas.getHeight()
give?这也给了我800X800@RiadSaadi是否要查找形成的图像的尺寸(包括轮廓)?是的,我要所有绘制区域的尺寸。例如,如果我画另一条线(10,10)到(10001000),我将从(-100,-100)到(10001000),这将给出宽度=1100,高度=1100,我很抱歉,但即使这样,它也给出了原始尺寸800X800,