android animation의 종류로는 기본적으로 3가지가 있다.

1. frame animation

2. tween animation (view animation)

3. property animation

.

.

.

.

.

.

.

.

frame animation

frame animation은 xml화일에 <animation-list> 와 <item>을 이용하여 만든다. 이를 ImageView에 src에 연결한다. 코드상에서는 ImageView를 통해서 AnimationDrawable obj형태로 받아오게된다.

res/drawable에 xml 형태로 저장된다.

참조) android docs AnimationDrawable https://developer.android.com/reference/android/graphics/drawable/AnimationDrawable

예시코드)

<!-- Animation frames are wheel0.png through wheel5.png
     files inside the res/drawable/ folder -->
 <animation-list android:id="@+id/selected" android:oneshot="false">
    <item android:drawable="@drawable/wheel0" android:duration="50" />
    <item android:drawable="@drawable/wheel1" android:duration="50" />
    <item android:drawable="@drawable/wheel2" android:duration="50" />
    <item android:drawable="@drawable/wheel3" android:duration="50" />
    <item android:drawable="@drawable/wheel4" android:duration="50" />
    <item android:drawable="@drawable/wheel5" android:duration="50" />
 </animation-list>
// Load the ImageView that will host the animation and
 // set its background to our AnimationDrawable XML resource.
 ImageView img = (ImageView)findViewById(R.id.spinning_wheel_image);
 img.setBackgroundResource(R.drawable.spin_animation);

 // Get the background, which has been compiled to an AnimationDrawable object.
 AnimationDrawable frameAnimation = (AnimationDrawable) img.getBackground();

 // Start the animation (looped playback by default).
 frameAnimation.start();

.

.

.

.

.

.

.

.

tween animation (view animation)

  • view의 display형태만 바꾸는 방법이며 실제로 view의 위치는 바뀌지 않는다. 사용자가 이동된 위치에서 터치를 해도 반응하지 않는다. 
  • animation은 translate, scale, rotate, alpha 4가지가 있다.
  • xml 형태로 만든 animation은 res/anim 폴더안에 넣는다.
  • tween animation은 view, drawable (ShapeDrawable, BitmapDrawable), layout에 적용가능하다.
  • tween animation은 내부적으로 Matrix, Transformation class를 사용한다. 
  • AnimationUtils의 loadAnimation()를 이용해서 xml형태의 Animation을 가져온다.

tween animation에서 만들때 사용가능한 animation tag들은 아래와 같다.

image
image

.

.

image
image
image
image

.

.

<set>을 이용하여 여러가지 animation을 동시에 구현할수 있다.

image
image
image
image

.

.

일반적으로 사용되는 위의 4가지 animation이 아닌 다른 움직임을 구현하기 위해서는 custom animation을 이용한다. 이는 Animation을 extends하고 필요한 method를 override해서 구현한다.

image

.

.

xml형태로 만들어진 tween animation은 layoutAnimation에 적용가능하다. layoutAnimation을 이용하면 layout내의 view들이 처음 등장할때 animation대로 등장하게된다.

https://youtu.be/qMQar9UNqjU?t=2311

image

.

.

activity간 전환에 적용되는 animation도 tween animation (view animation) 이다.

app 내에서 모든 window의 생성, 사라짐에 사용되는 animation를 지정할때는 theme에서 사용될 view animation을 지정해 주면된다.

image
image

단하나 주의점은 animation의 작동시간을 지정해도 0.25초안에 마무리 되게 된다. 

image
image

.

.

fragment 전환간 animation에도 tween animation (view animation)이 사용된다.

image
image

.

.

추가적으로 ViewSwitcher (TextSwitcher, ImageSwitcher), ViewFlipper는 tween animation (view animation)을 이용한다. 맨 아래부분 참조

.

.

.

.

.

.

.

.

property animation

