gpt4 book ai didi

java - 为什么没有为我设置 OnTouchListener?

转载 作者:行者123 更新时间:2023-12-01 11:06:12 26 4
gpt4 key购买 nike

我正在开发一个名为 CanvasView 的自定义 View 。这个 View 允许我在 onDraw 方法之外在其上绘制内容。它是这样的:

public class CanvasView extends View {
private ArrayList<Shape> shapes;
private Paint paint;

public CanvasView (Context c) {
super(c);
init ();
}

public CanvasView(Context context, AttributeSet attrs) {
super (context, attrs);
init ();
}

public CanvasView(Context context, AttributeSet attrs, int defStyleAttr) {
super (context, attrs, defStyleAttr);
init ();
}
private void init () {
shapes = new ArrayList<> ();
paint = new Paint ();
paint.setStrokeWidth (5);
paint.setColor (Color.BLACK);
}

//focus on this method, I think the others are irrelevant
@Override
public void setOnTouchListener (final OnTouchListener listener) {
final OnTouchListener baseListener = new OnTouchListener () {
@Override
public boolean onTouch(View v, MotionEvent event) {
float x = event.getX ();
float y = event.getY ();
if (x < 18 || x > getWidth () - 18 || y < 18 ||
y > getHeight () - 18)
return false;

return true;
}
};

super.setOnTouchListener (new OnTouchListener () {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (baseListener.onTouch (v, event)) {
if (listener != null) {
return listener.onTouch (v, event);
} else {
return true;
}
}
return false;
}
});
}

@Override
protected void onDraw (Canvas c) {
super.onDraw (c);
for (Shape s : shapes) {
s.draw (c);
}
//draw the border. irrelevant
c.drawLine (3, 3, getWidth () - 3, 3, paint);
c.drawLine (3, getHeight () - 3, getWidth () - 3, getHeight () - 3, paint);
c.drawLine (3, 3, 3, getHeight () - 3, paint);
c.drawLine (getWidth () - 3, 3, getWidth () - 3, getHeight () - 3, paint);

//draw the inner border
c.drawLine (18, 18, getWidth () - 18, 18, paint);
c.drawLine (18, getHeight () - 18, getWidth () - 18, getHeight () - 18, paint);
c.drawLine (18, 18, 18, getHeight () - 18, paint);
c.drawLine (getWidth () - 18, 18, getWidth () - 18, getHeight () - 18, paint);
}

public void addShape (Shape s) {
shapes.add (s);
}

public void clear () {
shapes.clear ();
}
}

说明:

Shape 是一个带有方法的接口(interface):

public void draw (Canvas c);

重点关注 setOnTouchListener 覆盖。我重写了这个方法,因为我想限制用户不触摸 CanvasView 的边框。如您所见,我首先调用 baseListener 来检查触摸是否在范围内。然后我调用构造函数中传递的监听器。这是一个好的做法吗?

无论如何,我在onCreate方法中设置了CanvasViewOnTouchListener:

canvas.setOnTouchListener (new View.OnTouchListener () {
@Override
public boolean onTouch(View v, final MotionEvent event) {
if (point1 != null && point2 != null) {
throw new IllegalStateException ("Both point1 and point2 are not null");
}

if (point1 == null) {
point1 = new PointF (event.getX (), event.getY ());
} else { //point2 is null
point2 = new PointF (event.getX (), event.getY ());
canvas.addShape (new Shape () {
@Override
public void draw(Canvas c) {
c.drawLine (point1.x, point1.y, point2.x, point2.y, paint);
}
});
canvas.setOnTouchListener (null);
}

canvas.addShape (new Shape () {
@Override
public void draw(Canvas c) {
c.drawCircle (event.getX (), event.getY (), 13, paint);
}
});

return true;
}
});

说明:

point1point2 是在 Activity 类中声明的字段。当用户触摸屏幕时,其中一个将被实例化,并在该点绘制一个小圆圈。当用户第二次触摸时,point2 被实例化,并在两点之间绘制一条线。

当我运行我的应用程序并触摸屏幕时,没有绘制任何内容!我认为 setOnClickListener 重写编写不正确。谁能告诉我为什么?

最佳答案

View 的 onDraw 方法仅在首次创建 View 时调用。要使其再次被调用,每次 View 的内容发生更改时,您都需要使用 View 上的 invalidate() 函数将 View 标记为“脏”(已进行更改)。

因此,调用此函数的一个方便位置是在 addShape 函数中向 View 添加形状之后。

 public void addShape (Shape s) {
shapes.add (s);
this.invalidate();
}

关于java - 为什么没有为我设置 OnTouchListener?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32918272/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com