[英]imageview does not move on touch event android
當調用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;
}
基本上,我試圖計算用戶觸摸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()
。 您記錄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。 如果是這種情況,代碼將對其進行一次設置,但不會再次進行設置...
處理Drawable時,重要的是要擺脫原始Drawable及其回調。 原因是一個眾所周知的問題,在Android中從未解決過,因為無法決定何時確切需要和何時不需要。 本質上,Android最終會耗盡內存,但不一定會告訴您,只是無法更新圖像。 這是您的操作方式(更換圖像時)。
Drawable trash = mImageView.getBackgroundDrawable();
if (trash != null)
{ trash.setCallback(null);
trash.recycle();
trash = null;
}
mImageView.setBackgroundDrawable(draw);
最后,您必須讓圖像知道它必須重繪。 情況並非總是如此,但並非總是如此。 mImageView.invalidate();
通常會解決此問題,因此應將其放在updateRotation()
方法的最后一行。
模糊邏輯
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.