android 如何实现 onTouch 功能?

发布于 2024-10-19 20:42:53 字数 756 浏览 0 评论 0原文

您好,我想创建一个加载大图像的应用程序,并且我可以通过简单的手势在它上面移动。我必须打印图像,但我无法实现 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

宣告ˉ结束 2024-10-26 20:42:53

由于您已经在编写自定义视图,因此您可能希望在视图中合并 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 the switch(e.getAction()) thing, because OnGestureListener is on a higher level and will provide you with event already detected as a gesture (scroll, fling, long press...).

See an example here.

落花浅忆 2024-10-26 20:42:53

在您的视图中,您需要创建“OnTouchListener”,它应该看起来像这样:

myView.setOnTouchListener(new View.OnTouchListnener(){
    @Override
    public boolean onTouch(View v, MotionEvent e){
        switch(e.getAction()){
        case MotionEvent.ACTION_DOWN:
        //and code will go here for putting the finger on the screen

我会看看 MotionEvent 并查看各个级别。您需要注意它如何将几位运动信息打包到一个 MotionEvent 中。

Within your view, you need to create the "OnTouchListener" which should look something like this:

myView.setOnTouchListener(new View.OnTouchListnener(){
    @Override
    public boolean onTouch(View v, MotionEvent e){
        switch(e.getAction()){
        case MotionEvent.ACTION_DOWN:
        //and code will go here for putting the finger on the screen

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文