ImageView内部的背景旋转(Android Studio-Java) [英] Background Rotation inside ImageView (Android Studio - Java)
问题描述
我正在尝试开发android应用程序.但是,我遇到了一个问题,那就是我想将彩色正方形背景旋转360度.这将是动画效果.不断旋转.有可能吗?
I am trying to develop android application. However, I faced with an issue and it is I want to rotate the colorful square background 360 degrees. It is going to be animation effect. Continuously rotate. Is it possible?
我尝试过view.animate().rotate(),但是它当然会旋转imageview.我只想旋转imageview中的背景可绘制对象.
I tried view.animate().rotate() but of course , it rotates imageview. I just want to rotate background drawable in imageview.
活动布局
<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">
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="@drawable/border"
android:contentDescription="@string/todo"
android:cropToPadding="true"
android:padding="10dp"
android:scaleType="centerCrop"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent />
</androidx.constraintlayout.widget.ConstraintLayout>
背景(@ drawable/border)
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:angle="270"
android:endColor="@color/colorYellowGreen"
android:type="linear"
android:centerColor="@color/colorBlue"
android:startColor="@color/colorPink" />
<corners android:radius="5dp" />
推荐答案
我在动画列表中使用了旋转标签.
I used rotate tag in animation-list.
<?xml version="1.0" encoding="utf-8"?>
<animation-list
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:duration="3000">
<rotate
android:fromDegrees="90"
android:toDegrees="180"
android:drawable="@drawable/border"
android:pivotY="50%"
android:pivotX="50%"/>
</item>
<item
android:duration="3000">
<rotate
android:fromDegrees="180"
android:toDegrees="270"
android:drawable="@drawable/border"
android:pivotY="50%"
android:pivotX="50%"/>
</item>
<item
android:duration="3000">
<rotate
android:fromDegrees="270"
android:toDegrees="360"
android:drawable="@drawable/border"
android:pivotY="50%"
android:pivotX="50%"/>
</item>
<item
android:duration="3000">
<rotate
android:fromDegrees="0"
android:toDegrees="90"
android:drawable="@drawable/border"
android:pivotY="50%"
android:pivotX="50%"/>
</item>
</animation-list>
上面的代码可能是最优的,但这是连续旋转
The code above may be optinal but this is the most important part for continously rotation
AnimationDrawable animationDrawable =
(AnimationDrawable) mQRImage.getBackground();
animationDrawable.setExitFadeDuration(3000);
animationDrawable.start();
这篇关于ImageView内部的背景旋转(Android Studio-Java)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!