onFling()2D动画发行 [英] onFling() 2D Animation Issue

查看:152
本文介绍了onFling()2D动画发行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想运行一个非常简单的2D动画,当我扔的图像视图。我也参与了这一活动2。

I'm trying to run a very simple 2D animation when I fling an image view. I have 2 activities involved in this.

本的GameCanvas

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;            
    }       

而GameWindow

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();
    }

}

我将不胜AP preciate如果有人可以帮助我弄清楚这个问题了。我已经尝试了许多不同的方法这个问题。

I would greatly appreciate if anyone helps me figure this problem out. I have tried many different approaches to this problem.

我只是想显示SlapImage半秒,然后恢复到正常的图像,直到onFling再次调用。

I just want to show the SlapImage for half a second, then revert back to the normal image till onFling is called again.

如果你想看到SetImage(),SlapLeft(),和SetImage(),让我知道!

If you would like to see SetImage(), SlapLeft(), and SetImage(), let me know!

非常感谢事先!

修改

EDIT

的GameCanvas不是画布OBJ。它的一个活动,setsContentView(R.layout.game_canvas)

GameCanvas is not a Canvas obj. Its an activity that setsContentView(R.layout.game_canvas)

GameWindow是一个自定义的ImageView

GameWindow is a custom ImageView

推荐答案

经过一番考虑,我决定实施此解决方案。我认为它的工作原理相当不错。

After some consideration, i decided to implement this solution. I think it works fairly well.

我仍然不知道为什么我的previous办法没有奏效。如果有人仍想阐述我会AP preciate。

I'm still not sure why my previous way didn't work. If anyone would still like to elaborate I would appreciate.

在GameCanvas。我创造了这个主题。

GameCanvas. I created this thread.

resetSlap = new Thread()
{
        public void run()
        {           
            while(true)
            {
                try 
                {
                    Thread.sleep(500);
                    GameWindow.post(new Runnable()
                    {
                        public void run()
                        {                           
                            GameWindow.setImageBitmap(images[0]);
                            GameWindow.postInvalidate();
                        }
                });
                } catch (InterruptedException e)
                {                               
                    e.printStackTrace();
                }                                   
            }
        }
    };          

Baiscally,每半秒我想图像复位到正常

Baiscally, every half-second I want to reset the image back to normal

然后,在我写的OnFling

Then in the OnFling I wrote

@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  
                    SlapLeft();
                    SlapSound();
                    if(voice.nextInt(10) < 3)
                    {
                        Voice.start();
                    }

                }  
                // left to right slap
                else if ((e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE) && 
                        (Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY))               
                {       
                    slappingRight = true;

                    //Animate Slap
                    SlapImage();
                    SlapSound();
                    if(voice.nextInt(10) < 3)
                    {
                        Voice.start();
                    }
                }
            }                
            catch (Exception e)                 
            {                                   
                //nothing                                   
            }               
        }

        slappingLeft = slappingRight  = false;
        return true;            
    }

基本上,如果我被拍走了,我会叫拍击离开,等待重置线程,如果我是正确的拍打我会打电话巴掌右和等待线程重新设置。

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.

这个答案很适合我的情况。我希望这有助于:D

This answer works well for my situation.. I hope this helps :D

感谢大家的帮助。

这篇关于onFling()2D动画发行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