Android实现左右滑动效果

2025-11-20 12:29:13

1、设置布局文件,其中使用到ViewFlipper控件,内容如下:

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout 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:background="@drawable/background"

    android:paddingBottom="@dimen/activity_optopns_vertical_margin"

    android:paddingLeft="@dimen/activity_options_horizontal_margin"

    android:paddingRight="@dimen/activity_options_horizontal_margin"

    android:paddingTop="@dimen/activity_optopns_vertical_margin"

    tools:context=".ImageFlipperActivity" >

    <RelativeLayout

        android:id="@id/rl_image_flipper_title"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:orientation="horizontal" >

        <Button

            android:id="@+id/btn_image_flipper_back"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:layout_alignParentLeft="true"

            android:layout_marginBottom="10dp"

            android:background="@drawable/custom_button"

            android:text="@string/back"

            android:textColor="@color/textColor"

            android:textSize="16sp"

            android:visibility="visible" />

        <TextView

            android:id="@id/tv_image_flipper_title"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:layout_centerInParent="true"

            android:text="@string/image_flipper"

            android:textColor="@color/textColor"

            android:textSize="30sp"

            android:textStyle="bold" />

    </RelativeLayout>

    <LinearLayout

        android:id="@id/ll_image_flipper_content"

        android:layout_width="match_parent"

        android:layout_height="match_parent"

        android:layout_below="@id/rl_image_flipper_title"

        android:layout_marginBottom="20dp"

        android:layout_marginTop="20dp"

        android:gravity="center"

        android:orientation="vertical" >

        <ViewFlipper

            android:id="@id/vf_image_flipper"

            android:layout_width="match_parent"

            android:layout_height="match_parent" />

    </LinearLayout>

</RelativeLayout>

2、从左边进入的动画文件,其内容如下:

<?xml version="1.0" encoding="utf-8"?>

<set xmlns:android="http://schemas.android.com/apk/res/android" >

    <!-- translate:画面转换位置移动动画效果 -->

    <translate

        android:duration="500"

        android:fromXDelta="100%p"

        android:toXDelta="0" />

    <!-- alpha:渐变透明度动画效果 -->

    <alpha

        android:duration="500"

        android:fromAlpha="0.1"

        android:toAlpha="1.0" />

</set>

3、从左边退出的动画文件,其内容如下:

<?xml version="1.0" encoding="utf-8"?>

<set xmlns:android="http://schemas.android.com/apk/res/android" >

    <translate

        android:duration="500"

        android:fromXDelta="0"

        android:toXDelta="-100%p" />

    <alpha

        android:duration="500"

        android:fromAlpha="1.0"

        android:toAlpha="0.1" />

</set>

4、从右边进入的动画文件,其内容如下:

<?xml version="1.0" encoding="utf-8"?>

<set xmlns:android="http://schemas.android.com/apk/res/android" >

    <translate

        android:duration="500"

        android:fromXDelta="-100%p"

        android:toXDelta="0" />

    <alpha

        android:duration="500"

        android:fromAlpha="0.1"

        android:toAlpha="1.0" />

</set>

5、从右边退出的动画文件,其内容如下:

<?xml version="1.0" encoding="utf-8"?>

<set xmlns:android="http://schemas.android.com/apk/res/android" >

    <translate

        android:duration="500"

        android:fromXDelta="0"

        android:toXDelta="100%p" />

    <alpha

        android:duration="500"

        android:fromAlpha="1.0"

        android:toAlpha="0.1" />

</set>

6、加载布局文件和动画文件的类,其源码内容为:

/**

 * 

 */

package com.i114gbox.aglieguy;

import android.content.Context;

import android.os.Bundle;

import android.view.GestureDetector;

import android.view.GestureDetector.OnGestureListener;

import android.view.MotionEvent;

import android.view.View;

import android.view.View.OnClickListener;

import android.view.animation.AnimationUtils;

import android.widget.Button;

import android.widget.ImageView;

import android.widget.ViewFlipper;

import com.i114gbox.sdk.activity.I114gBoxActivity;

import com.i114gbox.sdk.utils.I114gBoxCollectActivityUtils;

import com.i114gbox.sdk.utils.I114gBoxLogUtils;

import com.i114gbox.sdk.utils.I114gBoxResourceUtils;

/**

 * 图片滑动Activity

 * 

 * @author SJC

 * 

 */

public class ImageFlipperActivity extends I114gBoxActivity implements

