Desenvolvendo uma tela inicial do Android

Estou trabalhando em um aplicativo que tem uma tela inicial. Essa tela inicial deve se comportar como atela inicial do android onde você pode alternar entre várias visualizações, passando o dedo sobre a tela de toque.

A solução é fácil. eu tenho3 visualizar instâncias, direito, esquerda evisão atual. Eu recebo essas instâncias doviewflipper que eu inicializei anteriormente. Como tenho um HTC G1, minha tela tem 320 px de largura e 480 px de altura.

Imagine que você captura o valor baixo de um evento de ação em movimento ao tocar na tela. Então você move o dedo e a tela deve se mover exatamente da mesma maneira, para que você tenha que recalcular a posição da vista. Até agora funciona para mim, mas estou enfrentando um problema estranho. Quando você toca na visualização correta sem mover o dedo, mas mantendo-a na tela por menos de um segundo, a visualização desaparece e mostra a visualização esquerda.

Aqui está o meu código:

public class MainActivity extends Activity implements OnTouchListener{

    private ViewFlipper vf;
    private float downXValue;
    private View view1, view2, view3;

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

        this.vf = (ViewFlipper) findViewById(R.id.flipper);

        if(this.vf != null){
             this.view1 = vf.getChildAt(0);
             this.view2 = vf.getChildAt(1);  
             this.view3 = vf.getChildAt(2);
             vf.setDisplayedChild(0);
         }      

         LinearLayout layMain = (LinearLayout) findViewById(R.id.layout_main);
         layMain.setOnTouchListener((OnTouchListener) this);
    }

    public boolean onTouch(View v, MotionEvent arg1) {

         final View currentView = vf.getCurrentView();
         final View leftView, rightView;

         if(currentView == view1){
             leftView = view3;
             rightView = view2;
         }else if(currentView == view2){
             leftView = view1;
             rightView = view3;
         }else if(currentView == view3){
             leftView = view2;
             rightView = view1;
         }else{
             leftView = null;
             rightView = null;
         }

         switch (arg1.getAction()){
            case MotionEvent.ACTION_DOWN:{
                this.downXValue = arg1.getX();
                break;
            }
            case MotionEvent.ACTION_UP:{
                float currentX = arg1.getX();            
                    if ((downXValue < currentX)){
                        if(currentView != view3){
                        float t3 = (320-(currentX-downXValue))/320;                             
                        this.vf.setInAnimation(AnimationHelper.inFromLeftAnimation(t3));
                        this.vf.setOutAnimation(AnimationHelper.outToRightAnimation(t3));
                        this.vf.showPrevious(); } 
                      }

                    if ((downXValue > currentX)){
                        if(currentView != view2){
                        float t = (320-(downXValue-currentX))/320;
                        this.vf.setInAnimation(AnimationHelper.inFromRightAnimation(t));
                        this.vf.setOutAnimation(AnimationHelper.outToLeftAnimation(t));
                        this.vf.showNext();}    
                    }                         
            }
            break;
            case MotionEvent.ACTION_MOVE:{

                leftView.setVisibility(View.VISIBLE);
                rightView.setVisibility(View.VISIBLE);

                float currentX = arg1.getX();     
                if(downXValue > currentX){  
                    if(currentView != view2){
                        currentView.layout((int) (currentX - downXValue),
                        currentView.getTop(),
                        (int) (currentX - downXValue) + 320,
                        currentView.getBottom()); 
                    }
                }

                if(downXValue < currentX){  
                    if(currentView != view3){
                        currentView.layout((int) (currentX - downXValue),
                        currentView.getTop(),
                        (int) (currentX - downXValue) + 320,
                        currentView.getBottom());


                    }
                }
                leftView.layout(currentView.getLeft()-320, leftView.getTop(),
                       currentView.getLeft(), leftView.getBottom());   

                rightView.layout(currentView.getRight(), rightView.getTop(), 
                        currentView.getRight() + 320, rightView.getBottom());
                }
            }

        return true;
    }

    public static class AnimationHelper {
          public static Animation inFromRightAnimation(float param) {
            Animation inFromRight = new TranslateAnimation(
            Animation.RELATIVE_TO_PARENT, +param,
            Animation.RELATIVE_TO_PARENT, 0.0f,
            Animation.RELATIVE_TO_PARENT, 0.0f,
            Animation.RELATIVE_TO_PARENT, 0.0f);
            inFromRight.setDuration(250);
            inFromRight.setInterpolator(new AccelerateInterpolator());
            return inFromRight;
          }

          public static Animation outToLeftAnimation(float param) {
            Animation outtoLeft = new TranslateAnimation(
            Animation.RELATIVE_TO_PARENT, 0.0f,
            Animation.RELATIVE_TO_PARENT, -param,
            Animation.RELATIVE_TO_PARENT, 0.0f,
            Animation.RELATIVE_TO_PARENT, 0.0f);
            outtoLeft.setDuration(250);
            outtoLeft.setInterpolator(new AccelerateInterpolator());
            return outtoLeft;
          }

          // for the next movement
          public static Animation inFromLeftAnimation(float param) {
            Animation inFromLeft = new TranslateAnimation(
            Animation.RELATIVE_TO_PARENT, -param,
            Animation.RELATIVE_TO_PARENT, 0.0f,
            Animation.RELATIVE_TO_PARENT, 0.0f,
            Animation.RELATIVE_TO_PARENT, 0.0f);
            inFromLeft.setDuration(250);
            inFromLeft.setInterpolator(new AccelerateInterpolator());
            return inFromLeft;
          }

          public static Animation outToRightAnimation(float param) {
            Animation outtoRight = new TranslateAnimation(
            Animation.RELATIVE_TO_PARENT, 0.0f,
            Animation.RELATIVE_TO_PARENT, +param,
            Animation.RELATIVE_TO_PARENT, 0.0f,
            Animation.RELATIVE_TO_PARENT, 0.0f);
            outtoRight.setDuration(250);
            outtoRight.setInterpolator(new AccelerateInterpolator());
            return outtoRight;
          }
        }

}

Eu acho que essa tela inicial é um elemento interessante da interface do usuário.

Alguma ideia?

Você pode fazer o download do projeto eclipse de trabalho aqui: http://www.megaupload.com/?d=3M3IYGGM

questionAnswers(3)

yourAnswerToTheQuestion