본문 바로가기
안드로이드 코틀린

[Kotlin][Android] 이미지 회전

by teamnova 2022. 1. 30.

오늘은 아래와 같이 이미지뷰 회전을 해보겠습니다.

 

아래가 주요 코든데요

 

imageView.rotation 을 통해서 현재 각도를 구합니다.

 

그 후 아래의 메소드를 통해서 

ofFloat(

 target: T,

 property : Property<T, Float!>!,

 vararg values : Float

):ObjectAnimator!

 

target 은 T : 애니메이션 효과를 줄 개체이고

property 는 애니메이션이 되는 속성입니다.

Float는 애니메이션될 정도의 값입니다.

 

이를 통해서 setDuration(300) 을 통해서 몇 초 동안 애니메이션이 진행될지를 정해줍니다.

 

그리고 start를 통해서 애니메이션 효과가 진행됩니다. 이 내용은 아래와 같습니다.

 

private fun imageViewRotate(){
        val currentDegree = imageView.rotation
        ObjectAnimator.ofFloat(imageView,View.ROTATION, currentDegree, currentDegree+90f)
            .setDuration(300)
            .start()
    }

 

전체코드를 알려드리겠습니다.

class MainActivity : AppCompatActivity() {


    lateinit var imageView : ImageView
    lateinit var rotateBtn : Button

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        init()

        rotateBtn.setOnClickListener{
            Log.d(TAG, "onCreate: rotateButtonClick")
            imageViewRotate()
        }
    }

    fun init(){
        imageView = findViewById(R.id.imageView)
        rotateBtn = findViewById(R.id.button)
    }

    private fun imageViewRotate(){
        val currentDegree = imageView.rotation
        ObjectAnimator.ofFloat(imageView,View.ROTATION, currentDegree, currentDegree+90f)
            .setDuration(300)
            .start()
    }
}

이상으로 글을 마치겠습니다.