Transition

똘이주인·2021년 8월 9일
1

Transition

트랜지션은 장면이 전환될 때의 애니메이션에 대한 정보를 담고 있는 추상 클래스

Transition 방식은 SDKVersion 21 이상부터 지원하기 때문에 그 이하의 버전에서는 사용이 불가능

Transitions API

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

```java
TransitionManager.beginDelayedTransition(layoutFrame);

            if(text.getVisibility() == View.VISIBLE){
                text.setVisibility(View.GONE);
            } else {
                text.setVisibility(View.VISIBLE);
            }

```
  1. SLIDE

```java
TransitionManager.beginDelayedTransition(layoutFrame, new Slide(Gravity.RIGHT));
 
            if(text.getVisibility() == View.VISIBLE){
                text.setVisibility(View.GONE);
            } else {
                text.setVisibility(View.VISIBLE);
            }

```
  1. SCALE

```java
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);
            }

```
  1. RECOLOR

    ```java
    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));
                }
    ```
  2. ROTATE

```java
TransitionManager.beginDelayedTransition(layoutFrame, new Rotate());
            if(image.getRotation() == 0){
                image.setRotation(90);
            } else {
                image.setRotation(0);
            }
```
  1. CHANGE_TEXT

```java
TransitionManager.beginDelayedTransition(layoutFrame,
                    new ChangeText().setChangeBehavior(ChangeText.CHANGE_BEHAVIOR_OUT_IN));
 
            if(text.getText().equals("Hello")){
                text.setText("World");
            } else {
                text.setText("Hello");
            }
```
  1. PROGRESS_TRANSITION

```java
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;
    }
}

// ------------------------------------------------------------------------------------

ProgressTransition progressTransition = new ProgressTransition();
            progressTransition.setDuration(1000);
            TransitionManager.beginDelayedTransition(layoutFrame, progressTransition);
            progressBar.setProgress(100);

```

0개의 댓글