Android 主屏效果

发布于 2024-09-12 18:24:06 字数 2869 浏览 1 评论 0原文

我想做一些类似andriod的主屏效果。当我点击屏幕并移动时。当前视图将移动,下一个视图也可以显示。

现在代码只能显示当前视图。

感谢您的帮助,

来源如下:

public class test extends Activity implements OnTouchListener{
float downXValue;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Set main.XML as the layout for this Activity
    setContentView(R.layout.main);

    // Add these two lines
    LinearLayout layMain = (LinearLayout) findViewById(R.id.layout_main);
    layMain.setOnTouchListener((OnTouchListener) this); 

    // Add a few countries to the spinner
    Spinner spinnerCountries = (Spinner) findViewById(R.id.spinner_country);
    ArrayAdapter countryArrayAdapter = new ArrayAdapter(this,android.R.layout.simple_spinner_dropdown_item,new String[] { "Canada", "USA" });
    spinnerCountries.setAdapter(countryArrayAdapter);

}

public boolean onTouch(View arg0, MotionEvent arg1) {

    // Get the action that was done on this touch event
    switch (arg1.getAction())
    {
        case MotionEvent.ACTION_DOWN:
        {
            // store the X value when the user's finger was pressed down
            downXValue = arg1.getX();
            break;
        }

        case MotionEvent.ACTION_UP:
        {
            // Get the X value when the user released his/her finger
            float currentX = arg1.getX();            

            // going backwards: pushing stuff to the right
            if (downXValue < currentX)
            {
                // Get a reference to the ViewFlipper
                 ViewFlipper vf = (ViewFlipper) findViewById(R.id.details);
                 // Set the animation
                  vf.setAnimation(AnimationUtils.loadAnimation(this, R.anim.push_left_out));
                  // Flip!
                  vf.showPrevious();

            }

            // going forwards: pushing stuff to the left
            if (downXValue > currentX)
            {
                // Get a reference to the ViewFlipper
                ViewFlipper vf = (ViewFlipper) findViewById(R.id.details);
                 // Set the animation
                 vf.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.push_left_in));
                  // Flip!
                 vf.showNext();
            }
            break;
        }
        case MotionEvent.ACTION_MOVE:
          ViewFlipper vf = (ViewFlipper) findViewById(R.id.details);
            final View currentView = vf.getCurrentView();
             currentView.layout((int)(arg1.getX() - downXValue), 
                   currentView.getTop(), currentView.getRight(), 
                    currentView.getBottom());
             /*may be do something in thest*/

        break;

    }

    // if you return false, these actions will not be recorded
    return true;
}

}

I want to do something like andriod home screen effect. when i click the screen and move. the current view will move and next view also can show.

Now the code only can show the current view.

Thank you for your help

The source as follow:

public class test extends Activity implements OnTouchListener{
float downXValue;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Set main.XML as the layout for this Activity
    setContentView(R.layout.main);

    // Add these two lines
    LinearLayout layMain = (LinearLayout) findViewById(R.id.layout_main);
    layMain.setOnTouchListener((OnTouchListener) this); 

    // Add a few countries to the spinner
    Spinner spinnerCountries = (Spinner) findViewById(R.id.spinner_country);
    ArrayAdapter countryArrayAdapter = new ArrayAdapter(this,android.R.layout.simple_spinner_dropdown_item,new String[] { "Canada", "USA" });
    spinnerCountries.setAdapter(countryArrayAdapter);

}

public boolean onTouch(View arg0, MotionEvent arg1) {

    // Get the action that was done on this touch event
    switch (arg1.getAction())
    {
        case MotionEvent.ACTION_DOWN:
        {
            // store the X value when the user's finger was pressed down
            downXValue = arg1.getX();
            break;
        }

        case MotionEvent.ACTION_UP:
        {
            // Get the X value when the user released his/her finger
            float currentX = arg1.getX();            

            // going backwards: pushing stuff to the right
            if (downXValue < currentX)
            {
                // Get a reference to the ViewFlipper
                 ViewFlipper vf = (ViewFlipper) findViewById(R.id.details);
                 // Set the animation
                  vf.setAnimation(AnimationUtils.loadAnimation(this, R.anim.push_left_out));
                  // Flip!
                  vf.showPrevious();

            }

            // going forwards: pushing stuff to the left
            if (downXValue > currentX)
            {
                // Get a reference to the ViewFlipper
                ViewFlipper vf = (ViewFlipper) findViewById(R.id.details);
                 // Set the animation
                 vf.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.push_left_in));
                  // Flip!
                 vf.showNext();
            }
            break;
        }
        case MotionEvent.ACTION_MOVE:
          ViewFlipper vf = (ViewFlipper) findViewById(R.id.details);
            final View currentView = vf.getCurrentView();
             currentView.layout((int)(arg1.getX() - downXValue), 
                   currentView.getTop(), currentView.getRight(), 
                    currentView.getBottom());
             /*may be do something in thest*/

        break;

    }

    // if you return false, these actions will not be recorded
    return true;
}

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

挽容 2024-09-19 18:24:06

您可以使用 Viewpagertofeeq ahmad 说:

Viewpager 是切换视图的最佳方式。它提供了一种方法
从左到右和从右到左滑动视图。 Viewpager提供
滑动任何视图的有力方式。

您必须扩展 PagerAdapter ,然后使用 Viewpager。您可以在 Android、Java 和其他移动技术指南。

You can use Viewpager.tofeeq ahmad says:

Viewpager is the best way to switching among view. It provide a way
to swipe views from left to right and right to left. Viewpager provide
strong way to swipe any views.

You have to extend PagerAdapter and then use Viewpager.You can see snippet and full source code in Guidance on Android, Java and Other mobile Technology.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文