onFling() 2D 动画问题
当我抛出图像视图时,我试图运行一个非常简单的 2D 动画。我有两项活动涉及此。
GameCanvas
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY)
{
if(GameWindow.getContext() == null)
return false;
if((e1.getY() >= GameWindow.getHeight()) && (e1.getY() <= GameWindow.getBottom()))
{
try
{
if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)
return false;
// right to left slap
if((e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE) && (Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY))
{
slappingLeft = true;
//Animate Slap
GameWindow.update();
if(!running)
running = true;
}
else if ((e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE) && (Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY))
{
slappingRight = true;
//Animate Slap
GameWindow.update();
if(!running )
running = true;
}
}
catch (Exception e)
{
//nothing
}
}
slappingLeft = slappingRight = running = false;
return true;
}
和 GameWindow
private RefreshHandler mRedrawHandler = new RefreshHandler();
class RefreshHandler extends Handler
{
@Override
public void handleMessage(Message msg) {
GameWindow.this.update();
GameWindow.this.invalidate();
}
public void sleep(long delayMillis) {
this.removeMessages(0);
sendMessageDelayed(obtainMessage(0), delayMillis);
}
};
public void update()
{
//animate slap
if(GameCanvas.slappingLeft)
{
for(int i = 0; i < 500; i+=100)
{
GameCanvas.SlapLeft();
mRedrawHandler.sleep(100);
}
GameCanvas.SetImage();
//this.invalidate();
}
else if(GameCanvas.slappingRight)
{
for(int i = 0; i < 500; i+=100)
{
GameCanvas.SlapImage();
mRedrawHandler.sleep(100);
}
GameCanvas.SetImage();
//this.invalidate();
}
}
如果有人帮助我解决这个问题,我将不胜感激。我尝试了许多不同的方法来解决这个问题。
我只想显示 SlapImage 半秒钟,然后恢复到正常图像,直到再次调用 onFling。
如果您想查看 SetImage()、SlapLeft() 和 SetImage(),请告诉我!
预先非常感谢!
编辑
GameCanvas 不是 Canvas 对象。它的活动设置ContentView(R.layout.game_canvas)
GameWindow是自定义ImageView
I'm trying to run a very simple 2D animation when I fling an image view. I have 2 activities involved in this.
The GameCanvas
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY)
{
if(GameWindow.getContext() == null)
return false;
if((e1.getY() >= GameWindow.getHeight()) && (e1.getY() <= GameWindow.getBottom()))
{
try
{
if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)
return false;
// right to left slap
if((e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE) && (Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY))
{
slappingLeft = true;
//Animate Slap
GameWindow.update();
if(!running)
running = true;
}
else if ((e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE) && (Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY))
{
slappingRight = true;
//Animate Slap
GameWindow.update();
if(!running )
running = true;
}
}
catch (Exception e)
{
//nothing
}
}
slappingLeft = slappingRight = running = false;
return true;
}
And the GameWindow
private RefreshHandler mRedrawHandler = new RefreshHandler();
class RefreshHandler extends Handler
{
@Override
public void handleMessage(Message msg) {
GameWindow.this.update();
GameWindow.this.invalidate();
}
public void sleep(long delayMillis) {
this.removeMessages(0);
sendMessageDelayed(obtainMessage(0), delayMillis);
}
};
public void update()
{
//animate slap
if(GameCanvas.slappingLeft)
{
for(int i = 0; i < 500; i+=100)
{
GameCanvas.SlapLeft();
mRedrawHandler.sleep(100);
}
GameCanvas.SetImage();
//this.invalidate();
}
else if(GameCanvas.slappingRight)
{
for(int i = 0; i < 500; i+=100)
{
GameCanvas.SlapImage();
mRedrawHandler.sleep(100);
}
GameCanvas.SetImage();
//this.invalidate();
}
}
I would greatly appreciate if anyone helps me figure this problem out. I have tried many different approaches to this problem.
I just want to show the SlapImage for half a second, then revert back to the normal image till onFling is called again.
If you would like to see SetImage(), SlapLeft(), and SetImage(), let me know!
Thanks very much in advance!
EDIT
GameCanvas is not a Canvas obj. Its an activity that setsContentView(R.layout.game_canvas)
GameWindow is a custom ImageView
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我在你的代码中发现了问题。也许这对结果有影响,但我不确定。问题是您应该在 if 语句中使用括号,因为运算符 &&具有比比较运算符更高的优先级。所以你的代码应该如下所示:
我不知道这是否会影响你的代码的逻辑,但在我看来是的。
I've found the problem in your code. Maybe it is influence on the result but I'm not sure. The problem is that you should use brackets in your if statements because operator && has higher priority then comparison operators. So your code should look in the following way:
I don't know if this influence on the logic of your code but it seems to me that yes.
经过一番考虑,我决定实施这个解决方案。我认为效果相当好。
我仍然不确定为什么我之前的方法不起作用。如果有人仍然想详细说明,我将不胜感激。
游戏画布。我创建了这个线程。
基本上,每半秒我想将图像重置回正常
然后在 OnFling 中我写
基本上,如果我向左拍打,我会调用 Slap left 并等待线程重置,如果我向右侧拍打,我会调用 Slap正确并等待线程重置。
这个答案很适合我的情况。我希望这有帮助:D
谢谢大家的帮助。
After some consideration, i decided to implement this solution. I think it works fairly well.
I'm still not sure why my previous way didn't work. If anyone would still like to elaborate I would appreciate.
GameCanvas. I created this thread.
Baiscally, every half-second I want to reset the image back to normal
Then in the OnFling I wrote
Basically, if I was slapping left I would call Slap left and wait for the thread to reset and if i was slapping right I would call Slap Right and wait for the thread to reset.
This answer works well for my situation.. I hope this helps :D
Thank you all for your help.