【问题标题】:imageview does not move on touch event androidimageview不会在触摸事件android上移动
【发布时间】:2011-10-14 02:07:03
【问题描述】:

当 ontouch 事件被调用时,我的图像视图没有移动......虽然我可以从日志跟踪中看到 ACTION_UP 和 ACTION_MOVE。

这是我的代码:-

public boolean onTouch(View v, MotionEvent motion) {



     float dy ;
     float dx;
     double r = 0;
     float angle = 0;

     switch(v.getId())
     {
        case R.id.arrow:
            switch(motion.getActionMasked())
            {
                case MotionEvent.ACTION_MOVE:
                    Log.w(this.getClass().getName(),"In MOTION MOVE");
                    mX1 = motion.getX();
                    mY1 = motion.getY();
                    break;

               case MotionEvent.ACTION_UP:

                  Log.w(this.getClass().getName(),"In MOTION UP");
                  mX2 = motion.getX();
                  mY2 = motion.getY();
                dy = -( mY2-mY1);
                dx = -(mX2-mX1);
                r = Math.atan2(dy, dx);
                   angle = (int)Math.toDegrees(r);
                   updateRotation(angle);
                   break;

               default:
                   break;
            }

            Log.d(this.getClass().getName(), "Value of angle in ontouch: " + Double.toString(angle));

            break;

        default:
            break;
     }
    return true;
}

基本上我试图计算用户触摸图像视图并改变其位置时的角位移。

======================================= 编辑:- 以下是我的更新轮换功能:-

public void updateRotation(double angle)

{

    Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.arrow);
    int w = bitmap.getWidth();
    int h = bitmap.getHeight();
    String filePath = null;
    Matrix mtx = new Matrix();

    Log.d(this.getClass().getName(), "Value: " + Double.toString(angle));

    if(angle > 90)
        angle = 90;
    mtx.postRotate((float) angle);

    bitmap = Bitmap.createBitmap(bitmap, 0, 0, w, h, mtx, true);
    Drawable draw =new BitmapDrawable(bitmap);


    mImageView.setBackgroundDrawable(draw);
    }

================================================ ==========

编辑2:修改功能

public boolean onTouch(View v, MotionEvent motion) {



     float dy ;
     float dx;
     double r = 0;


     switch(v.getId())
     {
        case R.id.arrow:
            switch(motion.getActionMasked())
            {
            case MotionEvent.ACTION_DOWN:
                mX1 = motion.getX();
                mY1 = motion.getY();
                break;

                case MotionEvent.ACTION_MOVE:
                    Log.w(this.getClass().getName(),"In MOTION MOVE");
                    mX1 = motion.getX();
                    mY1 = motion.getY();
                    break;

               case MotionEvent.ACTION_UP:

                  Log.w(this.getClass().getName(),"In MOTION UP");
                  mX2 = motion.getX();
                  mY2 = motion.getY();
                dy = -( mY2-mY1);
                dx = -(mX2-mX1);
                r = Math.atan2(dy, dx);
                   mAngle = (int)Math.toDegrees(r);
                   updateRotation();
                   break;

               default:
                   break;
            }

            Log.d(this.getClass().getName(), "Value of angle in ontouch: " + Float.toString(mAngle));

            break;

        default:
            break;
     }
    return true;
}

==============================================

编辑 3:- 奇怪的是,如果我修改我的 onTouch 事件并在 ACTION_MOVE 中执行所有计算,则图像视图会响应每个触摸事件,但这不是我想要的,因为我无法在 ACTION_MOVE 事件中进行正确的角度旋转

【问题讨论】:

  • updateRotation() 是做什么的?
  • @Reno 添加功能

标签: android imageview


【解决方案1】:

鲁奇拉,

通过查看您的代码并亲自使用许多 onTouch 创新,我可以告诉您,问题肯定出在您的 onTouchEvent() 上。您在ACTION_MOVE 中记录了mX1mY1,而没有记录原始的ACTION_DOWN 事件。这意味着每次移动手指时,onTouchEvent() 方法都会认为这是原始位置。试试这个:

public boolean onTouch(View v, MotionEvent motion) {
     float dy ;
     float dx;
     double r = 0;
     float angle = 0;

     switch(v.getId())
     {
        case R.id.arrow:
            switch(motion.getActionMasked())
            {
               case MotionEvent.ACTION_DOWN:
                    mX1 = motion.getX();
                    mY1 = motion.getY();
                    break;
               case MotionEvent.ACTION_MOVE:
                    Log.w(this.getClass().getName(),"In MOTION MOVE");
               //Just to support real time rotation, you could:
                    mX2 = motion.getX();
                    mY2 = motion.getY();
                    //... some rotation code.
                    break;

               case MotionEvent.ACTION_UP:
                   Log.w(this.getClass().getName(),"In MOTION UP");
              // All of this should be fine.
                   mX2 = motion.getX();
                   mY2 = motion.getY();
                   dy = -( mY2-mY1);
                   dx = -(mX2-mX1);
                   r = Math.atan2(dy, dx);
                   angle = (int)Math.toDegrees(r);
                   updateRotation(angle);
                   break;

               default:
                   break;
            }

            Log.d(this.getClass().getName(), "Value of angle in ontouch: " + Double.toString(angle));

            break;

        default:
            break;
    }
    return true;
}

这至少应该正确跟踪您的号码,以便它们可以实际使用。

其他注意事项 最常见的错误是确保您没有将任何跟踪变量设置为 final。如果是这种情况,代码将设置一次,但不会再次设置...

在处理 Drawable 时,摆脱原来的 Drawable 及其回调是很重要的。其原因是一个众所周知的问题,该问题在 Android 中从未得到解决,因为无法确定何时需要以及何时不需要。本质上,Android 最终会耗尽内存,但不一定会告诉你,只是无法更新图像。这就是您执行此操作的方式(替换图像时)。

Drawable trash = mImageView.getBackgroundDrawable();
if (trash != null)
{   trash.setCallback(null);
    trash.recycle();
    trash = null;
}
mImageView.setBackgroundDrawable(draw);

最后,您必须让图像知道它必须重新绘制自己。情况并非总是如此,但往往是这样。 mImageView.invalidate(); 通常会解决问题,应该放在updateRotation() 方法的最后一行。

模糊逻辑

【讨论】:

  • 我粘贴了修改后的代码,但第一次触摸时图像视图仍然响应(旋转 180),随后不旋转。
  • 我已将修改后的函数作为我在 Edit2 中的问题的一部分
  • 您的 ACTION_MOVE 仍会更新 mX1 和 mY1。更改它以更新 mX2 和 mY2。只有你的 ACTION_DOWN 应该改变 mX1 和 mY1。
  • 如果没有看到整个班级,我无法确定...但有些东西在我上面的编辑中。 (给它几分钟发布)
  • 不,它没有。那只是你的偏好。如果需要,您可以手动处理所有滚动。 (虽然我不推荐它)。是的,有几种选择。您实际上可以将您的触摸处理移动到可以访问滚动的父视图并在那里中断,然后根据发生的情况推迟到 ImageView 或 Scroller。您可以为应用程序设置一个指示器,例如在旋转之前长按图像,并使用“isRotating”变量通知应用程序和 Scroller。不过,您所要求的实际上是另一个问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-16
  • 1970-01-01
相关资源
最近更新 更多