2017-07-28 30 views
0

我正在使用图像编辑器,它允许用户从图库中选择图像,然后在图像上绘制对象。在自定义图像视图上绘图

我创建了一个自定义的ImageView并使用所选图像调用了setImageBitmap。

问题是,图像被放置在屏幕的中心,当我试图画一个矩形形式(0,0)到(100,100)时,它从左上角开始绘制屏幕不是图像。

此外,所选位图的宽度和高度与自定义ImageView中onDraw()方法中画布的宽度和高度不同。是这样吗?

+0

设置属性android:scaleType =“fitXY”,android:layout_width =“match_parent” android:layout_height =“match_parent” – huk

+0

我不想缩放图像,我需要一种方式在任何地方直接绘制图像它是(中心或顶部)。 –

回答

0

选择图像后,请选择图像的位图,并尝试下面的代码:

int currentBitmapWidth = bitMap.getWidth(); 
int currentBitmapHeight = bitMap.getHeight(); 

int ivWidth = imageView.getWidth(); 
int ivHeight = imageView.getHeight(); 
int newWidth = ivWidth; 

newHeight = (int) Math.floor((double) currentBitmapHeight *((double) 
new_width/(double) currentBitmapWidth)); 

Bitmap newbitMap = Bitmap.createScaledBitmap(bitMap, newWidth, 
newHeight, true); 

imageView.setImageBitmap(newbitMap) 

这将图片设置为您的ImageView的大小。