• Android实现左右滑动效果(转)


    本示例演示在Android中实现图片左右滑动效果。

    主要效果如下图:

     

    程序结构:

    MainActivity文件中代码:

    package com.android.flip;
    import android.app.Activity;
    import android.os.Bundle;
    import android.view.GestureDetector;
    import android.view.MotionEvent;
    import android.view.View;
    import android.view.GestureDetector.OnGestureListener;
    import android.view.animation.AnimationUtils;
    import android.widget.ImageView;
    import android.widget.ViewFlipper;

    /**
    * Android实现左右滑动效果
    *
    @author Administrator
    *
    */
    public class MainActivity extends Activity implements OnGestureListener {
    private ViewFlipper flipper;
    private GestureDetector detector;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    detector = new GestureDetector(this);
    flipper = (ViewFlipper) this.findViewById(R.id.ViewFlipper1);

    flipper.addView(addTextView(R.drawable.one));
    flipper.addView(addTextView(R.drawable.two));
    flipper.addView(addTextView(R.drawable.three));
    flipper.addView(addTextView(R.drawable.four));
    flipper.addView(addTextView(R.drawable.five));
    }

    private View addTextView(int id) {
    ImageView iv = new ImageView(this);
    iv.setImageResource(id);
    return iv;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
    // TODO Auto-generated method stub
    return this.detector.onTouchEvent(event);
    }

    @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.flipper.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.push_left_in));
    this.flipper.setOutAnimation(AnimationUtils.loadAnimation(this, R.anim.push_left_out));
    this.flipper.showNext();
    return true;
    } else if (e1.getX() - e2.getX() < -120) {
    this.flipper.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.push_right_in));
    this.flipper.setOutAnimation(AnimationUtils.loadAnimation(this, R.anim.push_right_out));
    this.flipper.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;
    }
    }

    main.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"
    >
    <ViewFlipper android:id="@+id/ViewFlipper1"
    android:layout_width
    ="fill_parent"
    android:layout_height
    ="fill_parent">
    </ViewFlipper>
    </LinearLayout>

    push_left_in.xml文件中代码:

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

    push_left_out.xml文件中代码:

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

    push_right_in.xml文件中代码:

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

    push_right_out.xml文件中代码:

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


    </set>

    结束。^_^

    本文来源:http://www.cnblogs.com/hanyonglu/archive/2012/02/13/2349827.html

  • 相关阅读:
    YUI+Ant 实现JS CSS压缩
    13.QT-QMainWindow组件使用
    12.QT4.7.4-解决WIN平台和Linux平台中文乱码,QLineEdit右击菜单中文显示
    11.QT-布局管理器(Box,Grid,Form,Stacked)
    10.QT-定时器
    9.QT-标准对话框
    8.QT-对话框(模态与非模态)
    7.QT-Qt对象间的父子关系
    6.QT-简易计算器实现(详解)
    5.QT-QString类
  • 原文地址:https://www.cnblogs.com/jh5240/p/2432064.html
Copyright © 2020-2023  润新知