ValueAnimator, ObjectAnimator를 이용하는 방법이 있다. 기본적으로 ObjectAnimator는 ValueAnimator를 기반으로 extends 해서 만들어졌다.

ValueAnimator는 특정범위에안에서 변화된 값을 만들어낸다. 이때 addUpdateListener를 통해 연결된 listener에서 onAnimationUpdate() 안에서 변화되는 값에 접근할수 있다.

Animator, ObjectAnimator를 xml로 만드는 경우에는 res/animator에 만든다.

아래는 code에서 ValueAnimator를 이용한 경우

image

.

아래는 xml를 이용한 ValueAnimator 경우

image
image

.

ObjectAnimator를 code에서 이용하는 법과 xml을 통해 이용하는 방법을 보여주고 있다.

image
image

.

.

animator 를 이용한 animation을 AnimatorSet으로 묶어서 순서를 가지고 수행하거나 동시에 수행하는 방법을 설명하고 있다.

image
image

.

.

Animator를 이용해서 layout내의 view가 등장하거나 사라지거나 다른 view가 등장 사라짐에따라 위치가 변경되는 경우에 사용되는 animation을 만들수 있으며 이를 LayoutTransition이라고 한다.

image

.

.

Animator를 이용한 animation인데 기존의 경우 ValueAnimator가 특정범위에안에서 변화된 값을 만들어내고 addUpdateListener를 통해 연결된 listener에서 onAnimationUpdate() 안에서 변화되는 값을 통해 view의 속성값을 변화시켰다. 아래의 경우도 같은 효과를 가진다. 다만 ViewPropertyAnimator (support library를 사용한겨우 ViewPropertyAnimatorCompat)를 생성하기위해 View.animate (또는 support 라이브러리의 경우 ViewCompat.animate) 에 속성값을 animate할 view를 arg로 받게 된다.

image
image

.

.

RecyclerView내의 item이 생성되거나 사라질때 애니메이션을 이용할수 있는데 이때에도 Animator를 기반으로한 property animation을 사용한다. 

참조 영상 ) RecyclerView 내의 item 생성소멸시 animation 사용예 https://youtu.be/8sQmuafiaAQ?t=220  

image
image

.

.

AdapterViewAnimator, AdapterViewFlipper, StackView는 property animator를 기반으로 하고 있다.

.

.

.

.

.

이름이 ViewAnimator이지만 기본적으로 ViewSwitcher (TextSwitcher, ImageSwitcher), ViewFlipper는 tween animation (view animation)을 이용한다.

AdapterViewAnimator, AdapterViewFlipper, StackView는 property animator를 기반으로 하고 있다.

실제 사용 예시)

VIEW ANIMATION , PROPERTY ANIMATION 둘다 같은 작업을 수행할수있다. 차이점이 있다면 view animation의 경우 animation후에 보이는 것은 변경되었을지 모르지만 실제 존재하는 위치는 같으므로 클릭을 한다고 하면 본래 위치에 클릭해야 해당 view가 이벤트를 받아들이게 된다.Their state changes but their property does not change. 

 ObjectAnimator를 이용하는 것은 property animation이다. ObjectAnimator is a subclass of ValueAnimator

TranslateAnimationRotateAnimationScaleAnimationAlphaAnimation 을 이용하는 것은 view animation이다.  

When using View Animations, use XML instead of doing it programmatically. Using XML files, it is more readable and can be shared among other views.

참조 https://youtu.be/_UWXqFBF86U?t=204

basics  3 types of Animations:

  1. Property Animations— They are used to alter property of objects (Views or non view objects). We specify certain properties(like translateX, TextScaleX) of the objects to change. Various characteristics of animation which can be manipulated are animation duration, whether to reverse it and for how many times we want to repeat the animation etc. They were introduced in Android 3.0 (API level 11).
  2. View Animations — They are used to do simple animations like changing size, position, rotation, control transparency. They are easy to build and are very fast but have their own constraints. For eg — Their state changes but their property does not change. View animations will be covered in part 2.
  3. Drawable Animations — This is used to do animation using drawables. An XML file specifying various list of drawables is made which are run one by one just like a roll of a film. This is not much used so I won’t cover it.

property animation 

  •      – ObjectAnimator를 이용한다. ( ValueAnimator를 기반으로 한다 )
  •      – AnimatorSet 를 이용 여러 view들의 animations을 동시에 진행가능하다. ( with()를 사용하기도한다 )
  •      – 하나의 view에 대한 여러 animation을 동시에 진행하는 경우 ViewPropertyAnimator 를 이용한다. 
animateTextView.animate().rotation(360f).y(500f).setDuration(2000);

View Animation

  • Tween Animation — These are the animations applied on a view which is responsible for scaling, translating, rotating or fading a view (either together or one by one).
  • Frame Animation — These animations are applied using various drawables. In this, we just have to specify a list of drawables in the XML code and the animation runs just like frames of a video.

View Animations are not much used because the same thing can be using ViewPropertyAnimator object which is much faster and readable. Frame animation is similar to Drawable Animation. The more important thing is to understand the new Transition framework which is much useful and provide beautiful animations.

API 21이후에 추간된 ANIMATION

Shared Element Transitions 

Activity Transitions

Circular Reveal

Shared Element Transitions 

http://blogs.quovantis.com/how-to-apply-shared-element-transitions-in-android/


참고자료) 

view animation brief explanation

https://youtu.be/_UWXqFBF86U

A beginners guide to implement Android Animations — Part 1 (2 part series)

https://medium.com/@shubham.bestfriendforu/a-beginners-guide-to-implement-android-animations-part-1-2-part-series-b5fce1fc85

VIEW ANIMATION VS PROPERTY ANIMATION IN ANDROID

https://mahbubzaman.wordpress.com/2015/06/03/view-animation-vs-property-animation-in-android/

Sometimes we need to animate some view in our application. Here is a sample code for translate a view over x axis from its location.


Button bt;
bt = (Button) findViewById(R.id.bt);
bt.setOnClickListener(this);

TranslateAnimation animation = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 300, Animation.RELATIVE_TO_SELF, Animation.RELATIVE_TO_SELF);
animation.setDuration(1000);
animation.setFillAfter(true);
bt.setAnimation(animation);

But the problem of this code is if you want to click this button after animate you can not, why lets read the documentation from android developer site

disadvantage of the view animation system is that it only modified where the View was drawn, and not the actual View itself. For instance, if you animated a button to move across the screen, the button draws correctly, but the actual location where you can click the button does not change, so you have to implement your own logic to handle this.

more

To solve this issue we will use ObjectAnimator class to animate our view.


ObjectAnimator animXNext = ObjectAnimator.ofFloat(bt, "translationX", Animation.RELATIVE_TO_SELF, 300);
animXNext.setDuration(1000);
animXNext.start();

Now you can click on the button. Enjoy

VIEW ANIMATION , PROPERTY ANIMATION 둘다 같은 작업을 수행할수있다. 차이점이 있다면 view animation의 경우 animation후에 보이는 것은 변경되었을지 모르지만 실제 존재하는 위치는 같으므로 클릭을 한다고 하면 본래 위치에 클릭해야 해당 view가 이벤트를 받아들이게 된다.Their state changes but their property does not change. 

 ObjectAnimator를 이용하는 것은 property animation이다. ObjectAnimator is a subclass of ValueAnimator

TranslateAnimationRotateAnimationScaleAnimationAlphaAnimation 을 이용하는 것은 view animation이다.  

When using View Animations, use XML instead of doing it programmatically. Using XML files, it is more readable and can be shared among other views.

참조 https://youtu.be/_UWXqFBF86U?t=204

basics  3 types of Animations:

  1. Property Animations— They are used to alter property of objects (Views or non view objects). We specify certain properties(like translateX, TextScaleX) of the objects to change. Various characteristics of animation which can be manipulated are animation duration, whether to reverse it and for how many times we want to repeat the animation etc. They were introduced in Android 3.0 (API level 11).
  2. View Animations — They are used to do simple animations like changing size, position, rotation, control transparency. They are easy to build and are very fast but have their own constraints. For eg — Their state changes but their property does not change. View animations will be covered in part 2.
  3. Drawable Animations — This is used to do animation using drawables. An XML file specifying various list of drawables is made which are run one by one just like a roll of a film. This is not much used so I won’t cover it.

property animation 

  •      – ObjectAnimator를 이용한다. ( ValueAnimator를 기반으로 한다 )
  •      – AnimatorSet 를 이용 여러 view들의 animations을 동시에 진행가능하다. ( with()를 사용하기도한다 )
  •      – 하나의 view에 대한 여러 animation을 동시에 진행하는 경우 ViewPropertyAnimator 를 이용한다. 
animateTextView.animate().rotation(360f).y(500f).setDuration(2000);

View Animation

  • Tween Animation — These are the animations applied on a view which is responsible for scaling, translating, rotating or fading a view (either together or one by one).
  • Frame Animation — These animations are applied using various drawables. In this, we just have to specify a list of drawables in the XML code and the animation runs just like frames of a video.

View Animations are not much used because the same thing can be using ViewPropertyAnimator object which is much faster and readable. Frame animation is similar to Drawable Animation. The more important thing is to understand the new Transition framework which is much useful and provide beautiful animations.

API 21이후에 추간된 ANIMATION

Shared Element Transitions 

Activity Transitions

Circular Reveal

Shared Element Transitions 

http://blogs.quovantis.com/how-to-apply-shared-element-transitions-in-android/


참고자료) 

view animation brief explanation

https://youtu.be/_UWXqFBF86U

A beginners guide to implement Android Animations — Part 1 (2 part series)

https://medium.com/@shubham.bestfriendforu/a-beginners-guide-to-implement-android-animations-part-1-2-part-series-b5fce1fc85

VIEW ANIMATION VS PROPERTY ANIMATION IN ANDROID

https://mahbubzaman.wordpress.com/2015/06/03/view-animation-vs-property-animation-in-android/

Sometimes we need to animate some view in our application. Here is a sample code for translate a view over x axis from its location.


Button bt;
bt = (Button) findViewById(R.id.bt);
bt.setOnClickListener(this);

TranslateAnimation animation = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 300, Animation.RELATIVE_TO_SELF, Animation.RELATIVE_TO_SELF);
animation.setDuration(1000);
animation.setFillAfter(true);
bt.setAnimation(animation);

But the problem of this code is if you want to click this button after animate you can not, why lets read the documentation from android developer site

disadvantage of the view animation system is that it only modified where the View was drawn, and not the actual View itself. For instance, if you animated a button to move across the screen, the button draws correctly, but the actual location where you can click the button does not change, so you have to implement your own logic to handle this.

more

To solve this issue we will use ObjectAnimator class to animate our view.


ObjectAnimator animXNext = ObjectAnimator.ofFloat(bt, "translationX", Animation.RELATIVE_TO_SELF, 300);
animXNext.setDuration(1000);
animXNext.start();

Now you can click on the button. Enjoy

A beginners guide to implement Android Animations — Part 1 (2 part series)

A beginners guide to implement Android Animations — Part 1 (2 part series)

기존의 UIView에 기능을 추가 해서 사용하는 방법으로는 subclass를 만들어 사용하는 것과 expension을 이용 확대하는 방법이 있다.

