4

嗨,我想创建一个可以加载大图像的应用程序,并且我可以通过简单的手势移动它。我必须将图像打印出来,但我无法实现 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);      
            }
}
4

2 回答 2

6

由于您已经在编写自定义视图,而不是设置侦听器,您可能希望GestureDetector在视图中合并一个和侦听器,最重要的是避免这种switch(e.getAction())情况,因为OnGestureListener它处于更高级别,将为您提供已检测为的事件一个手势(滚动、投掷、长按……)。

请参阅此处的示例。

于 2011-02-27T20:58:43.743 回答
3

在您的视图中,您需要创建应如下所示的“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.

于 2011-02-27T20:36:52.897 回答