Android ImageView 애니메이션
-
19-09-2019 - |
문제
이미지보기와 웹보기로 레이아웃을 만들었습니다. 웹보기는 기본 가시성이 사라지도록 설정되었습니다. 활동이 시작되면 이미지보기가 먼저 표시되고 웹보기가 URL로드를 완료하면 표시가 표시되고 ImageView가 숨겨진 것으로 표시됩니다.
ImageView가 표시되면 약간 추가 된 Pizazz를 위해 반복적으로 회전하고 싶습니다.
나는 안드로이드에서 애니메이션을 한 적이 없으며 인터넷에 물었을 때 찾은 모든 게시물은 도움이되지 않았다. 따라서 나는 도움을 위해 SO로 돌아 왔습니다.
그래서 내가 이것으로 시작하면 ...
final ImageView splash = (ImageView)findViewById(R.id.splash);
반복적 인 회전 애니메이션을 생성하고 ImageView에 적용하려면 어떻게합니까?
다시 한 번 감사드립니다!
해결책
a RotateAnimation
, 피벗 지점을 이미지의 중심으로 설정합니다.
RotateAnimation anim = new RotateAnimation(0f, 350f, 15f, 15f);
anim.setInterpolator(new LinearInterpolator());
anim.setRepeatCount(Animation.INFINITE);
anim.setDuration(700);
// Start animating the image
final ImageView splash = (ImageView) findViewById(R.id.splash);
splash.startAnimation(anim);
// Later.. stop the animation
splash.setAnimation(null);
다른 팁
중앙 주변의 이미지를 회전하는 방법 :
ImageView view = ... //Initialize ImageView via FindViewById or programatically
RotateAnimation anim = new RotateAnimation(0.0f, 360.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
//Setup anim with desired properties
anim.setInterpolator(new LinearInterpolator());
anim.setRepeatCount(Animation.INFINITE); //Repeat animation indefinitely
anim.setDuration(700); //Put desired duration per anim cycle here, in milliseconds
//Start animation
view.startAnimation(anim);
//Later on, use view.setAnimation(null) to stop it.
이렇게하면 이미지가 중심 주위에서 회전하게됩니다 (폭/높이의 0.5 또는 50%). 나는 Google에서 여기에 도착한 미래의 독자들을 위해 이것을 게시하고 있으며, 그 중심을 절대 픽셀로 정의하지 않고 중앙 주위의 이미지를 회전시키려는 사람을 위해 이것을 게시하고 있습니다.
회전 애니메이션 기능을 사용하면 간단히 사용할 수 있습니다. 이는 이미지 뷰에서 미리 정해진 시간 동안 특정 애니메이션을 실행합니다.
Animation rotate = AnimationUtils.loadAnimation([context], R.anim.rotate_picture);
splash.startAnimation(rotate);
그런 다음 컨텐츠와 함께 rotate_picture라는 RES/Anim에서 애니메이션 XML 파일을 만듭니다.
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<rotate
android:fromDegrees="0"
android:toDegrees="360"
android:duration="5000"
android:pivotX="50%"
android:pivotY="50%">
</rotate>
</set>
불행히도, 이것은 한 번만 실행됩니다. 기다리는 동안 애니메이션을 반복하기 위해 어딘가에 루프가 필요합니다. 나는 약간의 실험을하고 프로그램이 무한 루프에 갇히게되었으므로 그에 대한 가장 좋은 방법은 확실하지 않습니다. 편집 : Christopher의 답변은 올바르게 루프를 만드는 방법에 대한 정보를 제공하므로 별도의 스레드에 대한 나쁜 제안을 제거합니다!
한 가지 방법 - 이미지를 n으로 나누어 매번 약간 회전합니다. 5가 충분하다고 말하고 싶습니다. 그런 다음 Drawable에서 이와 같은 것을 만듭니다
<animation-list android:id="@+id/handimation" android:oneshot="false"
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/progress1" android:duration="150" />
<item android:drawable="@drawable/progress2" android:duration="150" />
<item android:drawable="@drawable/progress3" android:duration="150" />
</animation-list>
코드 시작
progress.setVisibility(View.VISIBLE);
AnimationDrawable frameAnimation = (AnimationDrawable)progress.getDrawable();
frameAnimation.setCallback(progress);
frameAnimation.setVisible(true, true);
코드 중지
AnimationDrawable frameAnimation = (AnimationDrawable)progress.getDrawable();
frameAnimation.stop();
frameAnimation.setCallback(null);
frameAnimation = null;
progress.setVisibility(View.GONE);
더 여기
imgDics = (ImageView) v.findViewById(R.id.img_player_tab2_dics);
imgDics.setOnClickListener(onPlayer2Click);
anim = new RotateAnimation(0f, 360f,
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
0.5f);
anim.setInterpolator(new LinearInterpolator());
anim.setRepeatCount(Animation.INFINITE);
anim.setDuration(4000);
// Start animating the image
imgDics.startAnimation(anim);
요소 내부 :
android:repeatCount="infinite"
.getWidth/2 등을 사용하면 작동하지 않는다는 것을 알았습니다. 이미지가 픽셀의 수를 가져 와서 스스로 나누어야합니다. 마지막 2 개의 주장.
따라서 이미지가 120 픽셀 x 120 픽셀 스퀘어, UR X와 Y는 60 픽셀과 같을 것입니다. 따라서 코드에서는 다음과 같습니다.
RotateAnimation anim = new RotateAnimation(0f, 350f, 60f, 60f);
anim.setInterpolator(new LinearInterpolator());
anim.setRepeatCount(Animation.INFINITE);
anim.setDuration(700);
그리고 이제 당신의 이미지는 중앙에 피벗됩니다.
검증 된 코드 :
imageView.setImageResource(R.drawable.ic_arrow_up);
boolean up = true;
if (!up) {
up = true;
imageView.startAnimation(animate(up));
} else {
up = false;
imageView.startAnimation(animate(up));
}
private Animation animate(boolean up) {
Animation anim = AnimationUtils.loadAnimation(this, up ? R.anim.rotate_up : R.anim.rotate_down);
anim.setInterpolator(new LinearInterpolator()); // for smooth animation
return anim;
}
grawable/ic_arrow_up.xml
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="#3d3d3d"
android:pathData="M7.41,15.41L12,10.83l4.59,4.58L18,14l-6,-6 -6,6z"/>
</vector>
Anim/rotate_up.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true"
android:fillEnabled="true">
<rotate
android:duration="200"
android:fromDegrees="-180"
android:pivotX="50%"
android:pivotY="50%"
android:toDegrees="0" />
</set>
Anim/rotate_down.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true"
android:fillEnabled="true">
<rotate
android:duration="200"
android:fromDegrees="0"
android:pivotX="50%"
android:pivotY="50%"
android:toDegrees="180" />
</set>
하드 코드 이미지 경계를 사용하지 마십시오. 그냥 사용 :
RotateAnimation anim = new RotateAnimation( fromAngle, toAngle, imageView.getDrawable().getBounds().width()/2, imageView.getDrawable().getBounds().height()/2);