2011-09-29 10:07:50|  分类: android资料|举报|字号 订阅

Android实现上下滑动效果

1.     概念:

比如用Scroll手势在浏览器中滚屏,用Fling在阅读器中翻页等。在在Android系统中,

手势的识别是通过 GestureDetector.OnGestureListener接口来实现的。

我们先来明确一些概念:

(1)     首先:Android的事件处理机制是基于Listener(监听器)来实现的,比我们今天所

说的触摸屏相关的事件,就是通过 onTouchListener

(2)     其次:所有View的子类都可以通过setOnTouchListener()setOnKeyListener() 等方法来添加对某一类事件的监听器。

(3)     第三:Listener一般会以Interface(接口)的方式来提供,其中包含一个或多个abstract(抽象)方法,我们需要实现这些方法来完成onTouch()onKey()等等的操作。

2.     实践:

下面我们实现一个手触摸滑动的效果。

编写一个类,继承activity实现OnGestureListener接口。

public class Weight extends Activity implements OnGestureListener {

    private ViewFlipper flipper;

    private GestureDetector detector;

    protected void onCreate(Bundle savedInstanceState) {

       super.onCreate(savedInstanceState);

       flipper = new ViewFlipper(this);

       detector = new GestureDetector(this);

       LinearLayout lin = new LinearLayout(this);

        lin.setLayoutParams(newLinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT));

        lin.setOrientation(LinearLayout.VERTICAL);

        addTxt(lin);

        this.addContentView(lin, new ViewGroup.LayoutParams(200 ,200));

    }

    public void addTxt(ViewGroup view)

    {

       TextView txt1 = new TextView(this);

txt1.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));

        txt1.setText("11");

       ...txt2和txt3一样设定

        flipper.addView(txt1);

        flipper.addView(txt2);

        flipper.addView(txt3);

        view.addView(flipper);

    }

    /**

     * 重写GestureDeterctor类里面的onTouchEvent方法

     */

    public boolean onTouchEvent(MotionEvent event) {

       Log.i("golf", "onTouch");

       return this.detector.onTouchEvent(event);

    }

   .....

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

           float velocityY) {

       if (e1.getY() > e2.getY()) 

        this.flipper.showNext();

        else if (e1.getY() < e2.getY()) 

            this.flipper.showPrevious();

       else

            return false;

        return true;

    }

}

这里面用到了两个对象,ViewFlipperGestureDetector

我们先说说这ViewFilpper继承自FrameLayout、类似于一个容器,它有一个优点每次只能显示一个view,利用这个优点,我们可以实现类似的翻页的效果。

事先要把View都添加到ViewFlipper中去,代码如下:

TextView txt1 = new TextView(this);

       txt1.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));

        txt1.setText("11");

        ...txt2和txt3一样设定

        //添加到viewFlipper

        flipper.addView(txt1);

        flipper.addView(txt2);

        flipper.addView(txt3);

效果图:

 滚动之后

分析:

我们先从简单的入手,在一个activity中实现滚动,不要再弹出来的dialog中实现这个滚动效果。

要想实现滚动效果,必须实现android.view.GestureDetector包中的OnGestureListener接口,这个接口里面有六个要实现的方法onDown(MotionEvent e)onShowPress(MotionEvent e)onSingleTapup(MotionEvent e)onScroll(MotionEvent e1, MotionEvent e2, float distance, float distanceY)onLongPress(MotionEvent e)

onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocity)

我们所要做的事就是重写onTouchEvent放,通过GestureDetector对象获取触摸的类型,可能是按下、长时间按下、滑动等、就是对应的6个方法。

下面是重写的onTouchEvent方法:

/**

     * 重写GestureDeterctor类里面的onTouchEvent方法

     */

    public boolean onTouchEvent(MotionEvent event) {

       System.out.println("onTouch");

       return this.detector.onTouchEvent(event);

    }

这里面的变量detectoronCreate中实例化。

由于我们是滑动的效果,所以只要重写onFling(MotionEvent e1, MotionEvent e2, float velocityX, floatvelocity)方法

参数说明:(我的理解)

第一个参数:事件源的起始点

第二个参数:事件源的终点

第三个参数:水平方向滑动的距离

第四个参数:垂直方向滑动的距离。

下面是重写的onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocity)方法。


    public boolean onFling(MotionEvent e1, MotionEvent e2, floatvelocityX,

           float velocityY) {

       System.out.println("onFling");

       if (e1.getY() > e2.getY())

        flipper.showNext();

         else if (e1.getY() < e2.getY()) 

            flipper.showPrevious();

         else 

            return false;

       return true;

    }

其他的六个方法比较简单,通过system.out.println()方法看看什么时候调用这个方法。

3.     升华

接下来我们想将这个activitydialog的形式在其他的activity中显示。

先回忆一下我们dialog是如何创建的:

Dialog dialog = newAlertDialog.Builder(this).setView(flipper).create();

setView(View view)这个参数很灵活。

但是自己尝试了各种方法想在dialog实现这种翻页的效果,但是都是失败的。

那我们就通过视觉上的欺骗的方法来实现跳出来像dialogactivity

我们新建一个MainActivity,放一个button添加onclickListener事件。

事件中的代码:

Intent intent = new Intent();

       intent.setClass(this, Weight.class);

       startActivity(intent);

这样跳转肯定是跳转到新的activity中了,如何实现视觉上的欺骗呢?

只需要在申明这个activity中,给Weight添加一个属性:

代码如下:

<activity android:name=".Weight"

           android:label="@string/app_name"

           android:theme="@android:style/Theme.Dialog">

       activity>

Android:theme=”@android:style/Theme.Dialog”这个属性就会让Weight乖乖的像dialog一样显示了.

更多相关文章

  1. Android实现无标题栏全屏的方法
  2. Android中EditText 设置 imeOptions 无效问题的解决方法
  3. Android有两种方法检测USB设备插入
  4. android android屏幕禁止休眠和锁屏的方法
  5. android TextView 阴影效果,和使用style学习
  6. Android向服务器传接和接收数据的方法汇总
  7. 登录时旋转等待效果
  8. android spinner默认样式不支持换行和修改字体样式 的解决方法

随机推荐

  1. Android图形图画学习(5)——解码图片
  2. Android(安卓)常用的三大组件
  3. Android-配置文件中设置“android:clicka
  4. android studio报错:ClassLoader referenc
  5. Android(安卓)jni初探
  6. ContentProvider组件详细的使用方法
  7. Android(安卓)扬声器与听筒的切换
  8. Android(安卓)contentResolver 进行query
  9. Android中SurfaceView简单使用
  10. Android(安卓)Studio update失败问题 som