简体   繁体   English

如何通过在Android中指定x和y坐标在画布上绘制的位图图像上绘制点?

[英]How to Draw a point on over a bitmap image drawn on a canvas by specifying the x and y co-ordinate in android?

I have drawn a bitmap image over a canvas. 我已经在画布上绘制了位图图像。

Bitmap image = BitmapFactory.decodeResource(getResources(), R.drawable.sq);
        canvas.drawColor(color.black);  
        Rect dstRectForRender = new Rect(0,0,320,450);               
        canvas.drawBitmap(image, null,dstRectForRender,null);   

The image gets displayed based on my screnn on a cnavs. 该图像基于我在cnavs上的屏幕显示。 On my touch input, I need to pass the x and y co-ordinate position of the image and fill that pixel with a color to show that the image is painted on a drag event. 在触摸输入上,我需要传递图像的x和y坐标位置,并用一种​​颜色填充该像素,以显示图像是在拖动事件中绘制的。 How can I pass the x and y coo-ordinate parameters? 如何传递x和y坐标参数? Which functions should I use to plot the pixels on the image? 我应该使用哪些功能在图像上绘制像素? I appreciate your help and sweet time. 感谢您的帮助和美好的时光。

I'm not sure if this is the best way to do this, but it would be much easier to do this if you defined your own subclass of ImageView and name it something like DrawableImageView . 我不确定这是否是执行此操作的最佳方法,但是如果定义了自己的ImageView子类并将其命名为DrawableImageView ,则执行此操作会容易得多。 You'd have to make sure you implement all the basic constructors from ImageView , then override the onTouchEvent method. 您必须确保实现了ImageView所有基本构造函数,然后重写onTouchEvent方法。 From that event you can get the touch coordinates and store them in an ArrayList<Point> and use that ArrayList by overriding the onDraw method and "painting" the image. 从该事件中,您可以获得触摸坐标并将其存储在ArrayList<Point>并通过覆盖onDraw方法并“绘制”图像来使用该ArrayList。

public class DrawableImageView extends ImageView {

ArrayList<Point> list = new ArrayList<Point>();    

//constructors..

@Override
public boolean onTouchEvent (MotionEvent event) {
    float x = event.getX();
    float y = event.getY();
    list.add(new Point(x,y));
    invalidate();
}

This is just a very brief overview of how to start your class, and may not be the most accurate way of doing things (depending on your specific code). 这只是关于如何开始上课的非常简短的概述,并且可能不是最准确的处理方法(取决于您的特定代码)。 Now, instead of using <ImageView> tags in your xml (or, loading an ImageView programatically), you refer to your subclass like so: 现在,您可以像这样引用子类,而不是在xml中使用<ImageView>标签(或以编程方式加载ImageView):

<your.package.name.DrawableImageView
/>

Edit 编辑

In response to your comment, there is no predetermined way to draw over an image. 根据您的评论,没有预定的方式绘制图像。 You must implement this yourself, which is why I recommended storing Points in an ArrayList. 您必须自己实现这一点,这就是为什么我建议将Points存储在ArrayList中的原因。 I'm not really sure what you're trying to achieve here, but to draw (for example) black dots over an image you have to override onDraw : 我不太确定您要在这里实现什么,但是要在图像上绘制(例如)黑点,您必须覆盖onDraw

public void onDraw(Canvas c) {
    super.onDraw(c);
    for(Point p : list) {

        //Draw black point at x and y.. I'm posting from my cell so I can't go into much detail
   }
}

Also, to force a View to redraw itself you need to use the invalidate() method in your onTouchEvent() (which I've added above). 另外,要强制视图重绘自身,您需要在onTouchEvent()使用invalidate()方法(我已经在上面添加了)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM