0

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:

bogl
  • 1,864
  • 1
  • 15
  • 32
  • 1
    what's wrong with `imageView2.setRotation(180)`? Or try to supply bigger value then 180. Try it with `2500` – GV_FiQst Sep 27 '19 at 15:43
  • I looked at the sources and saw this line there: `final float value = level / (float) MAX_LEVEL;` Here `MAX_LEVEL = 10000`. So when you pass 180 it gets divided and you have rotation to `6.48` degrees – GV_FiQst Sep 27 '19 at 15:48
  • 360 vs 10000 is not the issue. I did try all kind of values. I am doing my first baby steps with the android API and its documentation. It would still be interesting to understand why the rotate drawable doesn't work as expected. – bogl Sep 27 '19 at 17:10

1 Answers1

1

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

GV_FiQst
  • 1,487
  • 14
  • 30