zoom sur un canvas android
Bonsoir
j ai un probleme pour faire un zoom sur un canvas avec une erreur sur le onTouchEvent.
des que je clique sur l image l application crash et je ne sais comment gerer mon erreur .
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
| private void drawBitmap(Canvas canvas) {
canvas.save();
canvas.translate(mPositionX,mPositionY);
canvas.scale(mScaleFactor,mScaleFactor);
canvas.drawBitmap(background,null,rect,null);
canvas.drawBitmap(tank,(dWidht/2-tankWidth/2),dHeight-tankHeight,null);
canvas.restore();
}
@Override
public boolean onTouchEvent(MotionEvent event){
mScaleDetector.onTouchEvent(event);
switch (event.getAction()){
case MotionEvent.ACTION_DOWN:
refX =event.getX();
refY=event.getY();
paint((int)((refX-mPositionX)/mScaleFactor),(int)((refY-mPositionY)/mScaleFactor));
break;
case MotionEvent.ACTION_MOVE:
float nX=event.getX();
float nY=event.getY();
mPositionX += nX-refX;
mPositionY +=nY-refY;
refX=nX;
refY=nY;
invalidate();
}
return true;
}
private void paint(int x, int y) {
if (x<0||x>=bitmap.getWidth()||y<0||y>=bitmap.getHeight())
return;
}
} |
et ci joint l erreur
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| 2020-11-22 17:58:06.788 21138-21138/? E/InputEventReceiver: Exception dispatching input event.
2020-11-22 17:58:06.788 21138-21138/? E/MessageQueue-JNI: Exception in MessageQueue callback:
handleReceiveCallback
2020-11-22 17:58:06.789 21138-21138/? E/MessageQueue-JNI: java.lang.NullPointerException:
Attempt to invoke virtual method 'boolean
android.view.ScaleGestureDetector.onTouchEvent(android.view.MotionEvent)' on a null object
reference
at com.example.myapplication.GameView.onTouchEvent(GameView.java:92)
at android.view.View.dispatchTouchEvent(View.java:13551)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3088)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2724)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3088)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2724)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3088)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2724)
at com.android.internal.policy.DecorView.superDispatchTouchEvent(DecorView.java:497)
at com.android.internal.policy.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1853)
at android.app.Activity.dispatchTouchEvent(Activity.java:4061)
at com.android.internal.policy.DecorView.dispatchTouchEvent(DecorView.java:455)
at android.view.View.dispatchPointerEvent(View.java:13812)
at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:5649)
at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:5449)
android |
merci de votre aide