Chinaunix首页 | 论坛 | 博客
  • 博客访问: 224657
  • 博文数量: 53
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1506
  • 用 户 组: 普通用户
  • 注册时间: 2013-07-25 10:52
文章分类
文章存档

2014年(22)

2013年(31)

我的朋友

分类: Android平台

2014-02-27 18:22:04

方法一(实现原理,主要依靠移动动画),主要代码如下:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
    
private OnTouchListener gonggaoOnTouch() {
        return new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                // TODO Auto-generated method stub
                switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN: {
                    break;
                }
                case MotionEvent.ACTION_UP: {
                    if (mAdsV.getTop() == mSlipRl.getTop() && moveY < 0) {
                        topTag();
                    } else if (moveY < -200) {
                        topTag();
                    } else {
                        nextTag();
                    }
                    break;
                }
                }
                gestureDetector.onTouchEvent(event);
                return true;
            }
            private void topTag() {
                animBuilder.setDurationMillis(400).setFromYValue(formY)
                        .setToYValue(mAdsV.getTop() - mSlipRl.getTop());
                bgMoveAnim = animBuilder.build();
                bgMoveAnim.setAnimationListener(new AnimationListener() {
                    @Override
                    public void onAnimationStart(Animation animation) {
                        // TODO Auto-generated method stub
                    }
                    @Override
                    public void onAnimationRepeat(Animation animation) {
                        // TODO Auto-generated method stub
                    }
                    @Override
                    public void onAnimationEnd(Animation animation) {
                        // TODO Auto-generated method stub
                        mSlipRl.clearAnimation();
                        setLayout(mSlipRl, 0, mAdsV.getTop(), 0);
                        mSlipRl.invalidate();
                    }
                });
                mSlipRl.startAnimation(bgMoveAnim);
            }
            private void nextTag() {
                animBuilder.setDurationMillis(400).setFromYValue(formY)
                        .setToYValue(mAdsV.getBottom() - mSlipRl.getTop());
                bgMoveAnim = animBuilder.build();
                bgMoveAnim.setAnimationListener(new AnimationListener() {
                    @Override
                    public void onAnimationStart(Animation animation) {
                        // TODO Auto-generated method stub
                    }
                    @Override
                    public void onAnimationRepeat(Animation animation) {
                        // TODO Auto-generated method stub
                    }
                    @Override
                    public void onAnimationEnd(Animation animation) {
                        // TODO Auto-generated method stub
                        mSlipRl.clearAnimation();
                        setLayout(mSlipRl, 0, mAdsV.getBottom(), 0);
                        mSlipRl.invalidate();
                    }
                });
                mSlipRl.startAnimation(bgMoveAnim);
            }
        };
    }


方法二,主要代码如下:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
    
@Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        // TODO Auto-generated method stub
        switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            lastY = event.getY();
            if(null==mLocationSlip||null==mSlipDown||null==mSlipMove){
                mLocationSlip = this.findViewById(R.id.location_slip);
                mSlipDown = this.findViewById(R.id.down_slip);
                mSlipMove = this.findViewById(R.id.move_slip);
            }
            top = mSlipMove.getTop();
            bottom = mSlipDown.getBottom();
            if (top <= lastY && lastY <= (bottom + top)) {
                isMove = true;
                return true;
            }
        case MotionEvent.ACTION_MOVE:
            if (isMove) {
                float newY = event.getY();
                int ay = (int) ((lastY - newY) * 0.5);
                lastY = newY;
                mSlipMove.offsetTopAndBottom(-ay);
            }
            break;
        case MotionEvent.ACTION_UP:
            if (isMove) {
                isMove = false;
                top = mSlipMove.getTop();
                System.out.println(top + "====" + bottom + "==="+ mLocationSlip.getBottom()*2/3);
                if(!location&&top<(mLocationSlip.getBottom()*2/3)){
                    setLayout(mSlipMove, 0, 0, 0);
                    location=true;
                }else if(location&&top>(mLocationSlip.getBottom()*1/3)){
                    setLayout(mSlipMove, 0, mLocationSlip.getBottom(), 0);
                    location=false;
                }else if(!location){
                    setLayout(mSlipMove, 0, mLocationSlip.getBottom(), 0);
                }else{
                    setLayout(mSlipMove, 0, 0, 0);
                }
            }
            break;
        }
        return super.dispatchTouchEvent(event);
    }



主要知识点,就是重新设置了View的位置,android 3.0之后View可以同setTop(),setLeft()等一系列方法直接设置位置方便了开发。3.0之前位置变化设置方法:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
    
/**
     * 给view重置位置
     * @param view
     * @param x
     * @param y
     * @param b
     */
    public void setLayout(View view, int x, int y, int b) {
        MarginLayoutParams margin = new MarginLayoutParams(
                view.getLayoutParams());
        margin.setMargins(x, y, 0, b);
        RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
                margin);
        view.setLayoutParams(layoutParams);
    }


文章涉及知识点不多,相信一看就懂。如有问题,留言
阅读(1750) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~