【问题标题】:Draw on Custom Image view在自定义图像视图上绘制
【发布时间】:2017-07-29 03:55:06
【问题描述】:

我正在开发图像编辑器,它允许用户从图库中选择图像,然后在图像上绘制对象。

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

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

另外,在自定义ImageView的onDraw()方法中,选中的位图的宽高和画布的宽高不同,这是怎么回事?

【问题讨论】:

  • 设置属性 android:scaleType="fitXY",android:layout_width="match_parent" android:layout_height="match_parent"
  • 我不想缩放图像,我需要一种方法来直接在图像的任何位置(中心或顶部)绘制。

标签: android canvas view imageview


【解决方案1】:

选择图像后,制作所选图像的位图并尝试以下代码:

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 的大小。

【讨论】: