Nice programing

Android ImageView 애니메이션

nicepro 2020. 12. 2. 21:54
반응형

Android ImageView 애니메이션


이미지보기와 웹보기가있는 레이아웃을 만들었습니다. 웹보기는 기본 가시성을 가지도록 설정되어 있습니다. 활동이 시작되면 먼저 이미지보기를 표시하고 웹보기가 URL로드를 완료하면 자체적으로 표시되고 이미지보기는 숨김으로 표시됩니다.

imageview가 표시 될 때 약간 추가 된 pizazz를 위해 반복적으로 회전하고 싶습니다.

나는 전에 안드로이드에서 애니메이션을 해본 적이없고 내가 인터넷에 물었을 때 찾은 모든 포스트가 도움이되지 않았다. 따라서 나는 도움을 위해 SO로 돌아 왔습니다.

그래서 이걸로 시작하면 ...

    final ImageView splash = (ImageView)findViewById(R.id.splash);

반복되는 회전 애니메이션을 만들고 ImageView에 적용하려면 어떻게해야합니까?

다시 한 번 감사드립니다!


를 사용 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 %)을 중심으로 회전합니다. 나는 구글에서 여기에 왔고 절대 픽셀로 중심을 정의하지 않고 중심을 중심으로 이미지를 회전하려는 미래의 독자를 위해 이것을 게시하고 있습니다.


애니메이션 회전 기능을 사용하기 만하면됩니다. ImageView에서 미리 결정된 시간 동안 특정 애니메이션을 실행합니다.

Animation rotate = AnimationUtils.loadAnimation([context], R.anim.rotate_picture);
splash.startAnimation(rotate);

그런 다음 res / anim에 rotate_picture라는 내용으로 애니메이션 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 개면 충분합니다. 그런 다음 드로어 블에서 이와 같은 것을 만듭니다.

<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로 나눈 다음 숫자를 입력해야합니다. 마지막 2 개의 인수.

so say your image was a 120 pixel by 120 pixel square, ur x and y would equal 60 pixels. so in your code, you would right:

RotateAnimation anim = new RotateAnimation(0f, 350f, 60f, 60f);
anim.setInterpolator(new LinearInterpolator());
anim.setRepeatCount(Animation.INFINITE);
anim.setDuration(700);

and now your image will pivot round its center.


Verified Code:

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

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

Don't hard code image bounds. Just use:

RotateAnimation anim = new RotateAnimation( fromAngle, toAngle, imageView.getDrawable().getBounds().width()/2, imageView.getDrawable().getBounds().height()/2);

참고URL : https://stackoverflow.com/questions/2032304/android-imageview-animation

반응형