bogl
bogl

Reputation: 1864

Rotate drawable has not effect

The starting point here is the need to periodically rotate a vector drawing, like for instance the hands of an analog clock.

I understand that a vectorDrawable resource does not directly support rotation attributes. I can put the drawable inside a group which can rotate the drawable, but that rotation won't be modifiable at run time.

I came across many posts explaining rotating views or canvases, as well as the rotate drawable. The latter seems to be appropriate for my purpose.

I created a new activity with the help of Android Studio 3.5 to achieve a simple rotation with the help of a rotate drawable.

java/com.example.myapplication/MainActivity:

package com.example.myapplication

import android.graphics.drawable.RotateDrawable
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.core.content.ContextCompat

class MainActivity : AppCompatActivity() {

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


        val activity = this@MainActivity
        val context  = activity.applicationContext

        val drawable = ContextCompat.getDrawable(context, R.drawable.rotation)
        val rotation = drawable as RotateDrawable

        rotation.setLevel(180)
    }
}

res/layout/activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/rotation" />
</androidx.constraintlayout.widget.ConstraintLayout>

res/drawable/rotation.xml:

<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
    android:pivotX="50%" android:pivotY="50%"
    android:fromDegrees="0" android:toDegrees="360"
    android:drawable="@drawable/ic_launcher_foreground"/>

I expect that the setLevel function can be used to rotate the drawable, but it has no effect. Changing the fromDegrees attribute in the rotation.xml does the rotation as expected. I am not sure if the maximum level for <rotate> should be 360 or 10000, but that is not the issue.

What am I missing? Is it a detail, or is the rotateDrawable the wrong approach altogether?


Related questions:

Upvotes: 0

Views: 770

Answers (1)

GV_FiQst
GV_FiQst

Reputation: 1567

Try using imageView2.setRotation(180) instead of rotation drawable.

Upvotes: 1

Related Questions