0

嗨,我在我的活动中创建了一个翻转视图我有 2 个需要翻转的图像,但它不是代表翻转我只能在屏幕上查看我的第一个图像......我在下面给出我的类文件和 xml。 . 我需要一些解决方案

       import android.app.Activity;
        import android.os.Bundle;
       import android.view.MotionEvent;
        import android.widget.ViewFlipper;

          public class Viewflip extends Activity 
             {
        private ViewFlipper viewFlipper;
        private float lastX;

        @Override
        protected void onCreate(Bundle savedInstanceState) 
        {
                     super.onCreate(savedInstanceState);
                     setContentView(R.layout.gallery);
                     viewFlipper = (ViewFlipper) findViewById(R.id.view_flipper);
        }



        // Method to handle touch event like left to right swap and right to left swap
        public boolean onTouchEvent(MotionEvent touchevent) 
        {
                     switch (touchevent.getAction())
                     {
                            // when user first touches the screen to swap
                             case MotionEvent.ACTION_DOWN: 
                             {
                                 lastX = touchevent.getX();
                                 break;
                            }
                             case MotionEvent.ACTION_UP: 
                             {
                                 float currentX = touchevent.getX();

                                 // if left to right swipe on screen
                                 if (lastX < currentX) 
                                 {
                                      // If no more View/Child to flip
                                     if (viewFlipper.getDisplayedChild() == 0)
                                         break;

                                     // set the required Animation type to ViewFlipper
                                     // The Next screen will come in form Left and 
                    current Screen will go OUT from Right 
                                     viewFlipper.setInAnimation(this, 
                 R.anim.in_from_left);
                                     viewFlipper.setOutAnimation(this,
                    R.anim.out_to_right);
                                     // Show the next Screen
                                     viewFlipper.showNext();
                                 }

                                 // if right to left swipe on screen
                                 if (lastX > currentX)
                                 {
                                     if (viewFlipper.getDisplayedChild() == 1)
                                         break;
                                     // set the required Animation type to ViewFlipper
                                     // The Next screen will come in form Right and 
              current Screen will go OUT from Left 
                                     viewFlipper.setInAnimation(this, 
                 R.anim.in_from_right);
                                     viewFlipper.setOutAnimation(this, 
                 R.anim.out_to_left);
                                     // Show The Previous Screen
                                     viewFlipper.showPrevious();
                                 }
                                 break;
                             }
                     }
                     return false;
                     }

                    }

xml文件

         <?xml version="1.0" encoding="utf-8"?>
          <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:background="#000000"
          android:orientation="vertical" >

         <ViewFlipper
            android:id="@+id/view_flipper"
           android:layout_width="fill_parent"
          android:layout_height="fill_parent"
            android:layout_margin="6dip" >

             <LinearLayout
              android:layout_width="fill_parent"
               android:layout_height="fill_parent"
                 android:orientation="vertical" >

                 <ImageView
                  android:id="@+id/imageView1"
                    android:layout_width="450dp"
                  android:layout_height="450dp"
                   android:layout_marginTop="15dp"
                 android:src="@drawable/scene1" />
              </LinearLayout>

              <!-- Layout 2 for 2nd Screen -->

              <LinearLayout
               android:layout_width="fill_parent"
                  android:layout_height="fill_parent"
                android:gravity="center"
                  android:orientation="vertical" >

                   <ImageView
                  android:id="@+id/imageView2"
                   android:layout_width="450dp"
                  android:layout_height="450dp"
                   android:layout_marginTop="15dp"
                android:src="@drawable/scene2" />
               </LinearLayout>
               </ViewFlipper>

                 </LinearLayout>

在下面给出我的动画 xml 文件

             **in_from_left.xml**

               <set xmlns:android="http://schemas.android.com/apk/res/android"
                android:shareInterpolator="false">
               <translate
                android:fromXDelta="-100%" android:toXDelta="0%"
               android:fromYDelta="0%" android:toYDelta="0%"
               android:duration="1400" />
                 </set>

               **in_from_right.xml**

              <set xmlns:android="http://schemas.android.com/apk/res/android"
              android:shareInterpolator="false">
             <translate
               android:fromXDelta="100%" android:toXDelta="0%"
              android:fromYDelta="0%" android:toYDelta="0%"
                android:duration="1400" />
                </set>


                **out_to_left.xml**

             <set xmlns:android="http://schemas.android.com/apk/res/android"
             android:shareInterpolator="false">
               <translate android:fromXDelta="0%" android:toXDelta="-100%"
              android:fromYDelta="0%" android:toYDelta="0%"
               android:duration="1400"/>
              </set>


                **out_to_right.xml**

              <set xmlns:android="http://schemas.android.com/apk/res/android"
              android:shareInterpolator="false">
             <translate android:fromXDelta="0%" android:toXDelta="100%"
                 android:fromYDelta="0%" android:toYDelta="0%"
               android:duration="1400"/>
                 </set>
4

1 回答 1

0

我试过你的代码,看起来没问题。

它运行良好。

问题出在动画线条上。那是行不通的。

只需评论动画线条,它将起作用。

如果您想要动画,还可以共享动画文件的代码。

于 2013-08-22T07:11:04.923 回答