woong's

Slide extend/collapse Animation 사용하기 본문

Develop/Android

Slide extend/collapse Animation 사용하기

dlsdnd345 2016. 2. 14. 17:15

Slide extend/collapse Animation 사용하기 



Colored By Color Scripter

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
package com.handstudio.android.healthup.util;
 
import android.animation.ValueAnimator;
import android.annotation.SuppressLint;
import android.view.View;
import android.view.ViewGroup;
 
@SuppressLint("NewApi")
public class SlideAnimation {
 
    /**
     * 레이아웃 확장
     * @param view
     */
    public void expand(View view) {
        //set Visible
        view.setVisibility(View.VISIBLE);
 
        final int widthSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
        final int heightSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
        view.measure(widthSpec, heightSpec);
 
        ValueAnimator mAnimator = slideAnimator(view, 0, view.getMeasuredHeight());
        mAnimator.start();
    }
 
    /**
     * 레이아웃 축소
     * @param view
     */
    public void collapse(View view) {
        int finalHeight = view.getHeight();
        ValueAnimator mAnimator = slideAnimator(view,finalHeight, 0);
        mAnimator.start();
    }
 
    /**
     * 슬라이드 애니메이션
     * @param view
     * @param start
     * @param end
     * @return
     */
    public ValueAnimator slideAnimator(final View view,int start, int end) {
 
        ValueAnimator animator = ValueAnimator.ofInt(start, end);
 
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator valueAnimator) {
                //Update Height
                int value = (Integer) valueAnimator.getAnimatedValue();
                ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
                layoutParams.height = value;
                view.setLayoutParams(layoutParams);
            }
        });
        return animator;
    }
    
}
 

 

 

 

​사용방법 


Colored By Color Scripter

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
OnTouchListener mOnTouchListener = new OnTouchListener(){
 
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            return gesture.onTouchEvent(event);
        }
        
    };
    
    /**
     * 제스처를 통해서 위아래 판단
     * 위 아래 애니메이션을 통해서 액션바 추가/제거
     */
    final GestureDetector gesture = new GestureDetector(getActivity(),new GestureDetector.SimpleOnGestureListener() {
 
        @Override
        public boolean onDown(MotionEvent e) {
            return true;
        }
 
        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
                float velocityY) {
 
            if(e1.getY() > e2.getY()){ // 아래에서 위로(Down)
                slideAnimation.collapse(layoutActionbar);
            }else if(e1.getY() < e2.getY()){ // 위에서 아래로(Up)
                if(layoutActionbar.getHeight() == 0){
                    slideAnimation.expand(layoutActionbar);
                }
            }
            return super.onFling(e1, e2, velocityX, velocityY);
        }
    });


'Develop > Android' 카테고리의 다른 글

Android HalfCircle 사용하기  (0) 2016.02.14
Android Circle Indicator 동적으로 사용하기  (0) 2016.02.14
Android xml  (0) 2016.02.14
Android View 최상위로 올리기  (0) 2016.02.14
Android Vertical SeekBar 사용하기  (0) 2016.02.14
Comments