android的ViewFlipper
activity_main.xml
<LinearLayoutxmlns: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"><ViewFlipperandroid:id="@+id/viewFlipper_1"android:layout_width="match_parent"android:layout_height="match_parent"><LinearLayoutandroid:id="@+id/linearLayout_1_1"android:layout_width="fill_parent"android:layout_height="fill_parent"><ImageViewandroid:id="@+id/p_w_picpathView_1_1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:src="@drawable/kobe0"/></LinearLayout><LinearLayoutandroid:id="@+id/linearayout_2_2"android:layout_width="fill_parent"android:layout_height="fill_parent"><ImageViewandroid:id="@+id/p_w_picpathView_2_2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:src="@drawable/kobe1"/></LinearLayout><LinearLayoutandroid:id="@+id/linearayout_3_3"android:layout_width="fill_parent"android:layout_height="fill_parent"><ImageViewandroid:id="@+id/p_w_picpathView_3_3"android:layout_width="wrap_content"android:layout_height="wrap_content"android:src="@drawable/kobe3"/></LinearLayout><LinearLayoutandroid:id="@+id/linearayout_4_4"android:layout_width="fill_parent"android:layout_height="fill_parent"><ImageViewandroid:id="@+id/p_w_picpathView_4_4"android:layout_width="wrap_content"android:layout_height="wrap_content"android:src="@drawable/kobe4"/></LinearLayout></ViewFlipper></LinearLayout>
res/anim/in_leftright.xml
<?xmlversion="1.0"encoding="utf-8"?><setxmlns:android="http://schemas.android.com/apk/res/android"><translateandroid:duration="3000"android:fromXDelta="-100%p"android:toXDelta="0"></translate></set>
res/anim/in_rightleft.xml
<?xmlversion="1.0"encoding="utf-8"?><setxmlns:android="http://schemas.android.com/apk/res/android"><translateandroid:duration="3000"android:fromXDelta="100%p"android:toXDelta="0"></translate></set>
res/anim/out_leftright.xml
<?xmlversion="1.0"encoding="utf-8"?><setxmlns:android="http://schemas.android.com/apk/res/android"><translateandroid:duration="3000"android:fromXDelta="0"android:toXDelta="100%p"></translate></set>
res/anim/out_rightleft.xml
<?xmlversion="1.0"encoding="utf-8"?><setxmlns:android="http://schemas.android.com/apk/res/android"><translateandroid:duration="3000"android:fromXDelta="0"android:toXDelta="-100%p"></translate></set>
MainActivity
packagecom.example.viewflipper;importandroid.R.integer;importandroid.app.Activity;importandroid.os.Bundle;importandroid.view.Menu;importandroid.view.MenuItem;importandroid.view.MotionEvent;importandroid.widget.ViewFlipper;publicclassMainActivityextendsActivity{privateViewFlipperviewFilpper;@OverrideprotectedvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);viewFilpper=(ViewFlipper)findViewById(R.id.viewFlipper_1);}//重写触屏事件监听方法floatstartx=0.0F;floatendx=0.0F;@OverridepublicbooleanonTouchEvent(MotionEventevent){//TODOAuto-generatedmethodstubintaction=event.getAction();switch(action){caseMotionEvent.ACTION_DOWN:startx=event.getX();break;caseMotionEvent.ACTION_UP://向右滑动if(event.getX()-startx>10){viewFilpper.setInAnimation(this,R.anim.in_leftright);viewFilpper.setOutAnimation(this,R.anim.out_leftright);viewFilpper.showNext();}elseif(startx-event.getX()>10){viewFilpper.setInAnimation(this,R.anim.in_rightleft);viewFilpper.setOutAnimation(this,R.anim.out_rightleft);viewFilpper.showPrevious();}break;default:break;}returnsuper.onTouchEvent(event);}}
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。