gpt4 book ai didi

android - imageview 不会在触摸事件 android 上移动

转载 作者:行者123 更新时间:2023-11-30 04:30:47 24 4
gpt4 key购买 nike

当调用 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;
}

基本上,我是在尝试计算用户触摸 ImageView 并更改其位置时的角位移。

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

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 事件中进行正确的角度旋转

最佳答案

鲁奇拉,

通过查看您的代码并亲自使用许多 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;
}

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

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

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

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

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

模糊逻辑

关于android - imageview 不会在触摸事件 android 上移动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7748936/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com