woong's

Android Trasitions API 사용하기 본문

Develop/Android

Android Trasitions API 사용하기

dlsdnd345 2016. 10. 12. 15:41

Android Trasitions API 사용하기


Android Animation 을 좀더 쉽게 사용할수 있는 Trasitions API 가 있어서 정리해 보려 합니다.

앱구성시 자주 쓰일것 같은 애니메이션만 예제로 작성해 보려 합니다.


  • Transitions API
    구글은 액티비티간 화면 전환을 위해 Android 5.0부터 이 API를 제공
  •  좋은 소식은 더 아래 버전에서도 사용할 수 있다는 점. Transitions Everywhere는 안드로이드 Transition API의 백포트입니다.(안드로이드 4.0이상 애니메이션 백포트 지원, 안드로이드 2.2이상 API호환가능)

1
2
3
dependencies {
    compile "com.andkulikov:transitionseverywhere:1.6.5"
}
cs


참고자료


https://realm.io/kr/news/aw213-android-transitions-api-rxjava/

https://github.com/andkulikov/Transitions-Everywhere



VISIBLE




1
2
3
4
5
6
7
TransitionManager.beginDelayedTransition(layoutFrame);
 
            if(text.getVisibility() == View.VISIBLE){
                text.setVisibility(View.GONE);
            } else {
                text.setVisibility(View.VISIBLE);
            }
cs



SLIDE




1
2
3
4
5
6
7
TransitionManager.beginDelayedTransition(layoutFrame, new Slide(Gravity.RIGHT));
 
            if(text.getVisibility() == View.VISIBLE){
                text.setVisibility(View.GONE);
            } else {
                text.setVisibility(View.VISIBLE);
            }
cs




SCALE




1
2
3
4
5
6
7
8
9
10
11
12
13
TransitionSet set = new TransitionSet()
                    .addTransition(new Scale(0.7f))
                    .addTransition(new Fade());
 
            if(text.getVisibility() == View.VISIBLE){
                set.setInterpolator(new FastOutLinearInInterpolator());
                TransitionManager.beginDelayedTransition(layoutFrame, set);
                text.setVisibility(View.INVISIBLE);
            } else {
                set.setInterpolator(new LinearOutSlowInInterpolator());
                TransitionManager.beginDelayedTransition(layoutFrame, set);
                text.setVisibility(View.VISIBLE);
            }
cs





RECOLOR




1
2
3
4
5
6
7
8
9
TransitionManager.beginDelayedTransition(layoutFrame, new Recolor());
 
            if(button.getCurrentTextColor() == getResources().getColor(R.color.colorPrimary)){
                button.setTextColor(getResources().getColor(R.color.colorAccent));
                button.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
            } else {
                button.setTextColor(getResources().getColor(R.color.colorPrimary));
                button.setBackgroundColor(getResources().getColor(R.color.colorAccent));
            }
cs




ROTATE




1
2
3
4
5
6
TransitionManager.beginDelayedTransition(layoutFrame, new Rotate());
            if(image.getRotation() == 0){
                image.setRotation(90);
            } else {
                image.setRotation(0);
            }
cs




CHANGE_TEXT




1
2
3
4
5
6
7
8
TransitionManager.beginDelayedTransition(layoutFrame,
                    new ChangeText().setChangeBehavior(ChangeText.CHANGE_BEHAVIOR_OUT_IN));
 
            if(text.getText().equals("Hello")){
                text.setText("World");
            } else {
                text.setText("Hello");
            }
cs




PROGRESS_TRANSITION



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
private class ProgressTransition extends Transition {
 
    /**
     * Property is like a helper that contain setter and getter in one place
     */
    private static final Property<ProgressBar, Integer> PROGRESS_PROPERTY = 
        new IntProperty<ProgressBar>() {
 
        @Override
        public void setValue(ProgressBar progressBar, int value) {
            progressBar.setProgress(value);
        }
 
        @Override
        public Integer get(ProgressBar progressBar) {
            return progressBar.getProgress();
        }
    };
 
    /**
      * Internal name of property. Like a intent bundles 
      */
    private static final String PROPNAME_PROGRESS = "ProgressTransition:progress";
 
    @Override
    public void captureStartValues(TransitionValues transitionValues) {
        captureValues(transitionValues);
    }
 
    @Override
    public void captureEndValues(TransitionValues transitionValues) {
        captureValues(transitionValues);
    }
 
    private void captureValues(TransitionValues transitionValues) {
        if (transitionValues.view instanceof ProgressBar) {
            // save current progress in the values map
            ProgressBar progressBar = ((ProgressBar) transitionValues.view);
            transitionValues.values.put(PROPNAME_PROGRESS, progressBar.getProgress());
        }
    }
 
    @Override
    public Animator createAnimator(ViewGroup sceneRoot, TransitionValues startValues, 
            TransitionValues endValues) {
        if (startValues != null && endValues != null && endValues.view instanceof ProgressBar) {
            ProgressBar progressBar = (ProgressBar) endValues.view;
            int start = (Integer) startValues.values.get(PROPNAME_PROGRESS);
            int end = (Integer) endValues.values.get(PROPNAME_PROGRESS);
            if (start != end) {
                // first of all we need to apply the start value, because right now
                // the view is have end value
                progressBar.setProgress(start);
                // create animator with our progressBar, property and end value
                return ObjectAnimator.ofInt(progressBar, PROGRESS_PROPERTY, end);
            }
         }
         return null;
    }
}
cs



1
2
3
4
ProgressTransition progressTransition = new ProgressTransition();
            progressTransition.setDuration(1000);
            TransitionManager.beginDelayedTransition(layoutFrame, progressTransition);
            progressBar.setProgress(100);
cs


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

Android Proguard 사용하기  (0) 2017.04.04
Android ButterKnife 사용하기  (0) 2017.02.10
Android 국가별 폴더 분기  (0) 2016.10.10
홈 버튼 오버라이드  (0) 2016.07.14
Android Zxing 사용하기  (1) 2016.05.10
Comments