Android使用ViewFlipper实现左右滑动效果面
在我的博客中,上次是使用ViewPager实现左右滑动的效果的,请看文章:Android使用ViewPager实现左右滑动效果 。
这次我来使用ViewFlipper实现这种效果,好了,先看看效果吧:
效果看完了就来实现这个效果。
1.布局文件
主界面使用下面的布局:
- <?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>
简单的介绍一下布局文件:最外层是一个FrameLayout,使用FrameLayout就是为了是的下面的四个点在ViewFlipper上面。LayoutLayout在FrameLayout的下面和水平居中。
2.ViewFlipper的使用
- 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());
在Activity中声明一个ViewFlipper的对象,在布局中找到。将三张图片加到ViewFlipper中,另外再加一个View,这个View是从XML布局文件中得到的。布局文件如下:
- <?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>
在这个布局文件中有一个Button,用于跳转Activity用。
在Activity中声明一个GestureDetector对象,在onCreate方法中分配内存。
detector = new GestureDetector(this);
使用this为参数,那么就要使得activity类impllements OnGestureListener接口。重写几个方法。覆盖父类的onTouchEvent方法,在这个方法中如下写:
- @Override
- public boolean onTouchEvent(MotionEvent event) {
- // TODO Auto-generated method stub
- return this.detector.onTouchEvent(event);
- }
这样就使得detector能接受消息响应了。
在实现OnGestureListener的方法中判断用户的滑动来切换界面:
- @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);
- }
- }
界面切换的时候改变下面的四个小ImageView的图片。切换的动画在res/anim文件夹中,这里就不多说了。
整个工程的下载:Android使用ViewFlipper实现左右滑动效果面。
-------------------------------------------------------------------------------------------------------------------------------
在android的图片的浏览时候经常使用。最近在网上查找资料自己也做一个如下:
- package com.easyway.buttonpageflipper;
- import com.easyway.buttonpageflipper.R;
- import android.app.Activity;
- import android.graphics.PixelFormat;
- import android.os.Bundle;
- import android.os.Handler;
- import android.os.Message;
- import android.view.GestureDetector;
- import android.view.GestureDetector.OnGestureListener;
- import android.view.Gravity;
- import android.view.MotionEvent;
- import android.view.View;
- import android.view.WindowManager;
- import android.view.WindowManager.LayoutParams;
- import android.view.animation.AnimationUtils;
- import android.widget.ImageView;
- import android.widget.ViewFlipper;
- /**
- * Android实现带渐显按钮的左右滑动效果 自然状态下按钮不可见,触摸屏幕时显示按钮
- *
- * @author longgangbai
- *
- */
- public class ButtonpageflipperActivity extends Activity implements OnGestureListener{
- // 声明两个按钮,分别代表向左和向右滑动
- private ImageView btnLeft = null;
- private ImageView btnRight = null;
- // 设置WindowManager
- private WindowManager wm = null;
- private WindowManager.LayoutParams wmParams = null;
- // ImageView的alpha值
- private int mAlpha = 0;
- private boolean isHide;
- private ViewFlipper viewFlipper = null;
- private GestureDetector detector;
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- setTitle("Android实现渐显按钮的左右滑动效果");
- viewFlipper = (ViewFlipper) this.findViewById(R.id.myViewFlipper);
- detector = new GestureDetector(this);
- // 初始化左右按钮
- initImageButtonView();
- viewFlipper.addView(addImageView(R.drawable.photo1));
- viewFlipper.addView(addImageView(R.drawable.photo2));
- viewFlipper.addView(addImageView(R.drawable.photo3));
- viewFlipper.addView(addImageView(R.drawable.photo4));
- viewFlipper.addView(addImageView(R.drawable.photo5));
- }
- private View addImageView(int id) {
- ImageView iv = new ImageView(this);
- iv.setImageResource(id);
- return iv;
- }
- /**
- * 初始化悬浮按钮
- */
- private void initImageButtonView() {
- // 获取WindowManager
- wm = (WindowManager) getApplicationContext().getSystemService("window");
- // 设置LayoutParams相关参数
- wmParams = new WindowManager.LayoutParams();
- // 设置window type
- wmParams.type = LayoutParams.TYPE_PHONE;
- // 设置图片格式,效果为背景透明
- wmParams.format = PixelFormat.RGBA_8888;
- // 设置Window flag参数
- wmParams.flags = LayoutParams.FLAG_NOT_TOUCH_MODAL
- | LayoutParams.FLAG_NOT_FOCUSABLE;
- // 设置x、y初始值
- wmParams.x = 0;
- wmParams.y = 0;
- // 设置窗口长宽数据
- wmParams.width = 50;
- wmParams.height = 50;
- // 创建左右按钮
- createLeftButtonView();
- createRightButtonView();
- }
- /**
- * 设置左边按钮
- */
- private void createLeftButtonView() {
- btnLeft = new ImageView(this);
- btnLeft.setImageResource(R.drawable.left);
- btnLeft.setAlpha(0);
- btnLeft.setOnClickListener(new View.OnClickListener() {
- public void onClick(View arg0) {
- // 上一个图像
- viewFlipper.setInAnimation(ButtonpageflipperActivity.this,
- R.anim.push_left_in);
- viewFlipper.setOutAnimation(ButtonpageflipperActivity.this,
- R.anim.push_left_out);
- viewFlipper.showPrevious();
- }
- });
- // 调整窗口
- wmParams.gravity = Gravity.LEFT | Gravity.CENTER_VERTICAL;
- // 显示图像
- wm.addView(btnLeft, wmParams);
- }
- /**
- * 设置右边按钮
- */
- private void createRightButtonView() {
- btnRight = new ImageView(this);
- btnRight.setImageResource(R.drawable.right);
- btnRight.setAlpha(0);
- btnRight.setOnClickListener(new View.OnClickListener() {
- public void onClick(View arg0) {
- // 下一个图像
- viewFlipper.setInAnimation(ButtonpageflipperActivity.this,
- R.anim.push_right_in);
- viewFlipper.setOutAnimation(ButtonpageflipperActivity.this,
- R.anim.push_right_out);
- viewFlipper.showNext();
- }
- });
- // 调整窗口
- wmParams.gravity = Gravity.RIGHT | Gravity.CENTER_VERTICAL;
- // 显示图像
- wm.addView(btnRight, wmParams);
- }
- /**
- * 设置按钮渐显效果
- */
- private Handler mHandler = new Handler() {
- public void handleMessage(Message msg) {
- if (msg.what == 1 && mAlpha < 255) {
- // 通过设置不透明度设置按钮的渐显效果
- mAlpha += 50;
- if (mAlpha > 255)
- mAlpha = 255;
- btnLeft.setAlpha(mAlpha);
- btnLeft.invalidate();
- btnRight.setAlpha(mAlpha);
- btnRight.invalidate();
- if (!isHide && mAlpha < 255)
- mHandler.sendEmptyMessageDelayed(1, 100);
- } else if (msg.what == 0 && mAlpha > 0) {
- mAlpha -= 10;
- if (mAlpha < 0)
- mAlpha = 0;
- btnLeft.setAlpha(mAlpha);
- btnLeft.invalidate();
- btnRight.setAlpha(mAlpha);
- btnRight.invalidate();
- if (isHide && mAlpha > 0)
- mHandler.sendEmptyMessageDelayed(0, 800);
- }
- }
- };
- private void showImageButtonView() {
- isHide = false;
- mHandler.sendEmptyMessage(1);
- }
- private void hideImageButtonView() {
- new Thread() {
- public void run() {
- try {
- Thread.sleep(1500);
- isHide = true;
- mHandler.sendEmptyMessage(0);
- } catch (Exception e) {
- ;
- }
- }
- }.start();
- }
- @Override
- public boolean onTouchEvent(MotionEvent event) {
- this.detector.onTouchEvent(event);
- switch (event.getAction()) {
- case MotionEvent.ACTION_MOVE:
- case MotionEvent.ACTION_DOWN:
- showImageButtonView();
- break;
- case MotionEvent.ACTION_UP:
- hideImageButtonView();
- break;
- }
- return true;
- }
- @Override
- public void onDestroy() {
- super.onDestroy();
- // 在程序退出(Activity销毁)时销毁窗口
- wm.removeView(btnLeft);
- wm.removeView(btnRight);
- }
- @Override
- public boolean onDown(MotionEvent e) {
- // TODO Auto-generated method stub
- return false;
- }
- @Override
- public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
- float velocityY) {
- if (e1.getX() - e2.getX() > 120) {
- this.viewFlipper.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.push_left_in));
- this.viewFlipper.setOutAnimation(AnimationUtils.loadAnimation(this, R.anim.push_left_out));
- this.viewFlipper.showNext();
- return true;
- } else if (e1.getX() - e2.getX() < -120) {
- this.viewFlipper.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.push_right_in));
- this.viewFlipper.setOutAnimation(AnimationUtils.loadAnimation(this, R.anim.push_right_out));
- this.viewFlipper.showPrevious();
- return true;
- }
- return false;
- }
- @Override
- public void onLongPress(MotionEvent e) {
- // TODO Auto-generated method stub
- }
- @Override
- public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
- float distanceY) {
- // 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;
- }
- }
邮箱:steven9801@163.com
QQ: 48039387