gpt4 book ai didi

Android SDK 获取点击图像的位置

转载 作者:行者123 更新时间:2023-11-29 00:35:16 27 4
gpt4 key购买 nike

我有一个 ImageView,但我如何才能看到用户在图像上点击/触摸的位置。

我知道我必须使用 MotionEvent.getX()MotionEvent.getY(),但我如何才能看到用户点击了图片的哪个位置?

谢谢!

最佳答案

您必须对 ImageView 进行子类化,并在用户触摸的位置明确绘制一个指示符。

public class TouchableImageView extends ImageView {

// Constructors should come here

// Override onTouch to remember the touch position in our variable touchLocation
// Set touchLocation to null when getting the ACTION_UP event

// Override onDraw to draw something at touchLocation. You should create a proper Paint object
// in your constructors and use it here
public void onDraw(Canvas c) {
if (touchLocation!=null) {
canvas.drawCircle(touchLocation.x, touchLocation.y, 10, indicatorPaint);
}
}

private Point touchLocation;
private Paint indicatorPaint;
}

关于Android SDK 获取点击图像的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13026224/

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