Entwicklung eines Android Homescreen

Ich arbeite an einer App mit einem Homescreen. Dieser Homescreen sollte sich wie das @ verhaltandroid Homescreen Hier können Sie zwischen verschiedenen Ansichten wechseln, indem Sie mit dem Finger über den Touchscreen fahren.

Die Lösung ist einfach. Ich habe 3 Instanzen anzeigen, richti, link undaktuelle Ansich. Ich erhalte diese Instanzen von der viewflipper dass ich früher initialisiert habe. Seit ich ein HTC G1 habe, ist mein Bildschirm 320 px breit und 480 px hoch.

Stellen Sie sich vor, Sie erfassen den Abwärtswert eines Aktionsabwärtsbewegungsereignisses, wenn Sie den Bildschirm berühren. Dann bewegen Sie Ihren Finger und der Bildschirm sollte sich genauso bewegen, so dass Sie die Position der Ansicht neu berechnen müssen. Bisher funktioniert es bei mir, aber ich stehe vor einem seltsamen Problem. Wenn Sie die rechte Ansicht berühren, ohne den Finger zu bewegen, sie jedoch weniger als eine Sekunde lang auf dem Bildschirm zu lassen, wird die Ansicht ausgeblendet und die linke Ansicht angezeigt.

Hier ist mein Code:

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

}

Ich denke so ein Homescreen ist ein interessantes UI-Element.

Irgendwelche Ideen

Sie können das funktionierende Eclipse-Projekt hier herunterladen: http: //www.megaupload.com/? d = 3M3IYGGM

Antworten auf die Frage(6)

Ihre Antwort auf die Frage