Не удается повторить анимацию для Android

#java #android

#Ява #Android

Вопрос:

Я пытался повторить определенную анимацию, но анимация выполняется только один раз и не повторяется.

animation.xml

 lt;?xml version="1.0" encoding="utf-8"?gt; lt;set xmlns:android="http://schemas.android.com/apk/res/android"gt;  lt;scale android:fromXScale="0"  android:toXScale="1"  android:fromYScale="0"  android:toYScale="1"  android:pivotX="50%"  android:pivotY="50%"  android:repeatMode="restart"  android:repeatCount="infinite"/gt;  lt;alpha android:fromAlpha="1"  android:toAlpha="0" /gt; lt;/setgt; 

MainActivity.java

 public class MainActivity extends AppCompatActivity {   private View view;   @Override  protected void onCreate(Bundle savedInstanceState) {  super.onCreate(savedInstanceState);  setContentView(R.layout.activity_main);  view = findViewById(R.id.view);   Animation animation = AnimationUtils.loadAnimation(this, R.anim.animation);  animation.setDuration(6000);  animation.setRepeatMode(Animation.RESTART);  animation.setRepeatCount(Animation.INFINITE);  view.startAnimation(animation);  } } 

Ответ №1:

Мне удалось заставить его работать.

animation.xml

 lt;?xml version="1.0" encoding="utf-8"?gt; lt;set xmlns:android="http://schemas.android.com/apk/res/android"  android:duration="6000"gt;  lt;scale  android:fromXScale="0"  android:fromYScale="0"  android:pivotX="50%"  android:pivotY="50%"  android:repeatCount="infinite"  android:repeatMode="restart"  android:toXScale="1"  android:toYScale="1" /gt;  lt;alpha  android:fromAlpha="1"  android:repeatCount="infinite"  android:repeatMode="restart"  android:toAlpha="0" /gt; lt;/setgt;  

и справиться с этим в Котлине

 var tex = findViewByIdlt;TextViewgt;(R.id.view);  tex.animation = AnimationUtils.loadAnimation(this,R.anim.animation)  tex.animation?.start()  

Проблема в том, что вам придется повторять обе анимации, которые вы повторяете в приложении масштабирования, но не в альфа-анимации.