Imageview不移动触摸事件android



当ontouch事件被调用时,我的imageview不移动…虽然我可以看到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中imageview响应每个触摸事件的所有计算,但这不是我想要的,因为我不能在ACTION_MOVE事件

中做适当的角度旋转

Ruchira,

通过查看您的代码并亲自使用许多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。如果是这种情况,代码将设置它一次,而不是第二次…

处理Drawables时,重要的是要摆脱原来的Drawable和它的回调。造成这种情况的原因是一个众所周知的问题,在Android中从未得到解决,因为无法决定何时需要,何时不需要。从本质上讲,Android最终会耗尽内存,但不一定会告诉你,只是不会更新映像。这就是你要做的(当替换图像时)。

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

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

FuzzicalLogic

最新更新