ViewFlipper+GestureDetector实现不循环滑动
16lz
2021-12-04
在布局文件中entering.xml
<ViewFlipper android:id="@+id/entering_vf" android:layout_width="fill_parent" android:layout_height="350dip" android:flipInterval="1000" android:persistentDrawingCache="animation" android:visibility="invisible" > <include android:id="@+id/entering_carpart01" layout="@layout/entering_carpart01" /> <include android:id="@+id/entering_carpart02" layout="@layout/entering_carpart02" /> </ViewFlipper>
布局文件entering_carpart01
<?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:orientation="vertical" > <LinearLayout android:layout_width="match_parent" android:layout_height="390dip" android:gravity="center_horizontal" android:orientation="vertical" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginBottom="40dip" android:layout_marginTop="20dip" android:gravity="center_horizontal" android:orientation="horizontal" > <FrameLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center" > <ImageView android:id="@+id/entering_iv_carpre" android:layout_width="293dip" android:layout_height="151dip" android:layout_marginLeft="100dip" android:background="@drawable/car_01" /> <ImageButton android:id="@+id/entering_imbtn_preCarPreWindow" style="@style/entering_imageviewRed_style" android:layout_marginLeft="240dip" android:layout_marginTop="8dip" /> <ImageButton android:id="@+id/entering_imbtn_preCarLight" style="@style/entering_imageviewRed_style" android:layout_marginLeft="160dip" android:layout_marginTop="50dip" /> <ImageButton android:id="@+id/entering_imbtn_preCarPre" style="@style/entering_imageviewRed_style" android:layout_marginLeft="243dip" android:layout_marginTop="30dip" /> <ImageButton android:id="@+id/entering_imbtn_preCarPlate" style="@style/entering_imageviewRed_style" android:layout_marginLeft="243dip" android:layout_marginTop="83dip" /> <ImageView android:id="@+id/entering_iv_carbehind" android:layout_width="253dip" android:layout_height="162dip" android:layout_marginLeft="500dip" android:background="@drawable/car_02" /> <ImageButton android:id="@+id/entering_imbtn_behindCar_windown" style="@style/entering_imageviewRed_style" android:layout_marginLeft="620dip" android:layout_marginTop="20dip" /> <ImageButton android:id="@+id/entering_imbtn_behindCar_middle" style="@style/entering_imageviewRed_style" android:layout_marginLeft="620dip" android:layout_marginTop="68dip" /> <ImageButton android:id="@+id/entering_imbtn_behindCar_bottom" style="@style/entering_imageviewRed_style" android:layout_marginLeft="620dip" android:layout_marginTop="103dip" /> </FrameLayout> </LinearLayout> <FrameLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center" > <ImageView android:background="@drawable/car_03" android:id="@+id/entering_iv_carleft" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="240dip" /> <ImageButton android:id="@+id/entering_imbtn_sideCar_top" style="@style/entering_imageviewRed_style" android:layout_marginLeft="430dip" /> <ImageButton android:id="@+id/entering_imbtn_sideCar_door" style="@style/entering_imageviewRed_style" android:layout_marginLeft="470dip" android:layout_marginTop="45dip" /> <ImageButton android:id="@+id/entering_imbtn_sideCar_carwheels" style="@style/entering_imageviewRed_style" android:layout_marginLeft="350dip" android:layout_marginTop="35dip" /> </FrameLayout> </LinearLayout></LinearLayout>
布局文件entering_carpart02
<?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:orientation="vertical" > <LinearLayout android:layout_width="match_parent" android:layout_height="390dip" android:gravity="center_horizontal" android:orientation="vertical" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginBottom="40dip" android:layout_marginTop="20dip" android:gravity="center_horizontal" android:orientation="horizontal" > <FrameLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center" > <ImageView android:id="@+id/entering_iv_carpre" android:layout_width="293dip" android:layout_height="151dip" android:layout_marginLeft="100dip" android:background="@drawable/car_01" /> <ImageButton android:id="@+id/entering_imbtn_preCarPreWindow" style="@style/entering_imageviewRed_style" android:layout_marginLeft="240dip" android:layout_marginTop="8dip" /> <ImageButton android:id="@+id/entering_imbtn_preCarLight" style="@style/entering_imageviewRed_style" android:layout_marginLeft="160dip" android:layout_marginTop="50dip" /> <ImageButton android:id="@+id/entering_imbtn_preCarPre" style="@style/entering_imageviewRed_style" android:layout_marginLeft="243dip" android:layout_marginTop="30dip" /> <ImageButton android:id="@+id/entering_imbtn_preCarPlate" style="@style/entering_imageviewRed_style" android:layout_marginLeft="243dip" android:layout_marginTop="83dip" /> <ImageView android:id="@+id/entering_iv_carbehind" android:layout_width="253dip" android:layout_height="162dip" android:layout_marginLeft="500dip" android:background="@drawable/car_02" /> <ImageButton android:id="@+id/entering_imbtn_behindCar_windown" style="@style/entering_imageviewRed_style" android:layout_marginLeft="620dip" android:layout_marginTop="20dip" /> <ImageButton android:id="@+id/entering_imbtn_behindCar_middle" style="@style/entering_imageviewRed_style" android:layout_marginLeft="620dip" android:layout_marginTop="68dip" /> <ImageButton android:id="@+id/entering_imbtn_behindCar_bottom" style="@style/entering_imageviewRed_style" android:layout_marginLeft="620dip" android:layout_marginTop="103dip" /> </FrameLayout> </LinearLayout> <FrameLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center" > <ImageView android:background="@drawable/car_03" android:id="@+id/entering_iv_carleft" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="240dip" /> <ImageButton android:id="@+id/entering_imbtn_sideCar_top" style="@style/entering_imageviewRed_style" android:layout_marginLeft="430dip" /> <ImageButton android:id="@+id/entering_imbtn_sideCar_door" style="@style/entering_imageviewRed_style" android:layout_marginLeft="470dip" android:layout_marginTop="45dip" /> <ImageButton android:id="@+id/entering_imbtn_sideCar_carwheels" style="@style/entering_imageviewRed_style" android:layout_marginLeft="350dip" android:layout_marginTop="35dip" /> </FrameLayout> </LinearLayout></LinearLayout>
java代码
在EnteringActivity.Activity java文件中
private ViewFlipper viewFlipper;// 定义一个手势识别器private GestureDetector mGestureDetector;@Overrideprotected void onCreate(Bundle savedInstanceState) {viewFlipper = (ViewFlipper) findViewById(R.id.entering_vf);mGestureDetector = new GestureDetector(new GestureDetector.SimpleOnGestureListener() {public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {if (Math.abs(velocityX) < 100)return true;if (Math.abs(e2.getRawY() - e1.getRawY()) > 100)return true;if (e2.getRawX() - e1.getRawX() > 100) {// 从右到左的移动if (viewFlipper.getDisplayedChild() == 0) {//viewFlipper对象没有子view的时候,停止滑动 viewFlipper.stopFlipping(); return false;} else {showPre();return false;}}if (e2.getRawX() - e1.getRawX() < -100) {if (viewFlipper.getDisplayedChild() == 2 - 1) {//viewFlipper对象等于1的时候,停止滑动 viewFlipper.stopFlipping(); return false;} else {showNext();return false;}}return false;};}); 关键就是要把 手势识别器 注册到 当前的Activity 的滑动方法里面@Overridepublic boolean onTouchEvent(MotionEvent event) {// 当手指在屏幕上触摸的时候 利用手势识别器 识别相应的动作mGestureDetector.onTouchEvent(event);return super.onTouchEvent(event);} public void Imv1() {imvred.setBackgroundDrawable(new BitmapDrawable(BitmapFactory.decodeResource(getResources(), R.drawable.point_red)));imvpurple.setBackgroundDrawable(new BitmapDrawable(BitmapFactory.decodeResource(getResources(), R.drawable.position_point)));}public void Imv2() {imvred.setBackgroundDrawable(new BitmapDrawable(BitmapFactory.decodeResource(getResources(), R.drawable.position_point)));imvpurple.setBackgroundDrawable(new BitmapDrawable(BitmapFactory.decodeResource(getResources(), R.drawable.point_red)));}protected void showPre() {Animation outAnimation = AnimationUtils.loadAnimation(this, R.anim.alpha_out);Animation inAnimation = AnimationUtils.loadAnimation(this, R.anim.alpha_in);viewFlipper.setOutAnimation(outAnimation);viewFlipper.setInAnimation(inAnimation);viewFlipper.showPrevious();Imv1();}private void showNext() {Animation outAnimation = AnimationUtils.loadAnimation(this, R.anim.tran_out);Animation inAnimation = AnimationUtils.loadAnimation(this, R.anim.tran_in);viewFlipper.setOutAnimation(outAnimation);viewFlipper.setInAnimation(inAnimation);viewFlipper.showNext();Imv2();}
更多相关文章
- android一些不常用的事件
- android 结合 opencv项目(NDK、OpenCV、android,官方Demo人脸识别)
- 【Android每周专题】触摸屏手势
- Android--Listview横向滑动
- 开机不锁屏
- Tab游标,实现fragment滑动,,游标改变
- Android音乐播放器汇总贴
- 【Android】FadingEdge
- listview的特殊属性