首页 > 代码库 > Android特效开发(仿zaker用手向上推动的效果(推动门效果))

Android特效开发(仿zaker用手向上推动的效果(推动门效果))

Android特效开发(仿zaker用手向上推动的效果(推动门效果))

[1].[代码] [Java]代码 跳至 [1]

?
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
publicclass PullDoorView extendsRelativeLayout { 
   
    privateContext mContext; 
   
    privateScroller mScroller; 
   
    privateint mScreenWidth = 0
   
    privateint mScreenHeigh = 0
   
    privateint mLastDownY = 0
   
    privateint mCurryY; 
   
    privateint mDelY; 
   
    privateboolean mCloseFlag = false
   
    privateImageView mImgView; 
   
    publicPullDoorView(Context context) { 
        super(context); 
        mContext = context; 
        setupView(); 
    
   
    publicPullDoorView(Context context, AttributeSet attrs) { 
        super(context, attrs); 
        mContext = context; 
        setupView(); 
    
   
    @SuppressLint("NewApi"
    privatevoid setupView() { 
   
        // 这个Interpolator你可以设置别的 我这里选择的是有弹跳效果的Interpolator 
        Interpolator polator = newBounceInterpolator(); 
        mScroller = newScroller(mContext, polator); 
   
        // 获取屏幕分辨率 
        WindowManager wm = (WindowManager) (mContext 
                .getSystemService(Context.WINDOW_SERVICE)); 
        DisplayMetrics dm = newDisplayMetrics(); 
        wm.getDefaultDisplay().getMetrics(dm); 
        mScreenHeigh = dm.heightPixels; 
        mScreenWidth = dm.widthPixels; 
   
        // 这里你一定要设置成透明背景,不然会影响你看到底层布局 
        this.setBackgroundColor(Color.argb(0,0,0,0)); 
        mImgView = newImageView(mContext); 
        mImgView.setLayoutParams(newLayoutParams(LayoutParams.MATCH_PARENT, 
                LayoutParams.MATCH_PARENT)); 
        mImgView.setScaleType(ImageView.ScaleType.FIT_XY);// 填充整个屏幕 
        mImgView.setImageResource(R.drawable.bg1);// 默认背景 
        addView(mImgView); 
    
   
    // 设置推动门背景 
    publicvoid setBgImage(intid) { 
        mImgView.setImageResource(id); 
    
   
    // 设置推动门背景 
    publicvoid setBgImage(Drawable drawable) { 
        mImgView.setImageDrawable(drawable); 
    
   
    // 推动门的动画 
    publicvoid startBounceAnim(intstartY, intdy, intduration) { 
        mScroller.startScroll(0, startY, 0, dy, duration); 
        invalidate(); 
    
   
    @Override 
    publicboolean onTouchEvent(MotionEvent event) { 
        intaction = event.getAction(); 
        switch(action) { 
        caseMotionEvent.ACTION_DOWN: 
            mLastDownY = (int) event.getY(); 
            System.err.println("ACTION_DOWN="+ mLastDownY); 
            returntrue
        caseMotionEvent.ACTION_MOVE: 
            mCurryY = (int) event.getY(); 
            System.err.println("ACTION_MOVE="+ mCurryY); 
            mDelY = mCurryY - mLastDownY; 
            // 只准上滑有效 
            if(mDelY < 0) { 
                scrollTo(0, -mDelY); 
            
            System.err.println("-------------  " + mDelY); 
   
            break
        caseMotionEvent.ACTION_UP: 
            mCurryY = (int) event.getY(); 
            mDelY = mCurryY - mLastDownY; 
            if(mDelY < 0) { 
   
                if(Math.abs(mDelY) > mScreenHeigh / 2) { 
   
                    // 向上滑动超过半个屏幕高的时候 开启向上消失动画 
                    startBounceAnim(this.getScrollY(), mScreenHeigh, 450); 
                    mCloseFlag = true
   
                }else
                    // 向上滑动未超过半个屏幕高的时候 开启向下弹动动画 
                    startBounceAnim(this.getScrollY(), -this.getScrollY(),1000); 
   
                
            
   
            break
        
        returnsuper.onTouchEvent(event); 
    
   
    @Override 
    publicvoid computeScroll() { 
   
        if(mScroller.computeScrollOffset()) { 
            scrollTo(mScroller.getCurrX(), mScroller.getCurrY()); 
            Log.i("scroller","getCurrX()= " + mScroller.getCurrX() 
                    +"     getCurrY()=" + mScroller.getCurrY() 
                    +"  getFinalY() =  " + mScroller.getFinalY()); 
            // 不要忘记更新界面 
            postInvalidate(); 
        }else
            if(mCloseFlag) { 
                this.setVisibility(View.GONE); 
            
        
    
   
}

Android特效开发(仿zaker用手向上推动的效果(推动门效果))