我想要移动一张图片。我这样做是为了在onTouch事件中移动图像
float downx = 0;
float downy = 0;
float upx = 0;
float upy = 0;
public boolean onTouch(View v, MotionEvent event) {
int action = event.getAction();
switch (action) {
case MotionEvent.ACTION_DOWN:
downx = event.getX();
downy = event.getY();
break;
case MotionEvent.ACTION_MOVE:
upx = event.getX();
upy = event.getY();
CompositeImageViewText.invalidate();
canvas.drawBitmap(bmw, upx,upy, paint);
CompositeImageViewText.setImageBitmap(bmw);
downx = upx;
downy = upy;
break;
case MotionEvent.ACTION_UP:
upx = event.getX();
upy = event.getY();
break;
case MotionEvent.ACTION_CANCEL:
break;
default:
break;
}
return true;
}
但是我经历了这样做的力量。可能是什么问题?我的代码是否正确?
logcat粘贴在
下面01-05 02:08:30.995: E/AndroidRuntime(1240): at com.apress.proandroidmedia.ch3.choosepicturecomposite.ChoosePictureComposite.onTouch(ChoosePictureComposite.java:335)
01-05 02:08:30.995: E/AndroidRuntime(1240): at android.view.View.dispatchTouchEvent(View.java:3881)
01-05 02:08:30.995: E/AndroidRuntime(1240): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:942)
01-05 02:08:30.995: E/AndroidRuntime(1240): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:942)
01-05 02:08:30.995: E/AndroidRuntime(1240): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:942)
01-05 02:08:30.995: E/AndroidRuntime(1240): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:942)
01-05 02:08:30.995: E/AndroidRuntime(1240): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1691)
01-05 02:08:30.995: E/AndroidRuntime(1240): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1125)
01-05 02:08:30.995: E/AndroidRuntime(1240): at android.app.Activity.dispatchTouchEvent(Activity.java:2096)
01-05 02:08:30.995: E/AndroidRuntime(1240): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1675)
01-05 02:08:30.995: E/AndroidRuntime(1240): at android.view.ViewRoot.deliverPointerEvent(ViewRoot.java:2194)
01-05 02:08:30.995: E/AndroidRuntime(1240): at android.view.ViewRoot.handleMessage(ViewRoot.java:1878)
01-05 02:08:30.995: E/AndroidRuntime(1240): at android.os.Handler.dispatchMessage(Handler.java:99)
01-05 02:08:30.995: E/AndroidRuntime(1240): at android.os.Looper.loop(Looper.java:130)
01-05 02:08:30.995: E/AndroidRuntime(1240): at android.app.ActivityThread.main(ActivityThread.java:3683)
01-05 02:08:30.995: E/AndroidRuntime(1240): at java.lang.reflect.Method.invokeNative(Native Method)
01-05 02:08:30.995: E/AndroidRuntime(1240): at java.lang.reflect.Method.invoke(Method.java:507)
01-05 02:08:30.995: E/AndroidRuntime(1240): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
01-05 02:08:30.995: E/AndroidRuntime(1240): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
01-05 02:08:30.995: E/AndroidRuntime(1240): at dalvik.system.NativeStart.main(Native Method)