OnGestureListener {

private static String TAG = "ImageFlipperActivity";

private Context ctx = null;

private GestureDetector gestureDetector;// 手势监听

private ViewFlipper viewFlipper;// 视图轮播

@Override

protected void onCreate(Bundle savedInstanceState) {

I114gBoxLogUtils.d(TAG, "The onCreate method execute.");

super.onCreate(savedInstanceState);

I114gBoxCollectActivityUtils.getInstance().addActivity(this);// 收集Activity

ctx = this;

setContentView(I114gBoxResourceUtils.getLayoutId(ctx,

"activity_image_flipper"));

gestureDetector = new GestureDetector(this);

viewFlipper = (ViewFlipper) findViewById(I114gBoxResourceUtils.getId(

ctx, "vf_image_flipper"));

viewFlipper.addView(addImageView(I114gBoxResourceUtils.getDrawableId(

ctx, "flipper_01")));

viewFlipper.addView(addImageView(I114gBoxResourceUtils.getDrawableId(

ctx, "flipper_02")));

viewFlipper.addView(addImageView(I114gBoxResourceUtils.getDrawableId(

ctx, "flipper_03")));

viewFlipper.addView(addImageView(I114gBoxResourceUtils.getDrawableId(

ctx, "flipper_04")));

viewFlipper.addView(addImageView(I114gBoxResourceUtils.getDrawableId(

ctx, "flipper_05")));

viewFlipper.addView(addImageView(I114gBoxResourceUtils.getDrawableId(

ctx, "flipper_06")));

// viewFlipper.addView(addImageView(R.drawable.one));

// viewFlipper.addView(addImageView(R.drawable.two));

// viewFlipper.addView(addImageView(R.drawable.three));

// viewFlipper.addView(addImageView(R.drawable.four));

// viewFlipper.addView(addImageView(R.drawable.five));

Button backButton = (Button) findViewById(I114gBoxResourceUtils.getId(

ctx, "btn_image_flipper_back"));

backButton.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

finish();

}

});

}

/** 添加ImageView控件 **/

private View addImageView(int id) {

ImageView imageView = new ImageView(this);

imageView.setImageResource(id);

return imageView;

}

@Override

public boolean onTouchEvent(MotionEvent event) {

I114gBoxLogUtils.d(TAG, "The onTouchEvent method execute.");

return gestureDetector.onTouchEvent(event);

}

@Override

public boolean onDown(MotionEvent e) {

I114gBoxLogUtils.d(TAG, "The onDown method execute.");

return false;

}

@Override

public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,

float velocityY) {

I114gBoxLogUtils.d(TAG, "The onFling method execute.");

I114gBoxLogUtils.i(TAG, "e1.x:" + e1.getX() + "|" + "e2.x:" + e2.getX()

+ "|" + "velocityX:" + velocityX + "|" + "velocityY:"

+ velocityY);

if (e1.getX() - e2.getX() > 120) {

this.viewFlipper.setInAnimation(AnimationUtils.loadAnimation(ctx,

R.anim.push_left_in));// 进入屏幕的动画

this.viewFlipper.setOutAnimation(AnimationUtils.loadAnimation(ctx,

R.anim.push_left_out));// 离开屏幕的动画

this.viewFlipper.showNext();// 手动显示下一个视图

return true;

} else if (e1.getX() - e2.getX() < -120) {

this.viewFlipper.setInAnimation(AnimationUtils.loadAnimation(ctx,

R.anim.push_right_in));

this.viewFlipper.setOutAnimation(AnimationUtils.loadAnimation(ctx,

R.anim.push_right_out));

this.viewFlipper.showPrevious();// 手动显示前一个视图

return true;

}

return false;

}

@Override

public void onLongPress(MotionEvent e) {

I114gBoxLogUtils.d(TAG, "The onLongPress method execute.");

}

@Override

public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,

float distanceY) {

I114gBoxLogUtils.d(TAG, "The onScroll method execute.");

I114gBoxLogUtils.i(TAG, "e1.X:" + e1.getX() + "|" + "e2.X:" + e2.getX()

+ "|" + "distanceX:" + distanceX + "|" + "distanceY:"

+ distanceY);

return false;

}

@Override

public void onShowPress(MotionEvent e) {

I114gBoxLogUtils.d(TAG, "The onShowPress method execute.");

}

@Override

public boolean onSingleTapUp(MotionEvent e) {

I114gBoxLogUtils.d(TAG, "The onSingleTapUp method execute.");

return false;

}

}

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