0

我想在触摸图像的地方放置红色圆圈,并有一个类来监听触摸并将坐标发送到另一个类:

public class Report extends Fragment {
private Context activity;
private Point point = new Point();
private DrawingCrl imgCircle = new DrawingCrl();

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.report, container,
            false);
    activity = this.getActivity();

            //Where I'm doing the touching and respond to that:
    final View touchView = rootView.findViewById(R.id.ImageC);
    touchView.setOnTouchListener(new View.OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            point.x = Float.valueOf(event.getX());
            point.y = Float.valueOf(event.getY());
                            touchView = imgCircle.DrawCircle(Point point);
            return true;
        }
    });

    return rootView;

    public class Point {
        float x;
        float y;
}

}

当我调用 DrawCircle 时,我希望 DrawingCrl 类能做到这一点,在我发送给方法的点上为我画一个圆:

public class DrawingCrl {
private Bitmap mBitmap;
private Paint paint;
    private Canvas canvas = new Canvas();

public void DrawCircle(Point point) {
    mBitmap = Bitmap.createBitmap(400, 800, Bitmap.Config.ARGB_8888);
    paint = new Paint();
    paint.setColor(Color.RED);
    paint.setStyle(Style.FILL);
    canvas.drawCircle(point.x, point.y, 50, paint);
}
}

我已经阅读了获取 X 和 Y 坐标以及在触摸时绘制圆和绘制圆,但不知道如何绘制圆。

我对android很陌生,所以很抱歉我的许多菜鸟失败。我希望你能帮助我完成这个工作!谢谢!:)

4

1 回答 1

0

好的,这就是我的做法,但我将稍微更改您的代码。

首先,我会将您的 DrawingCrl 类更改为仅存储位置(可能还有 Paint 对象,但这可能更适合作为所有 DrawingCrls 共享的静态),如下所示:

public class DrawingCrl {
    public Point myLoc;
    public Paint paint;

    public void DrawCircle(Point point) {
        paint = new Paint();
        paint.setColor(Color.RED);
        paint.setStyle(Style.FILL);
        myLoc = point;
    }
}

保存在 Activity 中创建的 DrawingCrl 对象的 Vector,并在 onTouch 事件处理程序中创建新对象。

这在 Report 类中:

java.util.Vector<DrawingCrl> myCircles = new java.util.Vector<DrawingCrl>();

然后在“onTouch”中创建一个新的圆形对象并保存它:

  @Override
  public boolean onTouch(View v, MotionEvent event) {
        point.x = Float.valueOf(event.getX());
        point.y = Float.valueOf(event.getY());

        // create a new Circle here and save it
        DrawingCrl c = new DrawingCrl(point);
        myCircles.add(c);
        return true;
    }

然后我会覆盖你的视图'touchView'的'onDraw'方法。

“onDraw”将为您提供要绘制的画布,因此不要像现在在 DrawingDMG 类中那样创建自己的画布。

@Override
protected void onDraw(Canvas c)
{
  for (DrawingCrl c : myCircles)
  {
    c.drawCircle(c.myLoc.x, c.myLoc.y, 50, c.paint);
  }
}
于 2013-05-12T03:37:57.660 回答