首页 > 代码库 > android的ViewFlipper

android的ViewFlipper

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
   >
	
    
    
    <ViewFlipper 
        android:id="@+id/viewFlipper_1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        >
        
        
        
         <LinearLayout 
        android:id="@+id/linearLayout_1_1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        >
        <ImageView 
            android:id="@+id/imageView_1_1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="http://www.mamicode.com/@drawable/kobe0"
            />
    </LinearLayout>
    <LinearLayout
        android:id="@+id/linearayout_2_2" 
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        
        >
          <ImageView 
            android:id="@+id/imageView_2_2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="http://www.mamicode.com/@drawable/kobe1"
            />
    </LinearLayout>
     <LinearLayout
        android:id="@+id/linearayout_3_3" 
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        
        >
          <ImageView 
            android:id="@+id/imageView_3_3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="http://www.mamicode.com/@drawable/kobe3"
            />
    </LinearLayout>
     <LinearLayout
        android:id="@+id/linearayout_4_4" 
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        
        >
          <ImageView 
            android:id="@+id/imageView_4_4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="http://www.mamicode.com/@drawable/kobe4"
            />
    </LinearLayout>
    
    </ViewFlipper>
   
   

</LinearLayout>

res/anim/in_leftright.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"  
    >
    <translate
        android:duration="3000"
        android:fromXDelta="-100%p"
        android:toXDelta="0"
        >
    

	</translate>
    
</set>

res/anim/in_rightleft.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"  
    >
    <translate
        android:duration="3000"
        android:fromXDelta="100%p"
        android:toXDelta="0"
        >
    

	</translate>
    
</set>

res/anim/out_leftright.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"  
    >
    <translate
        android:duration="3000"
        android:fromXDelta="0"
        android:toXDelta="100%p"
        >
    

	</translate>
    
</set>

res/anim/out_rightleft.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"  
    >
    <translate
        android:duration="3000"
        android:fromXDelta="0"
        android:toXDelta="-100%p"
        >
    

	</translate>
    
</set>

MainActivity

package com.example.viewflipper;

import android.R.integer;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.widget.ViewFlipper;

public class MainActivity extends Activity {
	private ViewFlipper viewFilpper;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		viewFilpper=(ViewFlipper) findViewById(R.id.viewFlipper_1);
	}
	//重写触屏事件监听方法
	float startx=0.0F;
	float endx=0.0F;
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		// TODO Auto-generated method stub
		int action=event.getAction();
		switch (action) {
		case MotionEvent.ACTION_DOWN:
			startx=event.getX();
			break;
		case MotionEvent.ACTION_UP:
			//向右滑动
			if(event.getX()-startx>10){
				viewFilpper.setInAnimation(this, R.anim.in_leftright);
				viewFilpper.setOutAnimation(this, R.anim.out_leftright);
				viewFilpper.showNext();
			}else if(startx-event.getX()>10){
				viewFilpper.setInAnimation(this, R.anim.in_rightleft);
				viewFilpper.setOutAnimation(this, R.anim.out_rightleft);
				viewFilpper.showPrevious();
			}
			
			break;
		default:
			break;
		}
		return super.onTouchEvent(event);
	}
}

技术分享

本文出自 “matengbing” 博客,请务必保留此出处http://matengbing.blog.51cto.com/11395502/1882840

android的ViewFlipper