Xoay nền bên trong ImageView (Android Studio - Java)

Sep 26 2020

Tôi đang cố gắng phát triển ứng dụng Android. Tuy nhiên, tôi gặp phải một vấn đề và đó là tôi muốn xoay nền hình vuông đầy màu sắc 360 độ. Nó sẽ là hiệu ứng hoạt hình. Liên tục xoay. Có khả thi không?

Tôi đã thử view.animate (). Xoay () nhưng tất nhiên, nó xoay imageview. Tôi chỉ muốn xoay nền có thể vẽ được trong chế độ xem ảnh.

Mục tiêu của tôi

Bố cục hoạt động

<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>

Nền (@ 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" />

Trả lời

SnefruClone Oct 10 2020 at 08:10

Tôi đã sử dụng thẻ xoay trong danh sách hoạt ảnh.

<?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>

Đoạn mã trên có thể là tối ưu nhưng đây là phần quan trọng nhất để xoay liên tục

AnimationDrawable animationDrawable =
(AnimationDrawable) mQRImage.getBackground();
animationDrawable.setExitFadeDuration(3000);
animationDrawable.start();