Question

J'ai créé une mise en page avec une vue de l'image et une vue web. La vue Web est configuré pour avoir une visibilité par défaut de parti. Lorsque les feux d'activité jusqu'à il affiche l'affichage de l'image en premier et quand la vue Web est terminé son URL, il se marque aussi visible et l'imageview est marqué comme caché.

Lorsque le imageview est montré, je voudrais le faire tourner à plusieurs reprises juste pour un peu pizazz ajouté.

Je ne l'ai jamais fait avant des animations dans Android et tous les postes que j'ai trouvé quand j'ai demandé l'Internet était pas utile; ainsi, je suis retourné à l'aide SO.

Donc, si je commence par ce ...

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

Comment puis-je créer une animation rotate répétée et l'appliquer à la ImageView?

Merci encore!

Était-ce utile?

La solution

Utilisez un RotateAnimation , le réglage du point de pivotement de la centre de l'image.

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

Autres conseils

Comment faire pivoter une image autour de son centre:

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.

Cela provoquera l'image pour faire tourner autour de son centre (0,5 ou 50% de sa largeur / hauteur). Je signale cela pour les futurs lecteurs qui obtiennent ici de Google, comme je l'ai, et qui souhaitent faire pivoter l'image autour de son centre sans définir ledit centre en pixels absolus.

Vous pouvez également utiliser simplement la fonction d'animation Rotation. Cela va à une animation spécifique, pour un montant prédéterminé de temps, sur un ImageView.

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

Créez ensuite un fichier XML d'animation dans votre résolution / anim appelé rotate_picture avec le contenu:

<?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>

Maintenant, malheureusement, cela ne fonctionnera qu'une seule fois. Vous aurez besoin d'une boucle quelque part pour faire répéter l'animation alors qu'il attend. J'ai expérimenté un peu et obtenu mon programme coincé dans des boucles infinies, donc je ne suis pas sûr de la meilleure façon de cela. EDIT: La réponse de Christopher fournit les informations sur la façon de faire la boucle correctement, donc enlever ma mauvaise suggestion au sujet des threads séparés

Une façon - vous diviser l'image en N tournant légèrement à chaque fois. Je dirais que 5 est suffisant. puis créer quelque chose comme ça dans 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> 

démarrage de code

progress.setVisibility(View.VISIBLE);
AnimationDrawable frameAnimation = (AnimationDrawable)progress.getDrawable();
frameAnimation.setCallback(progress);
frameAnimation.setVisible(true, true);

arrêt de code

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

A l'intérieur de l'élément mis:

android:repeatCount="infinite"

Je l'ai découvert, que si vous utilisez le .getWidth / 2 etc ... qu'il ne fonctionne, vous devez obtenir le nombre de pixels de l'image est et le diviser par 2, puis vous tapez juste dans la numéro pour les 2 derniers arguments.

so dire l'image est un pixel 120 par 120 pixels carrés, ur x et y serait égale à 60 pixels. donc dans votre code, vous le feriez droit:

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

et maintenant l'image pivote autour de son centre.

code de vérification:

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>

Ne pas dur limites d'image de code. Il suffit d'utiliser:

RotateAnimation anim = new RotateAnimation( fromAngle, toAngle, imageView.getDrawable().getBounds().width()/2, imageView.getDrawable().getBounds().height()/2);
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top