android 如何实现 onTouch 功能?
您好,我想创建一个加载大图像的应用程序,并且我可以通过简单的手势在它上面移动。我必须打印图像,但我无法实现 onTouch,因此它保持静止。任何帮助都可以。感谢
我绘制图片的代码:
@Override protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Paint paint = new Paint();
paint.setStyle(Paint.Style.FILL);
// make the entire canvas white
paint.setColor(Color.WHITE);
canvas.drawPaint(paint);
paint.setStrokeWidth(1);
paint.setPathEffect(null);
paint.setColor(Color.GRAY);
paint.setAntiAlias(true);
for (int i=1; i < 100; i++){
canvas.drawLine(0, i*1 , 600, i*20, paint);
canvas.drawLine(i*1 ,0, i*20, 600, paint);
}
}
Hi I want to create aplication which loads large image, and simple gesture I can move across it. I have to image printed out but I can not implement onTouch so it remains stationary. Any help apreseated. Thanks
My code for drawing out the picture:
@Override protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Paint paint = new Paint();
paint.setStyle(Paint.Style.FILL);
// make the entire canvas white
paint.setColor(Color.WHITE);
canvas.drawPaint(paint);
paint.setStrokeWidth(1);
paint.setPathEffect(null);
paint.setColor(Color.GRAY);
paint.setAntiAlias(true);
for (int i=1; i < 100; i++){
canvas.drawLine(0, i*1 , 600, i*20, paint);
canvas.drawLine(i*1 ,0, i*20, 600, paint);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
由于您已经在编写自定义视图,因此您可能希望在视图中合并
GestureDetector
和侦听器,而不是设置侦听器,最重要的是避免switch(e.getAction( ))
的事情,因为OnGestureListener
处于更高的级别,将为您提供已检测为手势的事件(滚动、快速滑动、长按...)。请参阅此处的示例。
As you're already writing a custom view, instead of setting a listener, you might want to incorporate a
GestureDetector
and listener inside your view, and above all avoid theswitch(e.getAction())
thing, becauseOnGestureListener
is on a higher level and will provide you with event already detected as a gesture (scroll, fling, long press...).See an example here.
在您的视图中,您需要创建“OnTouchListener”,它应该看起来像这样:
我会看看 MotionEvent 并查看各个级别。您需要注意它如何将几位运动信息打包到一个
MotionEvent 中。
Within your view, you need to create the "OnTouchListener" which should look something like this:
I would have a look at MotionEvent and looking at the various levels. You'll want to pay attention to how it can pack several bits of movement information into one
MotionEvent.