在android中绘制自定义图像视图
我正在开发一个绘图应用程序。我已经创建了一个绘图视图,允许用户在视图上绘图 问题是,当我在上面画画时,它超出了图片的面积请参考图片,黄线超出了实际图片的面积,我如何将画布大小粘贴到实际图片上 imageview如何可缩放?这意味着当用户点击画笔按钮时,它会画图,当用户再次点击时,它会变成缩放功能 谢谢。缩放功能可以在以后处理,超出面积的问题需要先解决 以下是xml中的自定义绘图视图在android中绘制自定义图像视图,android,canvas,android-imageview,android-view,android-drawable,Android,Canvas,Android Imageview,Android View,Android Drawable,我正在开发一个绘图应用程序。我已经创建了一个绘图视图,允许用户在视图上绘图 问题是,当我在上面画画时,它超出了图片的面积请参考图片,黄线超出了实际图片的面积,我如何将画布大小粘贴到实际图片上 imageview如何可缩放?这意味着当用户点击画笔按钮时,它会画图,当用户再次点击时,它会变成缩放功能 谢谢。缩放功能可以在以后处理,超出面积的问题需要先解决 以下是xml中的自定义绘图视图 <com.example.tool.DrawView android:id="@+id/
<com.example.tool.DrawView
android:id="@+id/draw"
android:layout_width="match_parent"
android:layout_height="match_parent" />
非常感谢您的帮助如果在ImageView的属性中有一个真实的属性adjustViewBounds,您可以通过编程将DrawView的宽度设置为ImageView的宽度。首先计算显示的图像大小,然后将eventX和eventY钳制到显示图像的边界。代码如下:
int imageViewH=imageView.getMeasuredHeight();//height of imageView
int imageViewW =imageView.getMeasuredWidth();//width of imageView
int drawableH =imageView.getDrawable().getIntrinsicHeight();//original height of underlying image
int drawableW=imageView.getDrawable().getIntrinsicWidth();//original width of underlying image
int displayH, displayW; // the shown height and width of the picture.
int leftX, rightX, topY, bottomY; // the shown edges of the picture.
if (imageViewH/drawableH <= imageViewW/drawableW){
displayW = drawableW*imageViewH/drawableH;//rescaled width of image within ImageView
displayH = imageViewH;
leftX = (imageViewW - displayW)/2; // left edge of the displayed image.
rightX = leftX + displayW; // right edge of the displayed image.
topY = 0; // top edg
bottomY = displayH; // bottom edge.
}else{
displayH = drawableH*imageViewW/drawableH;//rescaled height of image within ImageView
displayW = imageViewW;
leftX = 0; // left edge of the displayed image.
rightX = displayW; // right edge of the displayed image.
topY = (imageViewH - displayH)/2; // top edg
bottomY = topY + displayH; // bottom edge.
}
//TODO: clamp the eventX and eventY to the bound of leftX, rightX, topY, bottomY
注意:只有在ImageView具有测量的高度和可绘制的图形后,才应使用该代码 是的,我可以在缩放后返回宽度和高度。对不起,我猜代码放在主活动中了?你能解释一下在我得到H,W,leftX,rightX,topY,bottomY之后如何设置画布吗?谢谢,我认为该代码可以在onTouchEvent中使用,但我不确定这是否是最有效的方法。这是一种在不缩放的情况下计算显示图片边界的方法,缩放后需要更改计算。我已经测试了将代码放入自定义视图触屏案例,但这是否意味着我也需要从主活动中传入imageView?由于自定义绘图视图中不存在imageview,而且钳制事件X Y意味着在添加绘图夹持器列表之前检查其是否超出区域?对不起,有这么多问题。谢谢您的帮助不,您不需要通过imageView。因为您在DrawView中使用了代码,所以imageView等于。钳制事件X Y意味着确保传递给path.moveTo和path.lineTo方法的参数不会超过图片的区域。谢谢,我现在在xml中添加了adjustViewBound,如果有一些关于以编程方式将DrawView的宽度设置为ImageView的宽度的代码会更好。我认为您可以在以下位置限制线条绘图区域:MotionEvent.ACTION\u MOVE:holderList.getholderList.size-1.path.lineToeventX,eventY;打破
DrawView pic = findViewById(R.id.draw);
pic.setImageBitmap(bitmap);
int imageViewH=imageView.getMeasuredHeight();//height of imageView
int imageViewW =imageView.getMeasuredWidth();//width of imageView
int drawableH =imageView.getDrawable().getIntrinsicHeight();//original height of underlying image
int drawableW=imageView.getDrawable().getIntrinsicWidth();//original width of underlying image
int displayH, displayW; // the shown height and width of the picture.
int leftX, rightX, topY, bottomY; // the shown edges of the picture.
if (imageViewH/drawableH <= imageViewW/drawableW){
displayW = drawableW*imageViewH/drawableH;//rescaled width of image within ImageView
displayH = imageViewH;
leftX = (imageViewW - displayW)/2; // left edge of the displayed image.
rightX = leftX + displayW; // right edge of the displayed image.
topY = 0; // top edg
bottomY = displayH; // bottom edge.
}else{
displayH = drawableH*imageViewW/drawableH;//rescaled height of image within ImageView
displayW = imageViewW;
leftX = 0; // left edge of the displayed image.
rightX = displayW; // right edge of the displayed image.
topY = (imageViewH - displayH)/2; // top edg
bottomY = topY + displayH; // bottom edge.
}
//TODO: clamp the eventX and eventY to the bound of leftX, rightX, topY, bottomY