我想在触摸图像的位置放置红色圆圈,并有一个听取触摸的类并将坐标发送到另一个类:
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);
}
}
我已阅读getting X and Y coordinates and drawing circle和Draw Circle on touch但未了解如何绘制圆圈。
我对android很新,所以对不起我的许多noob失败。我希望你能帮助我实现这个目标!谢谢! :)
答案 0 :(得分: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对象的向量,并在onTouch事件处理程序中创建新对象。
这在报告类中:
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;
}
然后我会覆盖View'touchView'的'onDraw'方法。
'onDraw'将为您提供要绘制的Canvas,所以不要像现在在DrawingDMG类中那样创建自己的。
@Override
protected void onDraw(Canvas c)
{
for (DrawingCrl c : myCircles)
{
c.drawCircle(c.myLoc.x, c.myLoc.y, 50, c.paint);
}
}