如何在android中通过viewflipper翻转视图?

时间:2011-11-23 12:43:23

标签: android delay viewflipper

在Android应用程序中,我使用两个视图鳍状肢翻转视图。我想在翻转视图之间提供延迟。我在视图翻转器上调用on click处理程序。这是我的代码。

@Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.oldmactwo);

            flipper = (ViewFlipper) findViewById(R.id.jetViewflipper);
            flippercow=(ViewFlipper) findViewById(R.id.cowViewflipper);

            flippercow.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
        Toast.makeText(getApplicationContext(), "on click method call",Toast.LENGTH_SHORT).show();              

        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
                    flipper.setInAnimation(inFromLeftAnimation());
                    flipper.setOutAnimation(outToLeftAnimation());
                    flipper.showPrevious();

                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    /*Thread splashThread=new Thread()
                    {
                        public void run() {
                            try {
                                sleep(5000);
                            } catch (InterruptedException e) {
                                // TODO: handle exception
                            }
                            finally{
                                //splashThread.stop();
                            }

                        };
                    };
                    splashThread.start();*/

    Toast.makeText(getApplicationContext(), "delay ends",Toast.LENGTH_SHORT).show();                
                    //getcowFlipper();              
                    flippercow.setInAnimation(inFromBottomAnimation());
                    flippercow.setOutAnimation(outToTopAnimation());
                    flippercow.showNext();
                    //flipper.showPrevious();
    Toast.makeText(getApplicationContext(), "method ends",Toast.LENGTH_SHORT).show();               

                }
            });
     }

在上面的代码中,首先执行延迟,然后再查看翻转。

1 个答案:

答案 0 :(得分:1)

你的inFromLeftAnimation()+ inFromRightAnimation()+ outFromLeftAnimation()+ outFromRightAnimation()方法包含这样的部分:

inFromLeft.setDuration(400);

以上部分将延迟400ms。 当然你也得到了inFromRight,outFromLeft等等。

示例:

    private Animation inFromLeftAnimation() 
    {
      Animation inFromLeft = new TranslateAnimation(
      Animation.RELATIVE_TO_PARENT,  -1.0f, Animation.RELATIVE_TO_PARENT,  0.0f,
      Animation.RELATIVE_TO_PARENT,  0.0f, Animation.RELATIVE_TO_PARENT,   0.0f);

      inFromLeft.setDuration(400);
      inFromLeft.setInterpolator(new AccelerateInterpolator());
      return inFromLeft;
    }