当调用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事件中进行正确的角度旋转
答案 0 :(得分:1)
Ruchira,
通过查看您的代码并自行处理许多onTouch创新,我可以告诉您问题肯定在您的onTouchEvent()
中。您在mX1
中记录mY1
和ACTION_MOVE
而未记录原始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