Created
November 4, 2025 10:44
-
-
Save sunmeat/1edd6f7fd525bb0b2c7dac82d80c2a8a to your computer and use it in GitHub Desktop.
objectanimator android property animation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| MainActivity.java: | |
| package site.sunmeat.helloworld; | |
| import android.animation.AnimatorSet; | |
| import android.animation.ObjectAnimator; | |
| import android.os.Bundle; | |
| import android.view.animation.AccelerateDecelerateInterpolator; | |
| import android.widget.TextView; | |
| import androidx.appcompat.app.AppCompatActivity; | |
| public class MainActivity extends AppCompatActivity { | |
| @Override | |
| protected void onCreate(Bundle savedInstanceState) { | |
| super.onCreate(savedInstanceState); | |
| setContentView(R.layout.activity_main); | |
| TextView myTextView = findViewById(R.id.myTextView); | |
| // базова анімація зсуву по x | |
| ObjectAnimator translateX = ObjectAnimator.ofFloat(myTextView, "translationX", 0f, 500f); | |
| translateX.setDuration(2000); | |
| translateX.setRepeatCount(ObjectAnimator.INFINITE); | |
| translateX.setRepeatMode(ObjectAnimator.REVERSE); | |
| // анімація масштабування (scale x та y) | |
| ObjectAnimator scaleX = ObjectAnimator.ofFloat(myTextView, "scaleX", 1f, 1.5f); | |
| scaleX.setDuration(2000); | |
| scaleX.setRepeatCount(ObjectAnimator.INFINITE); | |
| scaleX.setRepeatMode(ObjectAnimator.REVERSE); | |
| ObjectAnimator scaleY = ObjectAnimator.ofFloat(myTextView, "scaleY", 1f, 1.5f); | |
| scaleY.setDuration(2000); | |
| scaleY.setRepeatCount(ObjectAnimator.INFINITE); | |
| scaleY.setRepeatMode(ObjectAnimator.REVERSE); | |
| // анімація прозорості (alpha) | |
| ObjectAnimator alpha = ObjectAnimator.ofFloat(myTextView, "alpha", 1f, 0.3f); | |
| alpha.setDuration(2000); | |
| alpha.setRepeatCount(ObjectAnimator.INFINITE); | |
| alpha.setRepeatMode(ObjectAnimator.REVERSE); | |
| // анімація обертання (rotation) | |
| ObjectAnimator rotation = ObjectAnimator.ofFloat(myTextView, "rotation", 0f, 360f); | |
| rotation.setDuration(4000); // повільніше для ефекту | |
| rotation.setRepeatCount(ObjectAnimator.INFINITE); | |
| // об'єднання всіх анімацій в сет для паралельного виконання | |
| // сет для комбінації анімацій | |
| AnimatorSet animatorSet = new AnimatorSet(); | |
| animatorSet.playTogether(translateX, scaleX, scaleY, alpha, rotation); | |
| // інтерполятор для плавності (прискорення на початку/кінці) | |
| animatorSet.setInterpolator(new AccelerateDecelerateInterpolator()); | |
| // автоматичний запуск анімації при створенні актівіті | |
| animatorSet.start(); | |
| } | |
| } | |
| ================================================================================== | |
| activity_main.xml: | |
| <?xml version="1.0" encoding="utf-8"?> | |
| <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
| android:layout_width="match_parent" | |
| android:layout_height="match_parent" | |
| android:orientation="vertical"> | |
| <TextView | |
| android:id="@+id/myTextView" | |
| android:layout_width="wrap_content" | |
| android:layout_height="wrap_content" | |
| android:text="Анімований текст" | |
| android:textSize="18sp" /> | |
| </LinearLayout> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment