Android 애니메이션이 반복되지 않습니다.
여러 번 (또는 무한히) 반복되는 간단한 애니메이션을 만들려고합니다. 작동하지 않는
것 같습니다 android:repeatCount
!
다음은 내 애니메이션 리소스입니다 /res/anim/first_animation.xml
.
<?xml version="1.0" encoding="utf-8"?>
<set
xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false"
android:repeatCount="infinite"
>
<scale
android:interpolator="@android:anim/decelerate_interpolator"
android:duration="500"
android:fromXScale="1.0"
android:fromYScale="1.0"
android:toXScale="1.2"
android:toYScale="1.2"
android:pivotX="50%"
android:pivotY="50%"
android:fillAfter="false" />
<scale
android:interpolator="@android:anim/accelerate_interpolator"
android:startOffset="500"
android:duration="500"
android:fromXScale="1.2"
android:fromYScale="1.2"
android:toXScale="1.0"
android:toYScale="1.0"
android:pivotX="50%"
android:pivotY="50%"
android:fillAfter="false" />
</set>
먼저 이미지를 1.0에서 1.2 크기로 500ms 안에 조정해야합니다.
그런 다음 500ms 내에 다시 1.0으로 확장합니다.
사용 방법은 다음과 같습니다.
Animation firstAnimation = AnimationUtils.loadAnimation(this, R.anim.first_animation);
imgView.startAnimation(firstAnimation);
한주기를 만들고 끝납니다.
확장 된 다음 ans를 축소 한 다음 중지합니다.
의도 한대로 작동하도록하려면 어떻게해야합니까?
업데이트 : 2011 년 9 월에 Android 엔지니어가이 문제를 대부분 해결했습니다. XML에서 무시되었던 속성은 이제 작동하지만를 제외 repeatCount
하고 fillEnabled
는 여전히 무시됩니다 (어떤 이유로 의도적으로). 이것은 AnimationSet
불행히도 반복하기가 여전히 쉽지 않다는 것을 의미합니다 .
자세한 내용은 업데이트 된 문서 의 개요를 참조하십시오 (무시되는 속성, 작동하는 속성 및 자식에게 전달되는 속성 설명). ,, 그리고 실제로 하는 fillAfter
일 에 대한 더 깊은 이해는 여기 에있는 엔지니어 (Chet Haase)의 블로그 게시물을 참조하십시오 .fillBefore
fillEnabled
원래 답변
Pavel과 다른 사람들의 답변을 확장하려면 <set>
태그가 엄청나게 버그가 많은 것은 사실입니다 . repeatCount
다른 여러 속성을 올바르게 처리 할 수 없습니다 .
처리 할 수있는 것과 처리 할 수없는 것을 파악하는 데 몇 시간을 보냈으며 여기에 버그 보고서 / 문제를 제출했습니다. 문제 17662
요약하면 (이것에 관한 것입니다 AnimationSet
) :
setRepeatCount () / android : repeatCount
이 속성 (및 repeatMode)은 코드 또는 XML에서 작동하지 않습니다. 이로 인해 전체 애니메이션 세트를 반복하기가 어렵습니다.
setDuration () / android : duration
코드 WORKS의 AnimationSet에 설정하지만 (하위 애니메이션의 모든 기간을 재정의 함), XML의 태그에 포함되지 않은 경우
setFillAfter () / android : fillAfter
이것은 태그의 코드와 XML 모두에서 작동합니다. 이상하게도 fillEnabled를 true로 설정할 필요없이 작동하도록했습니다.
setFillBefore () / android : fillBefore
코드와 XML 모두에서 효과가 없거나 무시되는 것 같습니다.
setFillEnabled () / android : fillEnabled
코드와 XML 모두에서 효과가 없거나 무시되는 것 같습니다. fillEnabled를 포함하거나 fillEnabled를 false로 설정하지 않아도 fillAfter가 작동하도록 할 수 있습니다.
setStartOffset () / android : startOffset
이것은 XML이 아닌 코드에서만 작동합니다.
나는 <set> 태그에 AnimationSet 클래스에서 버그가있는 구현 이 있다는 것을 발견했습니다 . repeatCount를
올바르게 처리 할 수 없습니다 . 우리가 할 수있는 일은 <scale> 태그 에 repeatCount를 직접 설정하는 것 입니다. 이 XML 리소스는 잘 작동합니다.
<?xml version="1.0" encoding="utf-8"?>
<scale
xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:duration="200"
android:fromXScale="1.0"
android:fromYScale="1.0"
android:toXScale="1.05"
android:toYScale="1.05"
android:pivotX="50%"
android:pivotY="50%"
android:repeatMode="reverse"
android:fillAfter="false"
android:repeatCount="24"
/>
불행히도 이것은 한 번에 하나의 애니메이션으로 제한됩니다.
이런 식으로 애니메이션 시퀀스를 정의 할 수 없습니다 ...
속성을 포함해야합니다.
android:repeatCount="infinite"
하지만 "세트"가 아닌 "스케일"애니메이션에서
반복되는 애니메이션을 얻기 위해 애니메이션 리스너를 활용하고 종료시 애니메이션을 다시 호출했습니다. 이것은 브래킷이있는 애니메이션처럼 초점을 맞추는 카메라 레티 큘을 수행합니다.
다음은 애니메이션 레이아웃 xml입니다.
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<scale
android:fromXScale="1.0"
android:toXScale=".7"
android:fromYScale="1.0"
android:pivotX="50%"
android:pivotY="50%"
android:toYScale=".7"
android:duration="1000"/>
<scale
android:duration="1000"
android:fromXScale=".7"
android:toXScale="1.0"
android:fromYScale=".7"
android:pivotX="50%"
android:pivotY="50%"
android:toYScale="1.0"
android:startOffset="1000"/>
</set>
다음은 자바 코드입니다.
public void startAnimation() {
View brackets = findViewById(R.id.brackets);
brackets.setVisibility(View.VISIBLE);
Animation anim = AnimationUtils.loadAnimation(BuzzFinderActivity.this, R.anim.crosshair_focusing);
anim.setAnimationListener(new AnimationListener() {
@Override
public void onAnimationEnd(Animation arg0) {
Animation anim = AnimationUtils.loadAnimation(BuzzFinderActivity.this, R.anim.crosshair_focusing);
anim.setAnimationListener(this);
brackets.startAnimation(anim);
}
@Override
public void onAnimationRepeat(Animation arg0) {
// TODO Auto-generated method stub
}
@Override
public void onAnimationStart(Animation arg0) {
// TODO Auto-generated method stub
}
});
brackets.startAnimation(anim);
}
나는 또한 같은 문제에 직면했다 .. 나는 XMl 파일에 android : repeatCount = "infinite"를 포함 시켰습니다. 이제 잘 작동합니다 ...
<translate
android:fromXDelta="0"
android:toXDelta="80"
android:duration="1000"
android:repeatCount="infinite"
android:repeatMode="reverse"
android:pivotX="50%"
android:pivotY="50%"
android:fillAfter="true"/>
이 코드를 시도해 볼 수 있습니다. 코드에 추가하십시오.
firstAnimation.setRepeatCount(5);
일정 시간 동안 애니메이션이 반복됩니다.
firstAnimation.setRepeatCount(Animation.INFINITE);
firstAnimation.setRepeatMode(Animation.INFINITE);
애니메이션이 무기한 반복됩니다.
나는 Daniel의 코드를 사용하여 애니메이션을 정확한 횟수로 표시하려고했지만 문제가있었습니다. 애니메이션이 n 번 예상했을 때 대략 n / 2 번 표시되었습니다.
그래서 Daniel의 코드를 수정했습니다.
//...
@Override
public void onAnimationEnd(Animation arg0) {
mCurrentCount++;
if (mCurrentCount < REPEAT_COUNT) {
Animation anim = AnimationUtils.loadAnimation(BuzzFinderActivity.this, R.anim.crosshair_focusing);
anim.setAnimationListener(this);
brackets.post(new Runnable() {
@Override
public void run() {
brackets.startAnimation(anim);
}
}
}
}
//...
위에 표시된 변형을 사용하면 View.post () 메서드가 이전 애니메이션과 관련된 모든 작업을 완료 한 후 새 애니메이션을 시작할 수있는 기능을 제공하기 때문에 애니메이션이 정확히 REPEAT_COUNT 번 표시됩니다.
아래에서 제안한 xml 코드에 한 줄만 추가하면됩니다.
<scale
android:duration="500"
android:fromXScale="1.0"
android:fromYScale="1.0"
android:toXScale="1.2"
android:toYScale="1.2"
android:pivotX="50%"
android:pivotY="50%"
android:repeatCount="infinite" // just add this one line
android:fillAfter="false"
/>
</set>
Android SDK 버전 4.0.3 사용 :
주어진 애니메이션 요소에서 :
android : repeatCount = "-1"
무한한 애니메이션이됩니다.
프로젝트에 다음 클래스를 추가하십시오.
import android.view.View;
import android.view.animation.Animation;
public class AnimationRepeater implements Animation.AnimationListener
{
private View view;
private Animation animation;
private int count;
public AnimationRepeater(View view, Animation animation)
{
this.view = view;
this.animation = animation;
this.count = -1;
}
public AnimationRepeater(View view, Animation animation, int count)
{
this.view = view;
this.animation = animation;
this.count = count;
}
public void start()
{
this.view.startAnimation(this.animation);
this.animation.setAnimationListener(this);
}
@Override
public void onAnimationStart(Animation animation) { }
@Override
public void onAnimationEnd(Animation animation)
{
if (this.count == -1)
this.view.startAnimation(animation);
else
{
if (count - 1 >= 0)
{
this.animation.start();
count --;
}
}
}
@Override
public void onAnimationRepeat(Animation animation) { }
}
보기의 무한 루프를 위해 다음을 수행하십시오.
Animation a = AnimationUtils(Context, R.anim.animation);
new AnimationRepeater(View, a).start();
N 회만 애니메이션을 반복하려면 다음을 수행하십시오.
Animation a = AnimationUtils(Context, R.anim.animation);
new AnimationRepeater(View, a, int N).start();
N은 반복 횟수를 나타냅니다.
내 프로젝트에서 이전에 reverse를 사용 하여이 문제를 해결했습니다.
<scale android:interpolator="@android:anim/decelerate_interpolator" android:duration="500" android:fromXScale="1.0" android:fromYScale="1.0" android:toXScale="1.2" android:toYScale="1.2" android:pivotX="50%" android:pivotY="50%" android:repeatMode="reverse" android:repeatCount="infinite" />
저는 대부분의 작업을 프로그래밍 방식으로 수행하고이 작업에 늦거나 비효율적 일 수 있지만 반복 애니메이션 세트 목표를 완료했습니다 (2 개의 교대 애니메이션 세트도 있습니다). 이 코드는 단순히 하나의 이미지를 페이드 인하 고, 일시 중지 한 다음 페이드 아웃하고, 다른 이미지를 페이드 인하 고, 일시 중지하고, 페이드 아웃하고, 첫 번째 이미지를 다시 가져 오는 것입니다 (헹굼 및 반복). 먼저 내 Imageview를 정의했습니다.
final ImageView purple = (ImageView)findViewById(R.id.purp);
final ImageView yellow = (ImageView)findViewById(R.id.yell);
purple.setVisibility(View.INVISIBLE);
yellow.setVisibility(View.INVISIBLE);
그런 다음 각 애니메이션을 시작하고 중지 할시기를 처리하기 위해 두 개의 타이머, 작업 타이머 및 핸들러를 만들었습니다.
Timer p = new Timer();
TimerTask pu = new TimerTask() {
public void run() {
handler1.post(new Runnable() {
public void run()
{
fadein(purple);
}
});
}};
p.schedule(pu, 6000, 12000);
final Handler handler2 = new Handler();
Timer y = new Timer();
TimerTask ye = new TimerTask() {
public void run() {
handler2.post(new Runnable() {
public void run()
{
fadein(yellow);
}
});
}};
y.schedule(ye, 0, 12000);
마지막으로 애니메이션을 추가하여 애니메이션 세트를 만드는 대신 각 애니메이션을 시작할 때를 결정하기 위해 리스너에게 애니메이션을 제공합니다.
public void fadein (final ImageView image)
{
Animation anim = new AlphaAnimation(0, 1);
anim.setDuration(2000);
image.startAnimation(anim);
anim.setAnimationListener(new AnimationListener() {
public void onAnimationEnd(Animation animation)
{
image.clearAnimation();
image.invalidate();
pause(image);
}
@Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
@Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
}
});
}
public void pause (final ImageView image)
{
Animation anim = new AlphaAnimation(1, 1);
anim.setDuration(2000);
image.startAnimation(anim);
anim.setAnimationListener(new AnimationListener() {
public void onAnimationEnd(Animation animation)
{
image.clearAnimation();
image.invalidate();
fadeout(image);
}
@Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
@Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
}
});
}
public void fadeout (final ImageView image)
{
Animation anim = new AlphaAnimation(1,0);
anim.setDuration(2000);
image.startAnimation(anim);
anim.setAnimationListener(new AnimationListener() {
public void onAnimationEnd(Animation animation)
{
image.clearAnimation();
image.invalidate();
}
@Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
@Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
}
});
}
clearanimation 및 무효화는 이전 시도가 제대로 작동하도록합니다. 필요한지 아닌지 모르겠습니다.
이것이 누군가를 돕기를 바랍니다.
라이언
나는 이것을 가야한다 ... 나는 계속해서 원을 그리며 회전하는 뷰를 얻으려고 노력했다.
이전에는 rotation.setRepeatMode (-1)을 사용했지만 작동하지 않았습니다. setrepeatcount로 전환하고 작동합니다. 이것은 젤리 빈 4.2.2에 있습니다.
ObjectAnimator rotation = ObjectAnimator.ofFloat(myview,
"rotation", 360).setDuration(2000);
rotation.setRepeatMode(-1);
rotation.setRepeatCount(Animation.INFINITE);
rotation.start();
나는 같은 문제에 직면했지만 UI 스레드가 때때로 매우 바쁠 수 있다는 점 때문에 Java에서 타이밍 작업을하고 싶지 않았습니다. INFINITE 플래그는 설정된 태그에 대해 작동하지 않습니다. 그래서 약간의 코드로 문제를 해결했습니다.
mAnimation = (AnimationSet) AnimationUtils.loadAnimation(myContext, R.anim.blink);
mIcon.startAnimation(mAnimation);
mAnimation.setAnimationListener(new AnimationListener() {
public void onAnimationStart(Animation animation) {}
public void onAnimationRepeat(Animation animation) {}
public void onAnimationEnd(Animation animation) {
mIcon.startAnimation(mAnimation);
}
});
다음 XML로 :
<alpha
xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="1000"
android:fromAlpha="0.0"
android:toAlpha="1.0" />
<alpha
xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="1000"
android:fromAlpha="0.9"
android:startOffset="1000"
android:toAlpha="0.0" />
mIcon은 내 레이아웃의 ImageView입니다.
이 문제를 해결했습니다. 이것은 수정 버전입니다.
public class HelloAndroidActivity extends Activity {
private static String TAG = "animTest";
private Animation scaleAnimation;
private int currentCover = 0;
private List<ImageView> imageViews = new ArrayList<ImageView>(3);
private Button btn;
private ImageView img;
/**
* Called when the activity is first created.
* @param savedInstanceState If the activity is being re-initialized after
* previously being shut down then this Bundle contains the data it most
* recently supplied in onSaveInstanceState(Bundle). <b>Note: Otherwise it is null.</b>
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.i(TAG, "onCreate");
setContentView(R.layout.test);
img = (ImageView)findViewById(R.id.testpict);
imageViews.add(img);
img = (ImageView)findViewById(R.id.testpictTwo);
imageViews.add(img);
img = (ImageView)findViewById(R.id.testpict3);
imageViews.add(img);
scaleAnimation = AnimationUtils.loadAnimation(this, R.anim.photo_scale);
scaleAnimation.setAnimationListener(new CyclicAnimationListener());
btn = (Button)findViewById(R.id.startBtn);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
imageViews.get(0).startAnimation(scaleAnimation);
}
});
}
private class CyclicAnimationListener implements AnimationListener{
@Override
public void onAnimationEnd(Animation animation) {
currentCover += 1;
if(currentCover >= imageViews.size()){
currentCover = 0;
}
img = imageViews.get(currentCover);
scaleAnimation = AnimationUtils.loadAnimation(HelloAndroidActivity.this, R.anim.photo_scale);
scaleAnimation.setAnimationListener(new CyclicAnimationListener());
img.startAnimation(scaleAnimation);
}
@Override
public void onAnimationRepeat(Animation animation) {
Log.d("Animation", "Repeat");
}
@Override
public void onAnimationStart(Animation animation) {
}
}
}
이전 버전과 호환되는 앱을 작업하는 동안 방금이 문제를 발견했습니다. 너무 답답 해요! 결국 onCreate에서 호출 할 수있는 멋진 해결 방법 클래스를 코딩하고 애니메이션 리소스를 무한 루프로 시작했습니다.
AnimationLooper 클래스는 https://gist.github.com/2018678에서 사용할 수 있습니다.
인터넷의 답변을 통해 조사한 결과 저에게 완벽하게 맞는 솔루션을 찾았습니다. (예, repeatCount 및 repeatMode는 animationSet와 함께 사용할 때 매우 버그가 있습니다).
anim_rotate_fade.xml :
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:ordering="together" >
<objectAnimator
android:duration="3000"
android:propertyName="rotation"
android:repeatCount="1"
android:valueTo="360"
android:valueType="floatType" />
<objectAnimator
android:duration="3000"
android:propertyName="alpha"
android:repeatCount="1"
android:repeatMode="reverse"
android:valueFrom="0.0"
android:valueTo="0.3"
android:valueType="floatType" />
<objectAnimator
android:duration="3000"
android:propertyName="y"
android:repeatCount="1"
android:repeatMode="reverse"
android:valueFrom="380"
android:valueTo="430"
android:valueType="floatType" />
</set>
활동 중 : (애니메이션 종료 후 약간의 지연을 도입하여 해결).
ImageView starlightImageView = new ImageView(this);
starlightImageView.setImageResource(R.drawable.starlight);
final AnimatorSet animate = (AnimatorSet) AnimatorInflater.loadAnimator(this, R.anim.anim_rotate_fade);
AnimatorListenerAdapter animatorListener = new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
new Handler().postDelayed(new Runnable() {
@Override public void run() {
animate.start();
}
}, 1000);
}
};
animate.setTarget(starlightImageView);
animate.addListener(animatorListener);
연구하고 싶은 수업이 많이 있지만 현재는 매우 유연한 objectAnimator를 사용하고 있습니다. Animation 또는 AnimationUtils를 사용하지 않는 것이 좋습니다.
- 생기
- AnimationUtils
- 애니메이터
- AnimatorInflater
- AnimatorListener
- AnimatorListenerAdapter
첫 번째 애니메이션의 완료를 듣고 onStopAnimation 콜백에서 애니메이션을 다시 시작해야합니다.이 링크를 시도해보세요.
다시로드되지 않도록 리소스를 절약하기 위해 @Danufr 응답을 약간 조정합니다.
operator = (ImageView) findViewById(R.id.operator_loading);
final Animation ani = AnimationUtils.loadAnimation(getApplicationContext(),R.anim.finding_operator);
ani.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
operator.startAnimation(ani);
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
operator.setAnimation(ani);
스레드를 사용하여이 문제를 해결했습니다.
Button btn = (Button) findViewById(R.id.buttonpush);
final TextView textview = (TextView) findViewById(R.id.hello);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
textview.setText("...................");
final Animation animationtest = AnimationUtils.loadAnimation(MainActivity.this, android.R.anim.slide_in_left);
animationtest.setDuration(1000);
final Handler handler = new Handler();
Runnable runnable = new Runnable() {
public void run() {
handler.postDelayed(this, 1500);
textview.startAnimation(animationtest);
}
};
handler.postDelayed(runnable, 500); // start
handler.removeCallbacks(runnable); //STOP Timer
}
});
잘 작동한다
GifDrawable gifDrawable = (GifDrawable) gifImageView.getDrawable();
gifDrawable.setLoopCount(0);
위의 솔루션 중 어느 것도 제 경우에는 효과가 없었습니다. Danuofr의 솔루션은 애니메이션 세트에서 작동했지만 단위 테스트를 수행 할 때 테스트는이 무한 루프에 갇혀있었습니다. 마지막으로 제 경우에 따라이 애니메이션을 특정 횟수만큼 반복해야했습니다. 그래서 저는 수동으로 anim_rot.xml에있는 내 애니메이션의 복사본을 오프셋 값을 추가 하는 계단식 방식으로 추가했습니다 . 나는 그것이 나쁘고 많은 사람들에게 작동하지 않을 것이라는 것을 알고 있지만 내 경우에 대한 유일한 해결 방법이었습니다.
anim_rot.xml
<set xmlns:android="http://schemas.android.com/apk/res/android">
<rotate
android:duration="2000"
android:fromDegrees="20"
android:pivotX="29%"
android:pivotY="50%"
android:toDegrees="-20" />
<rotate
android:duration="2000"
android:fromDegrees="-20"
android:pivotX="29%"
android:pivotY="53%"
android:startOffset="2000"
android:toDegrees="20" />
<rotate
android:startOffset="4000"
android:duration="2000"
android:fromDegrees="20"
android:pivotX="29%"
android:pivotY="56%"
android:toDegrees="-20" />
<rotate
android:duration="2000"
android:fromDegrees="-20"
android:pivotX="29%"
android:pivotY="59%"
android:startOffset="6000"
android:toDegrees="20" />
<rotate
android:startOffset="8000"
android:duration="2000"
android:fromDegrees="20"
android:pivotX="29%"
android:pivotY="62%"
android:toDegrees="-20" />
<rotate
android:duration="2000"
android:fromDegrees="-20"
android:pivotX="29%"
android:pivotY="65%"
android:startOffset="10000"
android:toDegrees="20" />
</set>
애니메이션을 3 번 반복하기 위해 이렇게했습니다. 오프셋 값을 추가하여 더 많은 사본을 추가하여 특정 시간을 반복 할 수 있습니다.
루핑 스레드 또는 while / for 문에 코드를 추가해보십시오.
참고 URL : https://stackoverflow.com/questions/4480652/android-animation-does-not-repeat
'development' 카테고리의 다른 글
GhostDoc을 사용하여 Visual Studio에서 모든 TODO 항목보기 (0) | 2020.09.23 |
---|---|
Homebrew를 통해 Memcached를 설치했습니다. 서버를 시작하고 중지하는 방법은 무엇입니까? (0) | 2020.09.23 |
종료 애니메이션 이벤트 android (0) | 2020.09.23 |
모델에서 속성 변경을 감지하는 방법은 무엇입니까? (0) | 2020.09.23 |
Android : 사용 가능한 공간을 채우기 위해 뷰를 확장하는 방법은 무엇입니까? (0) | 2020.09.23 |