Android使用ViewPager實現左右切換02(轉)

臨時轉載


public class Welcome extends Activity implements OnGestureListener {
    private GestureDetector detector;
    private ViewFlipper flipper;
    private Button button;
    ImageView []iamges=new ImageView[4];
    int i = 0;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layoutwelcome);
        iamges[0]=(ImageView) findViewById(R.id.imageview1);
        iamges[1]=(ImageView) findViewById(R.id.imageview2);
        iamges[2]=(ImageView) findViewById(R.id.imageview3);
        iamges[3]=(ImageView) findViewById(R.id.imageview4);

        detector = new GestureDetector(this);
        flipper = (ViewFlipper) this.findViewById(R.id.ViewFlipper1);
        flipper.addView(addImageView(R.drawable.png1o));
        flipper.addView(addImageView(R.drawable.png2o));
        flipper.addView(addImageView(R.drawable.png3o));

        flipper.addView(addView());

    }

    private View addImageView(int id) {
        ImageView iv = new ImageView(this);
        iv.setImageResource(id);
        return iv;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        // TODO Auto-generated method stub
        return this.detector.onTouchEvent(event); 
    }

    private View addView() {
        LayoutInflater inflater = LayoutInflater.from(this);
        View view = inflater.inflate(R.layout.layoutwelcome2, null);
        button = (Button) view.findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // Intent intent = new
                // Intent(getApplicationContext(),ActContent.class);
                // startActivity(intent);
                // finish();
                Toast.makeText(Welcome.this, "Clicked", 2000).show();
            }
        });
        return view;
    }

    @Override
    public boolean onDown(MotionEvent e) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public void onShowPress(MotionEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public boolean onSingleTapUp(MotionEvent e) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
            float distanceY) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public void onLongPress(MotionEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
            float velocityY) {
        System.out.println("in------------>>>>>>>");
        if (e1.getX() - e2.getX() > 120) {
            if (i < 3) {
                i++;
                setImage(i);
                this.flipper.setInAnimation(AnimationUtils.loadAnimation(this,
                        R.anim.animation_right_in));
                this.flipper.setOutAnimation(AnimationUtils.loadAnimation(this,
                        R.anim.animation_left_out));
                this.flipper.showNext();
            }
            return true;
        } 
        else if (e1.getX() - e2.getX() < -120) {
            if (i > 0) {
                i--;
                setImage(i);
                this.flipper.setInAnimation(AnimationUtils.loadAnimation(this,
                        R.anim.animation_left_in));
                this.flipper.setOutAnimation(AnimationUtils.loadAnimation(this,
                        R.anim.animation_right_out));
                this.flipper.showPrevious();
            }
            return true;
        }
        return false;

    }

    void setImage(int i)
    {
        for(int j=0;j<4;j++)
        {
            if(j!=i)
            iamges[j].setImageResource(R.drawable.xiao);
            else
                iamges[j].setImageResource(R.drawable.da);
        }
    }
}

主要佈局

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <ViewFlipper android:id="@+id/ViewFlipper1"
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent">
    </ViewFlipper>



    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="wrap_content"

        android:layout_gravity="bottom|center_horizontal"
        android:layout_height="wrap_content"

        >
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/da"

            android:id="@+id/imageview1"
            />
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/xiao"
            android:id="@+id/imageview2"
            />
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/xiao"
            android:id="@+id/imageview3"
            />
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/xiao"
            android:id="@+id/imageview4"
            />

        </LinearLayout>


</FrameLayout>

次要佈局

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

    >
<Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="45dp"      
        android:text="進入程序"
        android:textColor="#3E3E3E" 
        android:layout_gravity="center_horizontal"
        />
</LinearLayout> 

由於工作原因,無法下載代碼。因此貼出代碼僅供自己學習。學習結束整理分析或者刪除博文。

發佈了14 篇原創文章 · 獲贊 12 · 訪問量 6萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章