단 expension의 경우 stored property를 새로 추가 할수 는 없다. (참조 https://docs.swift.org/swift-book/LanguageGuide/Extensions.html    Extensions can add new computed properties, but they cannot add stored properties, or add property observers to existing properties.)



하부 subclass에서 상부의 init()을 이용하는 경우 사용하기 전에 먼저 subclass에서 추가된 property의 값이 할당이 되어야 수행된다. 

Swift has a very clear, specific sequence of operations that are done in initializers. Let’s start with some basic examples and work our way up to a general case.

Let’s take an object A. We’ll define it as follows.

class A {
    var x: Int
    init(x: Int) {
        self.x = x
    }
}

Notice that A does not have a superclass, so it cannot call a super.init() function as it does not exist.

OK, so now let’s subclass A with a new class named B.

class B: A {
    var y: Int
    init(x: Int, y: Int) {
        self.y = y
        super.init(x: x)
    }
}

This is a departure from Objective-C where [super init] would typically be called first before anything else. Not so in Swift. You are responsible for ensuring that your instance variables are in a consistent state before you do anything else, including calling methods (which includes your superclass’ initializer).

subclass 를 이용 추가 property를 사용하는 예시들

class ViewController: UIViewController {

    var imageURL: NSURL?

    // this is a convenient way to create this view controller without a imageURL
    convenience init() {
        self.init(imageURL: nil)
    }

    init(imageURL: NSURL?) {
        self.imageURL = imageURL
        super.init(nibName: nil, bundle: nil)
    }

    // if this view controller is loaded from a storyboard, imageURL will be nil

    /* Xcode 6
    required init(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    */

    // Xcode 7 & 8
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
}

class UIGridCell:UIView {

   var id:String=“”

   override init(frame: CGRect) {

       super.init(frame: frame) // calls designated initializer

   }

   convenience init(id: String) {

       self.init(frame: CGRect.zero)

       self.id = id

   }

   required init?(coder aDecoder: NSCoder) {

       super.init(coder: aDecoder)

   }

}

기존의 UIView에 기능을 추가 해서 사용하는 방법으로는 subclass를 만들어 사용하는 것과 expension을 이용 확대하는 방법이 있다.

단 expension의 경우 stored property를 새로 추가 할수 는 없다. (참조 https://docs.swift.org/swift-book/LanguageGuide/Extensions.html    Extensions can add new computed properties, but they cannot add stored properties, or add property observers to existing properties.)



하부 subclass에서 상부의 init()을 이용하는 경우 사용하기 전에 먼저 subclass에서 추가된 property의 값이 할당이 되어야 수행된다. 

Swift has a very clear, specific sequence of operations that are done in initializers. Let’s start with some basic examples and work our way up to a general case.

Let’s take an object A. We’ll define it as follows.

class A {
    var x: Int
    init(x: Int) {
        self.x = x
    }
}

Notice that A does not have a superclass, so it cannot call a super.init() function as it does not exist.

OK, so now let’s subclass A with a new class named B.

class B: A {
    var y: Int
    init(x: Int, y: Int) {
        self.y = y
        super.init(x: x)
    }
}

This is a departure from Objective-C where [super init] would typically be called first before anything else. Not so in Swift. You are responsible for ensuring that your instance variables are in a consistent state before you do anything else, including calling methods (which includes your superclass’ initializer).

subclass 를 이용 추가 property를 사용하는 예시들

class ViewController: UIViewController {

    var imageURL: NSURL?

    // this is a convenient way to create this view controller without a imageURL
    convenience init() {
        self.init(imageURL: nil)
    }

    init(imageURL: NSURL?) {
        self.imageURL = imageURL
        super.init(nibName: nil, bundle: nil)
    }

    // if this view controller is loaded from a storyboard, imageURL will be nil

    /* Xcode 6
    required init(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    */

    // Xcode 7 & 8
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
}

class UIGridCell:UIView {

   var id:String=“”

   override init(frame: CGRect) {

       super.init(frame: frame) // calls designated initializer

   }

   convenience init(id: String) {

       self.init(frame: CGRect.zero)

       self.id = id

   }

   required init?(coder aDecoder: NSCoder) {

       super.init(coder: aDecoder)

   }

}